compare values entered into text boxes with fields in table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aannll
    New Member
    • Mar 2013
    • 8

    compare values entered into text boxes with fields in table

    hi:
    i want to compare the values that entered into text boxes with fields in table
    if it exists in the table i want to open page called (result)
    else open(voting)
    but when i run the program my code in two cases open the result page
    here is my code
    Code:
    If Me.f2.Value = DLookup("Passwords", _
          "Passwords", _
          "[Names]='" & Me.f1.Value & "'") Then
       '
       DoCmd.Close acForm, "Main form", acSaveNo
       DoCmd.OpenForm "result"
    ElseIf Me.f2.Value <> DLookup("Passwords", _
          "Passwords", _
          "[Names]='" & Me.f1.Value & "'") Then
       '
       DoCmd.Close acForm, "Main form", acSaveNo
       DoCmd.OpenForm "Voting"
    End If
    Last edited by zmbd; Mar 8 '13, 05:46 PM. Reason: [z{formatted the VBA for easier read}]
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    Please read Use a good thread title

    I think you have a slight misunderstandin g of how the If/Then/Else statement works. You don't need to test for the Me.f2 <> Dlookup... as that would be the same as the false of Me.f2 = Dlookup... so the Else portion of the code would run. Try the following
    Code:
    If Me.f2.Value = DLookup("Passwords", "Passwords", "[Names]='" & Me.f1.Value & "'") Then
        DoCmd.OpenForm "result"
    Else
        DoCmd.OpenForm "Voting"
    End If
    
    DoCmd.Close acForm, "Main form", acSaveNo

    Comment

    • aannll
      New Member
      • Mar 2013
      • 8

      #3
      thanks for your answer
      but also when i use this code
      in two cases it open the result page

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        Code:
        Dim strPassword as String
        strPassword = DLookup("Passwords", "Passwords", "[Names]='" & Me.f1.Value & "'")
        
        Debug.Print strPassword
        Debug.Print Me.f2
        
        Stop
        If Me.f2.Value = strPassword Then
            DoCmd.OpenForm "result"
        Else
            DoCmd.OpenForm "Voting"
        End If
         
        DoCmd.Close acForm, "Main form", acSaveNo
        Try the above code. Execution will stop on line 7. Check the values in the immediate window (Ctrl + G opens it). Now press F8 to step through the code one line at a time. Tell me if the values in the immediate window are equal or not and then which lines of code were executed going through the If/Then/Else statement.

        Comment

        • aannll
          New Member
          • Mar 2013
          • 8

          #5
          the values in the immediate window are equal
          and the lines 8 9 12 14 are executed

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            Those are the results that I would expect. Now try it with the wrong password. I assume this is where you are having the problem?

            Comment

            • aannll
              New Member
              • Mar 2013
              • 8

              #7
              the value not in the table but it appear equal in the immediate window and take the same steps to the result page

              Comment

              • Seth Schrock
                Recognized Expert Specialist
                • Dec 2010
                • 2965

                #8
                Normally I would ask what the values in the immediate window are, but since they are passwords, I'll let you do the checking.

                You type in a wrong password (X)
                The script runs and it gives you what the correct password is (Y) and what it sees in your password textbox (Z)

                Are X and Z equal, and Y and Z not equal?

                Comment

                Working...