Solved

Prevent unload event on Access form

Posted on 2008-10-22
18
804 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

696 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