• Basic If..Then Question (Access 2K)

    Author
    Topic
    #382465

    I am trying to create an On Click event in VBA which will compare two unbound controls on my form and then open another form if the two controls match. For some reason I am running into problems although I know this has an easy solution. Current code is as follows:

    Private Sub cmdEnter_Click()

    If Me.txtInput = Me.txtverify Then
    DoCmd.Close
    DoCmd.OpenForm “MainMenu”
    Else
    MsgBox (“Error, please re-submit”)
    End If

    End Sub

    Thanks in advance for any assistance.

    Viewing 0 reply threads
    Author
    Replies
    • #648551

      Your code looks OK, but you didn’t tell us what problems you are running into. Do you get an error message? Do you get a different result from what you expected?

      • #648557

        Sorry, thought I had an obvious error in my code.

        When the two controls on my form match (both are text) it should open the form. When I enter text into the first field and it matches the second, It should open the form, but I get the message box instead. No errors, it just skips to the Else even though the text controls match.

        • #648561

          I created a test form with text boxes txtInput and txtVerify and a command button cmdEnter, and pasted your code into the OnClick event procedure of the command button. If I leave both text boxes empty, or enter different texts in them, I get the error message, but if I enter the same text in both, the form closes.

          Try putting a breakpoint in the code – click in the margin bar to the left of “Private Sub cmdEnter_Click()”.
          Now, when you open the form, enter identical text in the text boxes and click the button, code execution will pause in the line with the breakpoint. If you hover the mouse over Me.txtInput and Me.txtVerify in the code, you should see their current value. You can step through the code using F8. If the texts are identical but the code still goes to the Else part, there is something strange going on.

          • #648565

            I will try this shortly.

            Would it make a difference if the value for txtverify was already input when the form opened based on another control? The way the form was created, the txtverify control is not visible and not typed in by the user, but the txtInput control is entered by the user.

            • #648572

              It shouldn’t matter whether txtVerify is visible or whether it has been filled in by code. But you might make it visible temporarily for testing purposes.

              One other thought: if the text in txtVerify is entered using code, it might contain trailing spaces. If you enter a text with trailing spaces interactively, Access trims those. So if txtVerify contains “Marc ” and you type “Marc ” into txtInput, the latter becomes “Marc” the moment you exit txtInput, so the text boxes are not equal any more! If this is a possible explanation for your problem, you might use Trim:

              If Trim(txtInput) = Trim(txtVerify) Then

            • #648580

              Thanks for the help. I will try both suggestions tonight after work.

            • #648675

              Quick question. What type of data is going into these boxes. You may have the format set differently between the two.

              Just a thought.

    Viewing 0 reply threads
    Reply To: Basic If..Then Question (Access 2K)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: