Results 1 to 15 of 15

Thread: **RESOLVED**SQL Statement - Need a little HELP!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679

    **RESOLVED**SQL Statement - Need a little HELP!

    Ok, I thought that I was done, then the Management team pull me back!

    On one of my forms I needed to view only records that follow a certain criteria....within the OnLoad event I have the following code:

    VB Code:
    1. Dim rs as Recordset
    2. Set rs = New Recordset
    3.  
    4. rs.Open "SELECT AgentCompleted FROM Main WHERE AgentCompleted <> ' " & Text1.Text & "'", MyDataEnvironment, MyConnection
    5. rs.MoveFirst
    6. rs.Close
    7. ' so on
    8. ' and so on
    Now this workd great, it opens to the first record that fits the SQL criteria..but now on the form I have a command button for "Next Record"...I wrote the following code inside this command:

    VB Code:
    1. Dim rs as Recordset
    2. Set rs = New Recordset
    3. rs.Open "SELECT AgentCompleted FROM Main WHERE AgentCompleted <> ' " & Text1.Text & "'", MyDataEnvironment, MyConnection
    4. rs.MoveNextrs.Close
    5. 'so on
    but this SQL will not work....it does not give me an error message, but it will not move to the next available record???

    Any suggestions??

    Thanks
    Last edited by Salvatore; May 28th, 2003 at 01:18 PM.

  2. #2
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Don't believe
    Code:
    rs.MoveNextrs.Close
    is a valid ado method.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Sorry, I wrote it wrong in here...

    it should be :

    VB Code:
    1. Dim rs as Recordset
    2. Set rs = New Recordset
    3.  
    4. rs.Open "SELECT AgentCompleted FROM Main WHERE AgentCompleted <> ' " & Text1.Text & "'", MyDataEnvironment, MyConnection
    5. rs.MoveNext
    6. rs.Close

  4. #4
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Why not set rs as a form level variable (b/c you certainly don't need the traffic of going back and forth to the server for the *exact same data*) and then call the .MoveNext method from the Next Record cmd button?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    I'm sorry if I sound stupid...but would the form level variable be set within the Load event or right within the cmd button?

    And is this done to avoid creating a new recordset?

    Thanks for the lesson!

  6. #6
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    You need to do some reading on variable scope. http://msdn.microsoft.com/library/de...fvariables.asp
    Code:
    Option Explicit
    Dim rs As Recordset
    
    Private Sub Form_Load()
        Set rs = New Recordset
        rs.Open "SELECT AgentCompleted FROM Main WHERE AgentCompleted <> ' " & Text1.Text & "'", MyDataEnvironment, MyConnection
        rs.MoveFirst
    End Sub
    
    Private Sub cmdNextRecord_Click()
        rs.MoveNext
    End Sub
    
    Private Sub Form_Terminate()
        rs.Close
        Set rs = Nothing
    End Sub

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Briantcva,

    I appreciate the link, and yes I definitely need to do some more reading on these subjects...

    I took your suggestion, and at first I received the Object not set errors, so I adjusted the code so that the Dim statements that were originally in each sub command were removed, and made sure that the proper set statments were placed....

    But it still is not moving...I can tell this, because also within the code I am using the same SQL query as I gave earlier to count the records that fit that criteria...and it chows that there are still some records...

  8. #8
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    How can you tell it's not moving? Do you have bound (or manually filled for that matter) text boxes and the like that aren't updating?

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    Yes that's right...I can tell if it is not moving because within the Next Record cmd button I have a text box showing the total number of records that fit the criteria outlined in the SQl query...right now the count shows 2.

    So when I open the form and place a name in let's say "Agent" text box and hit the Next cmd button it should send that name to the table and adjust the record count..

    Right now all it does is stay on the same record, the number is not changing...

  10. #10
    Fanatic Member
    Join Date
    May 2002
    Posts
    746

    Post

    General declerations, Form_Load, cmdNext, common subs that might be called.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    VB Code:
    1. Option Explicit
    2. Dim rs As Recordset
    3.  
    4.  
    5. Private Sub cmdNextRec_Click()
    6. Const Formclose As Long = 3021
    7. On Error GoTo ErrorMsg
    8.  
    9.  
    10. Dim i As Integer, j As Integer
    11. Set rs = New Recordset
    12.  
    13. rs.MoveNext
    14.  
    15. rs.Open "SELECT AgentCmpltd FROM Main WHERE AgentCmpltd <> '" & Text1.Text & "'", MyDataEnvironment.MyConnection
    16. i = rs.RecordCount
    17. Text2.Text = i
    18. rs.Close
    19.  
    20. rs.Open "SELECT RepName FROM Main WHERE RepName <> '" & Text1.Text & "'", MyDataEnvironment.MyConnection
    21. j = rs.RecordCount
    22. Text3.Text = j
    23. rs.Close
    24.  
    25. txtSlsRefNot.Text = Text3.Text - Text2.Text
    26.  
    27. DisplayEnd:
    28.     Exit Sub
    29.  
    30. ErrorMsg:
    31.     If Err = Formclose Then
    32.     frmMsgboxLstRec.Show
    33.     End If
    34. Resume DisplayEnd
    35. End Sub
    36. __________________________________________________
    37. Private Sub cmdPrevRec_Click()
    38. Const Formclose As Long = 3021
    39. On Error GoTo ErrorMsg
    40.  
    41. MyDataEnvironment.rsCommand1.MovePrevious
    42.  
    43. DisplayEnd:
    44.     Exit Sub
    45.  
    46. ErrorMsg:
    47.     If Err = Formclose Then
    48.     frmMsgbx1stRec.Show
    49.     End If
    50. Resume DisplayEnd
    51.  
    52. End Sub
    53. ____________________________________________________
    54. Private Sub Form_Load()
    55. Dim i As Integer, j As Integer
    56. Set rs = New Recordset
    57.  
    58. rs.Open "SELECT AgentCmpltd FROM Main WHERE AgentCmpltd <> '" & Text1.Text & "'", MyDataEnvironment.MyConnection
    59. If rs.EOF = True And rs.BOF = True Then
    60. 'do nothing
    61. Else
    62. rs.MoveFirst
    63.  
    64. If rs.RecordCount <> 0 Then
    65. cmdPrevRec.Visible = True
    66. Else
    67. cmdPrevRec.Visible = False
    68.  
    69. End If
    70. txtDateCmpltd.Text = Date
    71. rs.Close
    72.  
    73. rs.Open "SELECT AgentCmpltd FROM Main WHERE AgentCmpltd <> '" & Text1.Text & "'", MyDataEnvironment.MyConnection
    74. i = rs.RecordCount
    75. Text2.Text = i
    76. rs.Close
    77.  
    78. rs.Open "SELECT RepName FROM Main WHERE RepName <> '" & Text1.Text & "'", MyDataEnvironment.MyConnection
    79. j = rs.RecordCount
    80. Text3.Text = j
    81. rs.Close
    82.  
    83. txtSlsRefNot.Text = Text3.Text - Text2.Text
    84. End If
    85.  
    86.  
    87. End Sub

  12. #12
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Please tell me you're indenting your code in your app and that it's just a formatting issue here? Makes things ever so much easier to read. Anyway...

    Why are you opening the exact same rs twice in your form load?

    Your problem is that you're closing the rs each time you use it. Open the rs when you load the form and then use the .MoveNext/.MovePrevious to navigate. Close and dum the rs when you do the same to the form.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    The reason why I have the recordsets opening and closing, was that I needed to use the various queries in order to pull the data...

    All I simply want to do is use this form to view entries made by the Main form which are fed to the Access table...when the user view a record they will need to enter their name into a textbox...this will serve as a signature...I had the form calculate how many records were in the table that did not have a signature...that is where the queries came into play.

    Once all records had an agent;s signature then I would alert them that there are no more records to view...this is where I am...I can move through the recordset, but I am having trouble with limiting the recordset to just records that have no agent name?? Also I noticed that when I went to the Access table to view th records from there..that each time I go to this form it is creating a new record

    So I am back to square 1...

    Any ideas will be appreciated...

    Thank you.

  14. #14
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Once all records had an agent;s signature then I would alert them that there are no more records to view...this is where I am...I can move through the recordset, but I am having trouble with limiting the recordset to just records that have no agent name??
    Create a new query in Access w/ the "Find Unmatched Query Wizard."

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Location
    New York
    Posts
    679
    That was an excellent suggestion!!! I should have thought of that...I guess sometimes thinking a little simpler, is really the best way to go!

    Thank you once again..

    I hope that you don't come looking for payment on my tab!!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width