Solved

Prevent unload event on Access form

Posted on 2008-10-22
18
802 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

813 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

9 Experts available now in Live!

Get 1:1 Help Now