Msgbox YESNO trouble

L

lcox400w

I am having trouble with a VByes no msg box. No matter if I pick yes or no,
it always goes to the "no" option and runs the "else" porton of the code. I
cant for the life of me figure out why. The code is suppose to check to see
if DRNo is null. If so it asks the user if they want to close the form or
not. Selecting yes would close the form, selecting no then keeps the focus
on the DRNo field so they can fill out the field. I'm confused as to why it
keeps going to the else portion of the statement and ignores the VBYES code...

Dim DRResponse

If IsNull(DRNo) Or DRNo = "" Then
If DRResponse = MsgBox("You did not put a DR number in. A DR is
required. Do you want to close the form and lose any data you have
entered?", vbYesNo) = vbYes Then
On Error Resume Next
DoCmd.Close
Exit Sub
Else
DRNo.SetFocus
Cancel = True
Exit Sub
End If

Else
End If
 
B

boblarson

You have that slightly configured wrong:



If IsNull(DRNo) Or DRNo = "" Then
If MsgBox("You did not put a DR number in. A DR is
required. Do you want to close the form and lose any data you have
entered?", vbYesNo) = vbYes Then
Cancel = True
Me.Undo
DoCmd.Close acForm, Me.Name, acSaveNo
Exit Sub
Else
Cancel = True
DRNo.SetFocus
End If
End If

Also, you don't need to put an Else in if there is no Else. You don't need
to assign the message box value to a variable, you need to issue a cancel in
both conditions and to close out you need to undo the form values before
closing. I use explicit closing code too as it can have unintended
consequences if you don't.
--
Bob Larson

Free Tutorials and Samples at http://www.btabdevelopment.com

__________________________________
 
L

lcox400w

Thanks for the clarification re using the variable within the message box.

I am however running into a problem which is when the code tries to close
the form it gets a runtime error 2585 which says "This action can not be
carried out while processing a form or report event"

This is what I kept running into so I forced the close by using on error
resume next which then allowed the form to close.
 
L

lcox400w

Its in the on exit event. I have 2 things I check for. Here is the complete
on exit event:

Private Sub DRNO_Exit(Cancel As Integer)
'checks to ensure this mandatory field is completed and gives appropriate
error message

If IsNull(DRNo) Or DRNo = "" Then
If MsgBox("You did not put a DR number in. A DR is required. Do you want
to close the form and lose any data you have entered?", vbYesNo) = vbYes Then
Cancel = True
Me.Undo
DoCmd.Close acForm, Me.Name, acSaveNo
Exit Sub
Else
Cancel = True
DRNo.SetFocus
Exit Sub
End If
End If

If Len(DRNo) < 9 Then
MsgBox "DR number must be 9 digits. If this is a CR number, you must
add extra zero's to make the CR number 9 digits", vbOKOnly, "DR number too
short"
DRNo.SetFocus

Else
Cancel = False
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