• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1170
  • Last Modified:

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
0
dbfromnewjersey
Asked:
dbfromnewjersey
  • 3
  • 2
  • 2
2 Solutions
 
mbizupCommented:
Use a variable.

StrResponse= msgbox (....


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

If lresponse = 1 then
....
End if
0
 
dbfromnewjerseyAuthor Commented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
OCDanCommented:
Just below the first if remove the msgbox code,  strresponse =msgbox is the only one you need there
0
 
mbizupCommented:
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

0
 
mbizupCommented:
As a side note, that Else condition also covers cases where the user simply closes the message box with the "X" buttton, without explicitly clicking the No button.

In the rare case that you really need to discern a "No" response from simply hitting the X button, you would need a special case for No:


Dim intResponse As integer

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

If intResponse = vbYes Then
    MsgBox "testing yes"
Elseif intResponse = vbNo then
    MsgBox "testing no"
Else
     MsgBox "testing X button"
 End If

Open in new window

0
 
dbfromnewjerseyAuthor Commented:
Thank you.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now