Msgbox triggering an event

commandolomo

Registered User.
Local time
Today, 18:42
Joined
Jun 11, 2004
Messages
73
Hello one and all

It may seem a rather simple question, though it continues to confound me -and I have searched to no avail :D

How do you trigger an event depending on what button is pressed on a msgbox?

For example, using a vbYesNo msgbox on a form, how can I assign events to either the Yes or the No option? Say, if "Yes" is clicked the focus would be set to a control on the form, and if "No" is clicked access would close down.

Any ideas?

I've currently got on the on click event of a command button (the bit in italics is fine, the bit in bold is not working!):-

Private Sub Command3_Click()
On Error GoTo Err_Command3_Click

Dim stDocName As String


MsgBox "Please select where you wish to save the exported file", vbOKOnly, "Save To Location"

stDocName = Month.Value
DoCmd.OutputTo acTable, stDocName, acFormatXLS


MsgBox "Do you want to select another History Extract?", vbYesNo, "Do you wish to continue?"

If intresponse = vbNo Then
DoCmd.Close
Else
Month.SetFocus
End If




Exit_Command3_Click:
Exit Sub

Err_Command3_Click:
MsgBox Err.Description
Resume Exit_Command3_Click

End Sub




But whether Yes or No is selected, the focus returns to the current form.

Cheers for any ideas
 
I had the same problem once, and below is an example of how I fixed the problem:

Code:
Dim X

X = MsgBox("There are  " & nrecords & "  occurences of this mpan in the database" & Chr(13) & "Would you like to filter by these records?", vbQuestion + vbYesNo)

    If X = vbYes Then
        [Forms]![Complaints].RecordSource = "filter_mpan"
    End If

Hope this makes sense and is of use to you.
 
Yuck. What's up with all the non essential coding?

Your problem is that your message box is not asking a question. The below code will do what you want. You have a field name "Month" and that is a reserved VBA key word. You need to use a naming convention. Search the forum if you do not know what that means. Your text box should be named something like txtMonth, not Month.

Code:
If MsgBox("Do you want to select another History Extract?", vbQuestion + vbYesNo, "Do you wish to continue?") = vbYes Then
    DoCmd.Close acForm, Me.Name
Else 'user clicked No
    Me.Month.SetFocus
End If
 
Thanks to the above, I have a working solution.

Out of interest, and a being a self taught vb novice, what code is non-essential? Looking at it, I thought that there was minimal code, and some well advised error handling?

Cheers for info, much appreicated.
 

Users who are viewing this thread

Back
Top Bottom