Results 1 to 10 of 10
  1. #1
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66

    Exclamation Compare Data in a Recordset from User Input

    I am trying to compare data from a user input to a recordset. If the data is found in the recordset, open a form, if it isnt found, open a different form. If the user inputs no data and presses enter, display error saying invalid ID.



    Code:
    Private Sub Command12_Click()
    
    Dim rs As DAO.Recordset
    Dim txtID As Variant
    txtID = Forms![LoginForm2]![txtEmployeeID]
     
    Set rs = CurrentProject.Connection.Execute("SELECT * FROM DataTable WHERE EmployeeID IS NULL")
    If Len(txtID) > 0 Then
    While Not rs.EOF
    If rs = txtID Then
    DoCmd.OpenForm "frmMain", acNormal
    Else
    DoCmd.OpenForm "HourlyForm", acNormal
    End If
    Else
    MsgBox "Please enter a valid Associate ID"
    rs.MoveNext
    Wend
    Set rs = Nothing
    End If
    Exit Sub
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Well, this

    If rs = txtID Then

    would have to be:

    If rs!FieldName = txtID Then

    Where FieldName is the field you want to compare the input to. That said, looping the table is very inefficient. I'd add a criteria to the SQL and then you simply test for EOF (did the recordset return anything).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    Could you show me what you mean by adding criteria to the SQL? Im not fluent in VB. Here is my code right now.

    Code:
    Private Sub Command12_Click()
    
    Dim rs As DAO.Recordset
    Dim txtID As Variant
    txtID = Forms![LoginForm2]![txtEmployeeID]
     
    Set rs = CurrentProject.Connection.Execute("SELECT * FROM DataTable WHERE EmployeeID IS Text1.Text")
    If Len(txtID) > 0 Then
    While Not rs.EOF
    If rs!EmployeeID = txtID Then
    DoCmd.OpenForm "frmMain", acNormal
    Else
    DoCmd.OpenForm "HourlyForm", acNormal
    End If
    Else
    MsgBox "Please enter a valid Associate ID"
    rs.MoveNext
    Wend
    Set rs = Nothing
    End If
    Exit Sub
    End Sub

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    For starters, you're mixing ADO and DAO there. You appear to be looking for a specific employee, so along the lines of

    Set rs = CurrentDb.OpenRecordset("SELECT * FROM DataTable WHERE EmployeeID = " & txtID , dbOpenDynaset)

    Which assumes the EmployeeID field is numeric (data type). Then you just test to see if the recordset returned anything:

    Code:
    If rs.EOF Then 'no such person  
      DoCmd.OpenForm "HourlyForm", acNormal
    Else
      DoCmd.OpenForm "frmMain", acNormal
    End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    I am getting an debug error on the set rs = line of the code

    Code:
    Private Sub Command12_Click()
    
    Dim rs As DAO.Recordset
    Dim txtID As Variant
    txtID = Forms![LoginForm2]![txtEmployeeID]
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM DataTable WHERE EmployeeID = " & txtID, dbOpenDynaset)
    If Len(txtID) > 0 Then
    If rs.EOF Then 'no such person
      DoCmd.OpenForm "HourlyForm", acNormal
    Else
      DoCmd.OpenForm "frmMain", acNormal
    End If
    Else
    MsgBox "Please enter a valid Associate ID"
    End If
    Exit Sub
    End Sub

  6. #6
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    I am getting this error:

    Syntax error (missing operator) in query expression 'EmployeeID = '

  7. #7
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    If i dont enter a number, it displays the error above. If I do enter a number, it displays the error data type mismatch in criteria expression

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    The first error implies the variable has no value. Make sure something has been entered in the textbox. The second that the field is text, not numeric; if so:


    Set rs = CurrentDb.OpenRecordset("SELECT * FROM DataTable WHERE EmployeeID = '" & txtID & "'", dbOpenDynaset)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    hammer187 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Aug 2012
    Posts
    66
    Wow, stupid syntax. That works. Thanks. I will mark this as solved. I am also having trouble with something else concerning this database, but I'm going to work on it for a couple of days and then if I can't get it, I will post on here.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-06-2012, 01:20 AM
  2. Replies: 4
    Last Post: 11-10-2011, 03:41 PM
  3. How to take a user input
    By iamraja5 in forum Access
    Replies: 1
    Last Post: 05-12-2011, 10:17 AM
  4. Replies: 9
    Last Post: 10-01-2010, 05:50 PM
  5. Replies: 1
    Last Post: 11-13-2009, 03:03 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums