if... elseif statement

G

Guest

I'm having an issue. I'm trying to make sure every question on a survey is
answered before the user goes to the next page. This is what my if...elseif
statement looks like:

Public Function ValidatePg1()
If IsNull(optTechCapabilities.Value) Then
MsgBox ("Please Answer Question 2")
ElseIf IsNull(optTechCurve.Value) Then
MsgBox ("Please Answer Question 3")
ElseIf IsNull(optEngtechSupport.Value) Then
MsgBox ("Please Answer Question 4")
Else
NextPage
End If
End Function

It will validate the first If, but once it is answered, it allows the user
to go to the next page instead of checking the other questions. Any ideas?

Thanks a bunch!
Seren
 
R

RobFMS

Try something like this:

Select Case True

Case IsNull(optTechCapabilities)
Msgbox Prompt:="Please answer question #2.")

Case IsNull(optTechCurve)
Msgbox Prompt:="Please answer question #3.")

Case IsNull(optEngtechSupport)
Msgbox Prompt:="Please answer question #4.")

Case Else
NextPage

End Select


In the "Select Case <condition>" ... we are indicating the condition as
TRUE. So this will walk through each of the case statements to determine
which is TRUE. The first occurrence of TRUE, it will display the appropriate
message. If none of the options are NULL, then the default case is
"NextPage".

Plug this in and give it a try. Let me know how it goes.

HTH

Rob Mastrostefano


--
FMS Professional Solutions Group
http://www.fmsinc.com/consulting

Software Tools for .NET, SQL Server, Visual Basic & Access
http://www.fmsinc.com

FMS Advanced Systems Group
http://www.fmsasg.com/
 
G

Guest

The Message Box will do what you want. Also, using an interger for a Yes/No
situation, to me, is not a good habit. I would use a Boolean variable.

Public Function ValidatePg1()
Dim blnNoAnswer as Boolean


If IsNull(optTechCapabilities.Value) Then
MsgBox ("Please Answer Question 2")
blnNoAnswer = True
end if
If IsNull(optTechCurve.Value) Then
MsgBox ("Please Answer Question 3")
blnNoAnswer = True
end if
If IsNull(optEngtechSupport.Value) Then
MsgBox ("Please Answer Question 4")
blnNoAnswer = True
end if

if blnNoAnswer Then
If MsgBox("What Do You Want To Do", vbQuestion + vbYesNo, _
"Answers Missing") = vbYes Then
'Do your code for if Yes is Clicked
Else
'Do your code for if No is Clicked
End If

End Function
 
G

Guest

Thank you! Very helpful. However, I just talked to the client... she
changed her mind. Now, she wants an alert letting the user know that all
questions were not answered on the page before going to the next page... with
an option to continue anyway. I've figured out the msgbox deal, but is there
an alert box that has yes/no buttons that will allow for different actions
for yes and no?
 
G

Guest

Use if for each check, without the else
Public Function ValidatePg1()
Dim I as Integer
I=0
If IsNull(optTechCapabilities.Value) Then
MsgBox ("Please Answer Question 2")
I = 1
end if
If IsNull(optTechCurve.Value) Then
MsgBox ("Please Answer Question 3")
I = 1
end if
If IsNull(optEngtechSupport.Value) Then
MsgBox ("Please Answer Question 4")
I = 1
end if
If I = 0 Then
NextPage
end if
End Function
 
G

Guest

This code will only catch the first unanswerd question. I believe the OP
wanted to check for all questions.

RobFMS said:
Try something like this:

Select Case True

Case IsNull(optTechCapabilities)
Msgbox Prompt:="Please answer question #2.")

Case IsNull(optTechCurve)
Msgbox Prompt:="Please answer question #3.")

Case IsNull(optEngtechSupport)
Msgbox Prompt:="Please answer question #4.")

Case Else
NextPage

End Select


In the "Select Case <condition>" ... we are indicating the condition as
TRUE. So this will walk through each of the case statements to determine
which is TRUE. The first occurrence of TRUE, it will display the appropriate
message. If none of the options are NULL, then the default case is
"NextPage".

Plug this in and give it a try. Let me know how it goes.

HTH

Rob Mastrostefano


--
FMS Professional Solutions Group
http://www.fmsinc.com/consulting

Software Tools for .NET, SQL Server, Visual Basic & Access
http://www.fmsinc.com

FMS Advanced Systems Group
http://www.fmsasg.com/
 
G

Guest

You are correct, but using your select case, the user will only be prompted
for the first missing answer.
 
R

RobFMS

Yes, this is true. We have found that multiple message boxes appearing (one
right after another) can.. or rather actually is, annoying to the end-user.

In any such case, it sounds like you have a good grasp on what you need to
resolve your particular issue.

Best Regards,
Rob Mastrostefano

--
FMS Professional Solutions Group
http://www.fmsinc.com/consulting

Software Tools for .NET, SQL Server, Visual Basic & Access
http://www.fmsinc.com

FMS Advanced Systems Group
http://www.fmsasg.com/
 
G

Guest

hi all,

i tried all of the above options and i agree with rob, it was better to have
one message at a time. and thanks a lot it worked great.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top