Else if statement

  • Thread starter injanib via AccessMonster.com
  • Start date
I

injanib via AccessMonster.com

In the procedure below I keep getting error saying "Else without if" and the
debug points to the "Else" statement I have underlined.

If I remove the first two "if" statements which is for the Null fields, then
the problem goes away.
I can't figure out what I need to add or remove.



Private Sub cmdModify_Click()
Dim intResponse As Integer
If Me.cmdModify.Caption = "Save" Then
If IsNull(Me.Contact) Then
MsgBox ("Contact name is required."), vbCritical + vbOKOnly
Me.Contact.SetFocus
Else
If IsNull(Me.Email) Then
MsgBox ("Email address is required."), vbCritical + vbOKOnly
Me.Email.SetFocus
Else
If Me.NewRecord Then
intResponse = MsgBox("Are you sure you want to save this new
customer?", vbQuestion + vbYesNo, "Save Record?")
Else
intResponse = MsgBox("Are you sure you want to save the changes?",
vbQuestion + vbYesNo, "Save Changes?")
End If
If intResponse = vbNo Then
Me.Undo
Else
Me.Dirty = False
Me.cmdNext.Enabled = True
Me.Contact.Locked = True
Me.Company.Locked = True
Me.Cell.Locked = True
Me.Email.Locked = True
Me.Address.Locked = True
Me.cmdDelete.Enabled = False
Me.cmdModify.Caption = "Modify"
End If
Else
------
If Me.cmdModify.Caption = "Modify" Then
Me.Contact.Locked = False
Me.Company.Locked = False
Me.Cell.Locked = False
Me.Email.Locked = False
Me.Address.Locked = False
Me.cmdModify.Caption = "Save"
Me.cmdDelete.Enabled = True
End If
End If
End Sub
 
K

kc-mass

Check your code. You have 5 IF statements and only 4 END IF statements

Regards

Kevin
 
D

Dale_Fye via AccessMonster.com

I ALWAYS indent the code that is between the IF, ElseIf and EndIf lines.

In your case, I think you inserted a couple of carraige returns where they do
not belong. ELSEIF should all be on one line.

Additionally, I think there is a flaw in your logic. The way you currently
have it working, if me.Contact or me.Email is blank you indicate the field is
required, but don't ask for a response. Then, when you exit that IF
statement, you check for intResponse = vbNo, but since you have not set
intResponse to vbNo, it will bypass that test and save the record.

I think you want:

Private Sub cmdModify_Click()

Dim strMsg as String
Dim intResponse As Integer

If Me.cmdModify.Caption = "Save" Then

If IsNull(Me.Contact) Then
MsgBox ("Contact name is required."), vbCritical + vbOKOnly
Me.Contact.SetFocus
intResponse = vbNo
ElseIf IsNull(Me.Email) Then
MsgBox ("Email address is required."), vbCritical + vbOKOnly
Me.Email.SetFocus
intResponse = vbNo
ElseIf Me.NewRecord Then
strMsg = "Are you sure you want to save this new customer?"
intResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?
")
Else
strMsg = "Are you sure you want to save the changes?",
intResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Changes?
")
End If

If intResponse = vbNo Then
Me.Undo
Else
Me.Dirty = False
Me.cmdNext.Enabled = True
Me.Contact.Locked = True
Me.Company.Locked = True
Me.Cell.Locked = True
Me.Email.Locked = True
Me.Address.Locked = True
Me.cmdDelete.Enabled = False
Me.cmdModify.Caption = "Modify"
End If

ElseIf Me.cmdModify.Caption = "Modify" Then
Me.Contact.Locked = False
Me.Company.Locked = False
Me.Cell.Locked = False
Me.Email.Locked = False
Me.Address.Locked = False
Me.cmdModify.Caption = "Save"
Me.cmdDelete.Enabled = True
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