Results 1 to 8 of 8

Thread: [RESOLVED] Changing the recordset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    66

    Resolved [RESOLVED] Changing the recordset

    Hello

    How do you change the recordset from one access table to another

    within the same form? i want to test a query in an If statment within

    a Select Case for two separate tables. Here is part of my code...

    Select Case rstRecordSet!Difficulty

    Case "Easy"
    MsgBox "Your level is set to easy"

    If txtUser.Text = rstRecordSet!Username Then

    Msgbox "test"

    Else
    sqlstring = "INSERT INTO tbleasy (Username) VALUES ('" &

    txtUser.Text & "')"

    conConnection.Execute sqlstring, , adCmdText + adExecuteNoRecords

    End If


    rstRecordSet!Difficulty is in one table and I want rstRecordset!

    Username to relate to another table.

    Thanks in advance

    Piglet

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Changing the recordset

    Are you using Bound Controls?

    If not, then it would be a simple matter of creating two recordset objects.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    66

    Re: Changing the recordset

    I have added another recordset but how do i let it know which table to refer to? It is still referring to the same table as the first recordset.

    Thanks

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Changing the recordset

    Show me what you are doing.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    66

    Re: Changing the recordset

    Here is the relevant part of my code:


    Private Sub Command1_Click()

    Dim conConnection As New ADODB.Connection
    Dim cmdCommand As New ADODB.Command
    Dim rstRecordSet As New ADODB.Recordset

    Dim rstRecordSet2 As New ADODB.Recordset


    conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    App.Path & "\" & "calculations.mdb;Mode=Read|Write"

    conConnection.CursorLocation = adUseClient


    conConnection.Open


    With cmdCommand
    .ActiveConnection = conConnection
    .CommandText = "SELECT * FROM tblRegister;"
    .CommandType = adCmdText
    End With


    With rstRecordSet
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .Open cmdCommand
    End With

    With rstRecordSet2
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .Open cmdCommand
    End With


    'tests each record in the table
    Dim bMatch As Boolean

    bMatch = False
    Do While bMatch = False


    If (txtUser.Text = rstRecordSet!Username) And (txtPwd.Text = rstRecordSet!password) Then
    bMatch = True
    Else
    rstRecordSet.MoveNext

    If rstRecordSet.EOF = True Then

    Exit Do
    End If
    End If
    Loop


    If bMatch Then

    Dim sqlstring As String
    Dim bMatch2 As Boolean

    Select Case rstRecordSet!Difficulty

    Case "Easy"

    MsgBox "Your level is set to easy"

    bMatch2 = False
    Do While bMatch2 = False

    If (txtUser.Text = rstRecordSet2!Username) Then
    bMatch2 = True
    Else
    rstRecordSet2.MoveNext

    If rstRecordSet2.EOF = True Then

    Exit Do
    End If
    End If
    Loop

    If bMatch2 Then 'the record matches

    MsgBox "Already in tblEasy"

    Else

    sqlstring = "INSERT INTO tbleasy (Username) VALUES ('" & txtUser.Text & "')"

    conConnection.Execute sqlstring, , adCmdText + adExecuteNoRecords

    MsgBox "Just added"

    End If


    I have 4 tables, tblRegister, tblEasy, tblMedium & tblHard. The primary key for tblRegister is 'Username' which is the foreign key in each of the other tables. The Easy, Medium & Hard tables each have their own primary key called '<difficulty>_ID'. Im unsure how to make rstRecordSet2 refer to tblEasy. Any suggestions?

    Thank you!

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Changing the recordset

    Did you post everything? Without the vbcode tags, it made no sense, so I copied it in to a command button, and then did some indenting, and I'm amazed that this doesn't produce errors.
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim conConnection As New ADODB.Connection
    3. Dim cmdCommand As New ADODB.Command
    4. Dim rstRecordSet As New ADODB.Recordset
    5. Dim rstRecordSet2 As New ADODB.Recordset
    6.  
    7.  
    8. conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    9. App.Path & "\" & "calculations.mdb;Mode=Read|Write"
    10.  
    11. conConnection.CursorLocation = adUseClient
    12.  
    13. conConnection.Open
    14.  
    15. With cmdCommand
    16. .ActiveConnection = conConnection
    17. .CommandText = "SELECT * FROM tblRegister;"
    18. .CommandType = adCmdText
    19. End With
    20.  
    21. With rstRecordSet
    22. .CursorType = adOpenStatic
    23. .CursorLocation = adUseClient
    24. .LockType = adLockOptimistic
    25. .Open cmdCommand
    26. End With
    27.  
    28. With rstRecordSet2
    29. .CursorType = adOpenStatic
    30. .CursorLocation = adUseClient
    31. .LockType = adLockOptimistic
    32. .Open "Select * From tblEasy "    [b]<===== try this[/b]
    33. End With
    34.  
    35.  
    36. 'tests each record in the table
    37. Dim bMatch As Boolean
    38.  
    39. bMatch = False
    40. Do While bMatch = False
    41.  
    42. If (txtUser.Text = rstRecordSet!Username) And (txtPwd.Text = rstRecordSet!password) Then
    43.     bMatch = True
    44. Else
    45.     rstRecordSet.MoveNext
    46.     If rstRecordSet.EOF = True Then
    47.         Exit Do
    48.     End If
    49. End If
    50. Loop
    51.  
    52. If bMatch Then    [b]<====== There is no End If for this[/b]
    53.  
    54.         Dim sqlstring As String
    55.         Dim bMatch2 As Boolean
    56.        
    57.         Select Case rstRecordSet!Difficulty  [b]<===== Where is End Select?
    58.              If you only have one Case, why use this instead of an If/Then[/b]
    59.        
    60.         Case "Easy"
    61.             MsgBox "Your level is set to easy"
    62.            
    63.             bMatch2 = False
    64.                 Do While bMatch2 = False
    65.                
    66.                 If (txtUser.Text = rstRecordSet2!Username) Then
    67.                     bMatch2 = True
    68.                 Else
    69.                     rstRecordSet2.MoveNext
    70.                
    71.                     If rstRecordSet2.EOF = True Then
    72.                      Exit Do
    73.                     End If
    74.                 End If
    75.                 Loop
    76.            
    77.             If bMatch2 Then 'the record matches
    78.                 MsgBox "Already in tblEasy"
    79.             Else
    80.                 sqlstring = "INSERT INTO tbleasy (Username) VALUES ('" & txtUser.Text & "')"
    81.                 conConnection.Execute sqlstring, , adCmdText + adExecuteNoRecords
    82.                 MsgBox "Just added"
    83.             End If
    84.  
    85. End Sub

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    66

    Re: Changing the recordset

    Sorry...I do have other cases, they're the same as the first one but for 'Medium' and 'Hard'

    From 'End Select' I have...


    End Select

    Else: MsgBox "Sorry, your details are invalid, please try again"

    End If


    conConnection.Close

    Set conConnection = Nothing
    Set cmdCommand = Nothing
    Set rstRecordSet = Nothing


    End Sub

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Changing the recordset

    Quote Originally Posted by Piglet
    Sorry...I do have other cases, they're the same as the first one but for 'Medium' and 'Hard'

    From 'End Select' I have...


    End Select

    Else: MsgBox "Sorry, your details are invalid, please try again"

    End If


    conConnection.Close

    Set conConnection = Nothing
    Set cmdCommand = Nothing
    Set rstRecordSet = Nothing


    End Sub
    I kind of figured but I wanted to make sure. Anyway, if I were doing this I would differ from what you have by just a bit.
    VB Code:
    1. 'this is wrong
    2. Dim rstRecordSet As New ADODB.Recordset
    3. Dim rstRecordSet2 As New ADODB.Recordset
    4. 'should be
    5. Dim rstRecordSet As ADODB.Recordset
    6. Dim rstRecordSet2 As ADODB.Recordset
    7. Set rstRecordSet = New ADODB.Recordset
    8. Set rstRecordSet2 = New ADODB.Recordset
    9. 'then I would do
    10. Dim sSQLReg As String
    11. Dim sSQLEasy As String
    12. sSQLReg = "SELECT * FROM tblRegister"
    13. rstRecordSet.Open sSQLReg, conConnection
    14. 'now I have all of tblRegister in one recordset
    15. sSQLEasy = "SELECT * FROM tblEasy "
    16. rstRecordSet2.Open sSQLEasy, conConnection
    17. 'now I have all of tblEasy in another recordset

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