Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Apr 2002
    Location
    Arkansas, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic If..Then Question (Access 2K)

    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.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Basic If..Then Question (Access 2K)

    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?

  3. #3
    2 Star Lounger
    Join Date
    Apr 2002
    Location
    Arkansas, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Basic If..Then Question (Access 2K)

    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.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Basic If..Then Question (Access 2K)

    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.

  5. #5
    2 Star Lounger
    Join Date
    Apr 2002
    Location
    Arkansas, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Basic If..Then Question (Access 2K)

    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.

  6. #6
    2 Star Lounger
    Join Date
    Apr 2002
    Location
    Arkansas, USA
    Posts
    163
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Basic If..Then Question (Access 2K)

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

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Basic If..Then Question (Access 2K)

    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

  8. #8
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Basic If..Then Question (Access 2K)

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

    Just a thought.

Posting Permissions

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