Button works every other time???

Hassan

Registered User.
Local time
Today, 14:02
Joined
Feb 24, 2000
Messages
15
I placed a button on my form with this code in OnClick event. When I click it, message box appears. But when I click "Yes" button in this msg box, it works every other time. Why? What I am doing wrong?

Private Sub Command22_Click()
Dim Msg, Style, Title, Response

Msg = "This is my message"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "This is my title"

Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
docmd.OpenQuery "Query1"
docmd.OpenQuery "Query2"
Me.Requery

Else
End If
End Sub
 
Try putting in Cancel = True under your Else If
 
Jwindon, you mean something like this?

Private Sub Command22_Click()
Dim Msg, Style, Title, Response

Msg = "This is my message"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "This is my title"

Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
docmd.OpenQuery "Query1"
docmd.OpenQuery "Query2"

Me.Requery

Else
End If
Cancel = True
End Sub

Well it breaks with error "Variable not defined" pointing to that Cancel
Probably I am doing something stupid, but I am really just learning :-)
 
try:

Private Sub Command22_Click()
'don't leave variable types undeclared because they will default to variants and these use much more memory.

Dim Msg as String, Style as Byte, Title as String

Msg = "This is my message"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "This is my title"

If MsgBox(Msg, Style, Title)= vbYes Then
docmd.OpenQuery "Query1"
docmd.OpenQuery "Query2"
Me.Requery
End If
End Sub


JW: Cancel = True will not work with the button click because they don't have a cancel argument.

This is basically the same code as you already had but I can't see any obvious flaws, does it work OK if you run it from another form?

Ian
 
Well I did thoes changes but problem is still there. Nothing has changed... :-(
Maybe you can look at this database and see if there is something wrong?
 
Thanks God! I found it! I forgot to place Me.Refresh as the first action run after clicking that button. Otherwise that record I was trying to append to new table and then delete from origin table wasn't saved. Anyway now it works fine!

Anyway, thank you guys!
 

Users who are viewing this thread

Back
Top Bottom