close button with warning message (1 Viewer)

AccessWater

Registered User.
Local time
Yesterday, 22:08
Joined
Jun 14, 2006
Messages
52
I use a close button to close a form. There is list box in this Form. If the list box is empty, then simly close the form. However, if there are projects in the list box, I want to give a warning. If the user say YES, then everything in the listbox will be deleted and the form will be closed. If user say NO, tehn the form will not be closed. They then can use anotehr button to save teh projects.

I wrote the follwing code. The delete portion itself works. But it seesm comfused with msgbox. Could you pelase help me correct this code? Thank you very much for your help.

lbDestination -- list box name



Private Sub Close_Click()
On Error GoTo Err_Close_Click

If IsNull(Me.lbDestination) Then
DoCmd.Close
Exit Sub
End If

If MsgBox("You have unsaved initiatives. Are you sure you want to close?", vbQuestion + vbYesNo + vbDefaultButton2, _
"Delete?") = vbYes Then

Dim rsDestination As DAO.Recordset
Set rsDestination = CurrentDb.OpenRecordset("tbInit", dbOpenDynaset)

rsDestination.MoveFirst
Do While Not rsDestination.EOF

rsDestination.Edit
rsDestination.Delete
rsDestination.MoveNext
Loop

Me.lbDestination.Requery

End If


Exit_Close_Click:
Exit Sub

Err_Close_Click:
Resume Exit_Close_Click



End Sub
 

elbweb

Self Taught Hero
Local time
Yesterday, 22:08
Joined
Jul 28, 2006
Messages
126
Code:
If lbDestination.ListCount = 0 Then
	DoCmd.Close
ElseIf msgbox("You have unsaved initiatives. Are you sure you want to close?", vbQuestion + vbYesNo + vbDefaultButton2, "Delete?") = vbYes Then
	DoCmd.RunSQL "DELETE * FROM tbInit;"
	lbDestination.Requery
Else
	'if user says no, do nothing
End If

now i know this looks a lot different but its basically the same thing, and it should work. Just streamlined everything, less code, less confusion, maybe you will be able to figure out what is going wrong now, if anything is still going wrong.
 

AccessWater

Registered User.
Local time
Yesterday, 22:08
Joined
Jun 14, 2006
Messages
52
Hi, elbweb, Thank you so much. It works great. That is really a much simpler and cleaner code.
 
Last edited:

elbweb

Self Taught Hero
Local time
Yesterday, 22:08
Joined
Jul 28, 2006
Messages
126
i'm glad it works, but most importantly, i hope you can use it to understand what its doing differently, and use that for yourself later if your other code :)
 

Users who are viewing this thread

Top Bottom