?
Solved

Prevent unload event on Access form

Posted on 2008-10-22
18
Medium Priority
?
808 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

800 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