Solved

Prevent unload event on Access form

Posted on 2008-10-22
18
798 Views
Last Modified: 2013-11-28
Hi, I'm using Axys2003. I've cut and past the solution into my unload trigger from the form:
If msgbox("Are you sure you want to close this form?",vbyesno,"test")=vbno then
     cancel=1
end if
But, never the less I puch the No button, the trigger continious and the form is closed. What did I do wrong?
0
Comment
Question by:jkruijt
  • 8
  • 7
  • 3
18 Comments
 
LVL 16

Expert Comment

by:Sheils
ID: 22783665
try Exit sub instead of cancel=1
0
 
LVL 77

Expert Comment

by:peter57r
ID: 22783669
The code you have posted should work in the way you require.
Is there other code in the unload event procedure?

And you are sure this IS the unload procedure and not say, the beforeupdate procedure?
0
 

Author Comment

by:jkruijt
ID: 22783675
Hi sb9,

Exit sub, does prevent the further executing of the trigger, but still closes the form.
0
 

Author Comment

by:jkruijt
ID: 22783680
Hi Peter57r

See for your self:

Private Sub Form_Close()
Dim stDocName As String

If MsgBox("Are you sure you want to close this form?", vbYesNo, "test") = vbNo Then
     Exit Sub
End If

MsgBox "after the message"

stDocName = "set_SecSelectOff"
DoCmd.OpenQuery stDocName, acNormal, acEdit
   
DoCmd.RunSQL "delete * from tmp_kandidaten"

End Sub
0
 
LVL 16

Expert Comment

by:Sheils
ID: 22783707
where is your code that close the form placed
0
 

Author Comment

by:jkruijt
ID: 22783726
sb9,
At the close form trigger. So it will execute when the user tries to close the form.
kind regards,
John
0
 
LVL 16

Expert Comment

by:Sheils
ID: 22783730
I'd use

Private Sub Command61_Click()

Dim strClose As String

strClose = MsgBox("Are you sure you want to close this form?", vbYesNo, "test")

If strClose = No Then

Exit Sub

Else

DoCmd.Close

End If
End Sub
0
 

Author Comment

by:jkruijt
ID: 22783740
sb9,

Does this work on the X button on the window? Or need I deactivate that?
0
 
LVL 77

Expert Comment

by:peter57r
ID: 22783755
That is NOT the Form Unload event.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 16

Expert Comment

by:Sheils
ID: 22783756
Yes because when you click x you trigger the close action. And this is before you msgbox condition
0
 

Author Comment

by:jkruijt
ID: 22783764
peter57r,
No you're right. I did see that and I have made a form_unload event. But the effect stays the same. Hitting No will close the form.
kind regards,
John
0
 
LVL 16

Expert Comment

by:Sheils
ID: 22783785
yah peter is right you can also put it in the on unload event. Then it will work with the X.
0
 

Author Comment

by:jkruijt
ID: 22783794
sb9,
But is doen't work with the X. I've tried that.
0
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 62 total points
ID: 22783795
I can assure you that this code works for me...

Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Are you sure you want to close this form?", vbYesNo, "test") = vbNo Then
     Cancel = 1
End If

End Sub
0
 

Author Comment

by:jkruijt
ID: 22783805
Peter,
You're sure ypur form doesn't close and unload?
0
 
LVL 16

Accepted Solution

by:
Sheils earned 63 total points
ID: 22783806
Private Sub Form_Unload(Cancel As Integer)
Dim strclose As String

strclose = MsgBox("Are you sure you want to close this form?", vbYesNo, "test")

If strclose = vbNo Then

Cancel = 1

End If
End Sub
0
 

Author Comment

by:jkruijt
ID: 22783830
sb9 and Peter,

Both your solution are right. I did had an close trigger also 9with no commands) and this seams to interfere with the unload trigger. Thanks the bot h of you!
0
 
LVL 16

Expert Comment

by:Sheils
ID: 22783834
I guess peter deserves the points for this. JK both code should work
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now