Can't get proper response from MsgBox

G

Graham

I'm trying to control what happens after the user clicks on 'Yes' or 'No' but
without joy. Code is as follows
sMsg = "You are about to DELETE the Financial Record" _
& vbCrLf & " associated with this Contact." _
& vbCrLf & vbCrLf & "Do you wish to continue?"
bResponse = MsgBox(sMsg, vbYesNo + vbCritical)
MsgBox (bResponse)
' If bResponse = True _

If bResponse = vbYes _
Then
MsgBox "then"
sSQL = "DELETE tcFinancial.*, tcFinancial.ContactID " _
& "FROM tcFinancial " _
& "WHERE (((tcFinancial.ContactID)= " & fiContactID & "
));"
DoCmd.SetWarnings False
DoCmd.RunSQL sSQL
DoCmd.SetWarnings True
sfContacts2.Form.Requery
Else
MsgBox "else"
Cancel = True
Me.cbEnqID.Undo
End If

The value of bResponse for both Yes and No buttons is True so if I test for
that, it is always the Then condition that is processed, whereas if I test
for vbYes, it is always the Else that is processed.

Must be simple but it beats me.

Access 2002
 
T

TonyT

If you take out;
MsgBox (bResponse)
it works for me, although it looks a bit odd having messageboxes coming up
and saying 'then' or 'else' - I'm assuming they are areas of code to be
completed at a later date and for test purposes only.
Also the SQL code posted here is not constructed correctly with wordwrap
mucking it up, but I guess you've got that bit covered.

TonyT..
 
R

RoyVidar

Graham said:
I'm trying to control what happens after the user clicks on 'Yes' or
'No' but without joy. Code is as follows
sMsg = "You are about to DELETE the Financial Record" _
& vbCrLf & " associated with this Contact." _
& vbCrLf & vbCrLf & "Do you wish to continue?"
bResponse = MsgBox(sMsg, vbYesNo + vbCritical)
MsgBox (bResponse)
' If bResponse = True _

If bResponse = vbYes _
Then
MsgBox "then"
sSQL = "DELETE tcFinancial.*, tcFinancial.ContactID " _
& "FROM tcFinancial " _
& "WHERE (((tcFinancial.ContactID)= " &
fiContactID & " ));"
DoCmd.SetWarnings False
DoCmd.RunSQL sSQL
DoCmd.SetWarnings True
sfContacts2.Form.Requery
Else
MsgBox "else"
Cancel = True
Me.cbEnqID.Undo
End If

The value of bResponse for both Yes and No buttons is True so if I
test for that, it is always the Then condition that is processed,
whereas if I test for vbYes, it is always the Else that is
processed.

Must be simple but it beats me.

Access 2002

The return from the MsgBox function, is an Integer. You seem to
assign it to a Boolean, where all numbers except 0, will be
evaluated as false. Try using integer. Air Code

dim intAnswer as integer
intAnswer = MsgBox("Select something", vbYesNoCancel)
Select Case intAnswer
Case vbYes
MsgBox "Yes"
Case vbNo
MsgBox "No"
Case vbCancel
MsgBox "Cancel"
Case Else
MsgBox "Dunno..."
End Select
 
G

Graham

Thanks Tony & Roy

Changing the Response variable to an integer fixed the problem - I can now
remove the debug code and proceed with getting the rest of it to work.

It seemed logical to me that the variable should be boolean - no doubt it is
obvious that VBA is not my first language.

Thanks again for a great forum.
 

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