A Little Help With My IF Statements Please

the-m0th

Registered User.
Local time
Today, 22:42
Joined
Apr 14, 2015
Messages
51
Hi,

I've recently had to build a survey in access that people on the phones downstairs will be using. I'm trying to place if statements on the buttons that navigate between tab pages to make sure that questions are not missed out and are filled in correctly. My problem is that as i've got it now, if the page has more than one question on it runs every if statement. I'd like it to end the sub routine on failing an if statement. ie if the first question has been missed or filled in incorrectly, it pops up the message box and doesn't bother checking the rest.

my first attempt went like this:

Code:
If Me.Text82.Value > "" Then Me.Q3.SetFocus Else MsgBox "You Must Enter A Date Of Birth!"
If Me.Combo77.Value = "" Then MsgBox "Does The Date Of Birth Match The Data?"
If Me.Combo77.Value = "No" And Me.Text86.Value = "" Then MsgBox "If The Data Doesn't Match, We Require The Date Of Birth"
Me.Q3.SetFocus
End Sub

then my second attempt:

Code:
If Me.Text82.Value = "" Then
    MsgBox "You Must Enter A Date Of Birth!"
    End Sub
End If
If Me.Combo77.Value = "" Then
    MsgBox "Does The Date Of Birth Match The Data?"
    End Sub
End If
If Me.Combo77.Value = "no" And Me.Text86.Value = "" Then
    MsgBox "If The Date Of Birth Does Not Match, We Require An Actual Age!"
    End Sub
End If
Me.Q3.SetFocus
End Sub

It's still not working as I'd like it to so I'm turning to you guys for help. Any assistance you can give me will be very gratefully received.

Thanks
Wayne
 
You should use meaningful names for your controls. Text82,combo77 have no meaning to anyone but you, and you will forget/confuse it with other text boxes soon enough.

Research the IsNull() function.

If you want to continue processing after checking the status of "Date of Birth", you have to have logic to do so.

eg.
Code:
     If tbBirthDate = ""  OR IsNull(tbBirthDate) Then
       MsgBox "You must enter a Date of Birth "
     Else
       'txBirthDate contains something
       'You could check here for a valid Date....IsDate()
       ' and you could decide on its appropriateness (value > 15???
     end if
' Next check

In your current code you are going/exiting the sub after each check, so the logic does not have it checking all boxes.

You do not need the .value since that is the default property.

Good luck.
 
thanks jdraw, i usually do have meaningful names for everything but this database had to be built in the space of about an hour as it was a job in progress when our server decided to blow itself up.

i've managed to get my second batch of code working by changing all of the "end sub"s to "exit subs" apart from the last one, but i will be trying the sample you provided as it looks better than mine :)

thanks again,
Wayne
 
As I see it

End Sub is a compiler/interpreter command. This is a statement to show the end of the procedure/subfunction. Just as Sub XYZ is the start of the procedure/subfunction.

Exit Sub is flow command. When reaching this statement during processing, exit the procedure/subfunction.
Good luck.
 

Users who are viewing this thread

Back
Top Bottom