Full Version: repeated message box
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
Dani50
Hello,
I have a subform which is displayed in continuous form where edits and additions are not allowed but deletions are.
This subform's On_delete event is as follows:

Private Sub Form_Delete(Cancel As Integer)
Dim strText, strAnswer As String
strText = "Are you sure you want to delete '" & Me.FullText & "'?"
strAnswer = MsgBox(strText, vbYesNo)
If strAnswer = vbYes Then
Cancel = False
Else
Cancel = True
End If
End Sub

The problem (which is only an irritation) is that if the user chooses "no" the mesage box appears again before acting on the response. If they choose "yes" it doesn't. Changing which button has the focus makes no difference. Under 'confirm' on the 'edit/find' tab of 'options' I have "record changes" set to "off" so I cannot see that it is an automated message. Any suggestions as to the reason it does this twice and what I can do about it.

Dani.
TheForce
Not sure but have you tried a Me.Undo after Cancel = True?
Willem
Hi,

Hmm... odd, when I replicate your problem I do not get a second msgbox when I choose 'No'. When I choose 'Yes' I do get an extra msgbox which can be suppressed by using:

CODE
DoCmd.SetWarnings False


See attached for my db, do you get the same problem there?
Alan_G
Hi

In your Dim statement, you're only actually dimensioning strAnswer as a string (which funnily enough isn't correct anyway - it should be an integer). strText is being dimensioned as a Variant. To dimension multiple variables on the same line seperated by commas, you need to explicitly declare the variable type for each one.

I think I'd put the delete code behind a command button instead of the forms Delete event, but you could try

CODE
Private Sub Form_Delete(Cancel As Integer)

Dim strText As String, intAnswer As Integer

strText = "Are you sure you want to delete " & Chr(34) & Me.FullText & Chr(34) & "?"

intAnswer = MsgBox(strText, vbYesNo)

If intAnswer = vbNo Then

Cancel = True

End If

End Sub


***Untested
Dani50
Thanks a lot for all your replies I have tried them all and I am still getting the same problem. I wonder if it could be Vista?
Thanks for the tips Alan, I'll do as you suggest.
Originally I had a list box and a command button but when I changed to a a cts form as a subform I wasn't sure how to refer to the selected record. The things I tried didn't work, but I might have another go.
Dani.
TheForce
Try replicating the problem in a new db file. It would help to rule out file corruption.
JVanKirk
And on the note of corruption, have your tried a compact and repair?

J
Alan_G
Wouldn't have thought it would be anything to do with Vista. You can still get the selected record to delete even though it's now a subform and not a listbox. I'd definitely go with the command button. If, for example, your command button was on your main form then code such as (assuming ReordID to be numeric)

CODE
Dim strText As String, intAnswer As Integer
strText = "Are you sure you want to delete " & Chr(34) & Me.NameOfSubFormControl.Form!FullText & Chr(34) & "?"
intAnswer = MsgBox(strText, vbYesNo)
If intAnswer = vbYes Then
Currentdb.Execute "DELETE RecordID FROM YourTable WHERE RecordID = " & Me.NameOfSubFormControl.Form!RecordID,dbFailOnError
Me.NameOfSubFormControl.Form.Requery
End If


should do it.

**Untested
Dani50
That worked perfectly Alan.
Thank you all for your help.
Dani.
Alan_G
No problem - glad it worked for you sad.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.