vbYesNoCancel with if_then_else statements

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having a proble with the syntax below using vbYesNoCancel, kindly help
me with the solution

Private Sub cmdAdd_Click()

If MsgBox("To add records, click YES; To edit records, click NO",
vbYesNoCancel, "Test Database") = vbYes Then
txtIndex.Locked = False
Else
txtIndex.Locked = True
End if
End sub
 
Alylia said:
I am having a proble with the syntax below using vbYesNoCancel,
kindly help me with the solution

Private Sub cmdAdd_Click()

If MsgBox("To add records, click YES; To edit records, click NO",
vbYesNoCancel, "Test Database") = vbYes Then
txtIndex.Locked = False
Else
txtIndex.Locked = True
End if
End sub

I don't see anything wrong with the syntax, unless the original is
really broken onto two lines without a line-continuation character. If
that's the problem, do it like this:

If MsgBox( _
"To add records, click YES; To edit records, click NO", _
vbYesNoCancel, _
"Test Database") _
= vbYes
Then

But aside from that line break, which may just be an artifact of the
newsreader, the code looks fine. What problem do you find?
 
thanks for your prompt response. but that is not the problem. If i click on
the cancel button, it acts on the no condition.
 
Alylia said:
thanks for your prompt response. but that is not the problem. If i
click on the cancel button, it acts on the no condition.

You didn't ask it to distinguish between Yes, No, and Cancel. Although
you presented all three options as choices, your code only tests for
Yes, vs. "not Yes". You'd need to do something like this:

Select Case MsgBox( _
"To add records, click YES; To edit records, click NO",
_
vbYesNoCancel, _
"Test Database")

Case vbYes
txtIndex.Locked = False

Case vbNo
txtIndex.Locked = True

Case vbCancel
' ... do something if Cancel was chosen ...

End Select
 
Back
Top