View Full Version : Button works every other time???


Hassan
10-02-2001, 08:26 AM
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

jwindon
10-02-2001, 09:11 AM
Try putting in Cancel = True under your Else If

Hassan
10-02-2001, 09:22 AM
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 :-)

Fornatian
10-02-2001, 10:17 AM
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

Hassan
10-02-2001, 10:28 AM
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?

jwindon
10-02-2001, 10:38 AM
Still learning myself too.

Hassan
10-02-2001, 10:42 AM
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!