Link to home
Start Free TrialLog in
Avatar of Jenkins
JenkinsFlag for United States of America

asked on

vbYesNo MS Access

I'm having trouble with vbYesNo in Access.

I have a vbYesNo msgbox with a long message under a command button.
I am now trying to check whether the user selected Yes or No.

The problem I'm having is that when I click Yes or No when prompted by the message, the vbYesNo message box is being repeated.  Here is the code

'Here is the start of the code where I cause a vbYesNo message box to pop up
If .......some criteria....... Then
    MsgBox "bla bla bla....................... " & _
    "bla bla bla..................................... " & _
    "bla bla bla", vbYesNo
   
'Next, i am trying to check if YES was selected. This way of checking is causing the
'the vbYesNo message box to be repeated.  This is where the problem is.

    If MsgBox("MsgBox "bla bla bla....................... " & _                
    "bla bla bla..................................... " & _
    "bla bla bla", vbYesNo) = vbYes Then
   
'After clicking YES twice in the vbYesNo message box that pops up twice, I finally come to here. This where I want to come after clicking YES in the vbYesNo messagebox the first time.

    MsgBox "testing yes"
    Else
    MsgBox "testing no"
    End If
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Use a variable.

StrResponse= msgbox (....


And check the value of the variable instead of repeating the message box.
Dim lresponse as integer
Lresponse =msgbox(.....)

If lresponse = 1 then
....
End if
Avatar of Jenkins

ASKER

As usual I'm confused.  I tried to modify it and now it's repeating the vbYesNo messagebox 3 times before finally getting to the vbYes messagebox

This is what I have now:

Private Sub cmdButton_Click()
Dim StrResponse As String

If .......some criteria....... Then
    MsgBox "bla bla bla....................... " & _
    "bla bla bla..................................... " & _
    "bla bla bla", vbYesNo

StrResponse =  MsgBox("MsgBox "bla bla bla....................... " & _                
    "bla bla bla..................................... " & _
    "bla bla bla", vbYesNo)

If StrResponse = vbYes Then
    MsgBox "testing yes"
    Else
    MsgBox "testing no"
    End If
SOLUTION
Avatar of OCDan
OCDan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Like this:

Dim StrResponse As String

StrResponse =  MsgBox("MsgBox "bla bla bla....................... " & _                 
    "bla bla bla..................................... " & _
    "bla bla bla", vbYesNo) 

If StrResponse = vbYes Then
    MsgBox "testing yes"
    Else
    MsgBox "testing no"
 End If

Open in new window


That should work as is.  However, as OCDan's comment pointed out, the response from a message box is actually an integer so this would be better style:

Dim intResponse As integer

intResponse =  MsgBox("MsgBox "bla bla bla....................... " & _                 
    "bla bla bla..................................... " & _
    "bla bla bla", vbYesNo) 

If intResponse = vbYes Then
    MsgBox "testing yes"
    Else
    MsgBox "testing no"
 End If

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jenkins

ASKER

Thank you.