Help with IF Statement

D

David

My question is highlighted in the code below.



Private Sub NextCmd_Click()
Dim stDocName As String
Dim stLinkCriteria As String


If IsNull(SecSSN) = True Then
MsgBox "Please Enter Student ID"
SecSSN.SetFocus
Cancel = -1

ElseIf IsNull(Fname) = True Then
MsgBox "Please Enter Your First Name"
Fname.SetFocus
Cancel = -1

ElseIf IsNull(Lname) = True Then
MsgBox "Please Enter Your Last Name"
Lname.SetFocus
Cancel = -1

ElseIf IsNull(TOS) = True Then
MsgBox "Please Select Service"
TOS.SetFocus
Cancel = -1



ElseIf (Course) = "" Then
MsgBox "Please Enter Course"
Course.SetFocus
Cancel = -1

ElseIf (TOS) = "Avid Program" Then
ElseIf IsNull(HighSchool) = True Then
MsgBox "Please Select High School"
HighSchool.SetFocus
Cancel = -1


ElseIf (TOS) = "Avid Program" Then
ElseIf IsNull(CompMath) = True Then
MsgBox "Please Select Completed Math Course"
CompMath.SetFocus
Cancel = -1
______________________________________________________________
'Question: where do I put the End if's. If I put them here a Msgbox for
Compmath and Highschool will appear if null, but it will not go to the else
statement where the close is.
________________________________________________________________
Else


DoCmd.Close
stDocName = "WelcomeFrm"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
_____________________________________________________________
'Question 2 if I put them here Highschool and Compmath work fine after the
messagebox, but if course is empty and not "Avid Program" then a msgbox
appears, but does not go to the else statement for closing.
______________________________________________________________
End Sub

Any help would be great
Thank you
David
 
J

Jeff L

I would clean this up and use one message box. Since I am not sure
where you are doing this, on a form or in a module, you may need Me. in
front of your names. Me.Fname for example.

Private Sub NextCmd_Click()
Dim stDocName As String
Dim stLinkCriteria As String, CanClose as Boolean, Message as string, I
as Integer

CanClose = True
Message = "Please Enter:" & vbcrlf
I = 1

If IsNull(SecSSN) = True Then
Message = Message & I & ") StudentID" & vbcrlf
CanClose = False
I = I + 1
End If

If IsNull(Fname) = True Then
Message = Message & I & ") Your First Name" & vbcrlf
CanClose = False
I = I + 1
End If

If IsNull(Lname) = True Then
Message = Message & I & ") Your Last Name" & vbcrlf
CanClose = False
I = I + 1
End If

If IsNull(TOS) = True Then
Message = Message & I & ") Service" & vbcrlf
CanClose = False
I = I + 1
End If

If Course = "" Then
Message = Message & I & ") Course" & vbcrlf
CanClose = False
I = I + 1
End If

If TOS = "Avid Program" And IsNull(HighSchool) Then
Message = Message & I & ") High School" & vbcrlf
CanClose = False
I = I + 1
End If

If TOS = "Avid Program" And IsNull(CompMath) Then
Message = Message & I & ") Completed Math Course"
CanClose = False
I = I + 1
End If

If CanClose = False then
Msgbox Message
Cancel = -1
Else
DoCmd.Close
stDocName = "WelcomeFrm"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End IF

Hope that helps!
 
K

Klatuu

For the most part, all the ElseIfs are not needed. Each control check stands
alone except for TOS. The actual problem is you have one ElseIf where an
If/End If is needed. See my rewrite below, I think it reads a little better
and will get you want you need. One other point - It make the code easier to
read when you use constants rather than values. For -1 use True. Also, alway
qualify your objects.

ElseIf (TOS) = "Avid Program" Then
ElseIf IsNull(HighSchool) = True Then
MsgBox "Please Select High School"
HighSchool.SetFocus
Cancel = -1

Rewrite Starts Here

Private Sub NextCmd_Click()
Dim stDocName As String
Dim stLinkCriteria As String

'Check Student ID
If IsNull(Me.SecSSN) Then
MsgBox "Please Enter Student ID"
Me.SecSSN.SetFocus
Cancel = True
Exit Sub
End If

'Check First Name
If IsNull(Me.Fname) Then
MsgBox "Please Enter Your First Name"
Me.Fname.SetFocus
Cancel = True
Exit Sub
End If

'Check Last Name
If IsNull(Me.Lname) Then
MsgBox "Please Enter Your Last Name"
Me.Lname.SetFocus
Cancel = True
Exit Sub
End If

'Check Course
If Me.Course = "" Then '*****Are you sure, why would this not be Null????
MsgBox "Please Enter Course"
Me.Course.SetFocus
Cancel = True
Exit Sub
End If

'Check Service
If IsNull(Me.TOS) Then
MsgBox "Please Select Service"
Me.TOS.SetFocus
Cancel = True
Exit Sub
End If

If (Me.TOS) = "Avid Program" Then
If IsNull(Me.HighSchool) Then
MsgBox "Please Select High School"
Me.HighSchool.SetFocus
Cancel = True
Exit Sub
Else
If IsNull(Me.CompMath) Then
MsgBox "Please Select Completed Math Course"
Me.CompMath.SetFocus
Cancel = True
Exit Sub
End If
End If

DoCmd.Close
stDocName = "WelcomeFrm"
?????????What stLinkCriteria - I don't see it in your code
DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub
 
D

Douglas J. Steele

You don't really need the CanClose variable. Simply check whether a message
exists.

I'm not sure what your Cancel = -1 is supposed to do: there is no Cancel
parameter on a Click event.

Private Sub NextCmd_Click()
Dim stDocName As String
Dim stLinkCriteria As String, Message as string
Dim I as Integer

I = 1

If IsNull(SecSSN) = True Then
Message = Message & I & ") StudentID" & vbcrlf
I = I + 1
End If

If IsNull(Fname) = True Then
Message = Message & I & ") Your First Name" & vbcrlf
I = I + 1
End If

If IsNull(Lname) = True Then
Message = Message & I & ") Your Last Name" & vbcrlf
I = I + 1
End If

If IsNull(TOS) = True Then
Message = Message & I & ") Service" & vbcrlf
I = I + 1
End If

If Course = "" Then
Message = Message & I & ") Course" & vbcrlf
I = I + 1
End If

If TOS = "Avid Program" And IsNull(HighSchool) Then
Message = Message & I & ") High School" & vbcrlf
I = I + 1
End If

If TOS = "Avid Program" And IsNull(CompMath) Then
Message = Message & I & ") Completed Math Course"
I = I + 1
End If

If Len(Message) > 0 Then
Msgbox "Please Enter:" & vbcrlf & Message
' Cancel = -1 <<< ???
Else
DoCmd.Close
stDocName = "WelcomeFrm"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End IF

End Sub
 

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