Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help I'm getting 2 MsgBoxes

Status
Not open for further replies.

millrat

Technical User
Dec 23, 2003
98
US
Hi,
Can anybody help me with this code. 2 checkboxes on form but can only select 1. Below is code for msgboxes everything works how I want but I get 2 msgboxes instead of 1.
Thanks
millrat

Private Sub Check339_BeforeUpdate(Cancel As Integer)
Dim StrMsg As String
StrMsg = "You can't select both checkboxes! Click OK to Cancel"
If Me.Check351.Value = True Then
MsgBox StrMsg, vbOKOnly, "Dual Selection"
If MsgBox(StrMsg, vbOKOnly, "Dual Selection") = vbOK Then
DoCmd.CancelEvent
Else
End If
End If
End Sub


Private Sub Check351_BeforeUpdate(Cancel As Integer)
Dim StrMsg As String
StrMsg = "You can't select two checkboxes! Click OK to Cancel"
If Me.Check339.Value = True Then
MsgBox StrMsg, vbOKOnly, "Dual Selection"
If MsgBox(StrMsg, vbOKOnly, "Dual Selection") = vbOK Then
DoCmd.CancelEvent
Else
End If
End If
End Sub
 
To walk you through the situation (I'll use Check351_BeforeUpdate as the sub in this scenario):

1) set up a string
2) if a check box is checked, display a message.
3) display a message and get value of user selection.

Looking at your subroutine, we can clean up the code a little bit. Try doing something like as follows:

Code:
Private Sub Check351_BeforeUpdate(Cancel As Integer)
   ' verify other check box not selected
   If Me.Check339.Value = True Then
      MsgBox "Cannot pick 2 both checkboxes!", vbOKOnly, "Dual Selection"
      ' cancel updating the checkbox
      docmd.cancelevent = true
   End If
End Sub

Or, alternatively, since the users are only allowed to check one box, you can use this code:

Code:
Private Sub Check351_BeforeUpdate(Cancel As Integer)
   If Me.Check339.Value = True Then
     ' check current value of this checkbox
     me.check339.value = false
   End If
End Sub

Or, even better and easier to implement: use an option group with radio buttons. Then the user is only permitted to pick one item, and you don't have to worry about validation code or any of the headaches that go along with it.

HTH



Greg Tammi, IT Design & Consultation
Work: Home:
 
How are ya millrat . . . . .
Code:
[blue]MsgBox StrMsg, vbOKOnly, "Dual Selection"
If MsgBox(StrMsg, vbOKOnly, "Dual Selection") = vbOK Then[/blue]
Here your calling the MsgBox function twice.

[blue]MsgBox StrMsg, vbOKOnly, "Dual Selection"[/blue] is the same as
[purple]Call MsgBox StrMsg, vbOKOnly, "Dual Selection"[/purple]. This is the method for calling a function without using the return value. So jst remove it!

Calvin.gif
See Ya! . . . . . .
 
Thanks guys
Problem solved using below.
Note: removed "= true" from "docmd.cancelevent" for it to work.

Private Sub Check351_BeforeUpdate(Cancel As Integer)
' verify other check box not selected
If Me.Check339.Value = True Then
MsgBox "Cannot pick 2 both checkboxes!", vbOKOnly, "Dual Selection"
' cancel updating the checkbox
docmd.cancelevent
End If
End Sub
millrat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top