• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1268
  • 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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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