Solved

Prevent unload event on Access form

Posted on 2008-10-22
18
806 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

695 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