Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I run a sub procedure when a form closes?

Status
Not open for further replies.

LisetteR

Programmer
Oct 4, 2004
8
0
0
GB
Hello, beginner here.

I want an Access database to be closed as a VB form is closed. Firstly I'm not sure how to call the procedure, I thought it would come under:

Private Sub Form_Close()

However nothing happens when I close the form, so I assume that is wrong.

Secondly, I want the Access application as well as the database to close when I call this procedure. At the moment I have:

Private Sub Form_Close()
objAccess.CloseCurrentDatabase
Set objAccess = Nothing
End Sub

But this only closes the database and leaves the application running.

Any help would be appreciated.
Lisette
 
Try form_unload.

"I'm living so far beyond my income that we may almost be said to be living apart
 
There is no form "Close" event. Use Form_Unload or Form_QueryUnload event and place your code there.

Robert
 
Hi,

I put this in (my form is called form1):

Private Sub Form1_Unload()
objAccess.CloseCurrentDatabase
Set objAccess = Nothing
End Sub

But still nothing happens when I close the form.

Incidentally, when I use the following code which only uses the word Form as an identifier (not Form1) it works as Form1 opens:

Private Sub Form_Load()

Application = "C:\Program Files\Office97\Office\MSACCESS.EXE"
objAccess.OpenCurrentDatabase "C:\TideWeather.mdb"

objAccess.DoCmd.OpenForm ("Start Sending")
objAccess.Screen.ActiveForm.ActiveControl.Requery

End Sub

I'm confused!
 
The ide will create the event procedure for you, choose form from the drop down box on the lhs of the code window, thne chose either unload or queryunload from the drop down box on the right

this will automatically create the event definition which you can then fill in with your code.

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
When developing, do you manually create your event procedures by typing or do you select the object from the object dropdown and then select the event from the eventlist?
If you do the latter it should create the correct procedure for you.
Secondly, when you say you close your form, how are you closing it? are you using a menu or button which has code like
"unload form1"
or something similiar?
You should debug your code to see if the event is actually been triggered but I would guess that something is either reloading your form again after closure (any reference to any form properties or objects on the form will cause it to load again), or you arent closing it probably hiding it instead.
I would suggest debugging your application and see what code is being called.



"I'm living so far beyond my income that we may almost be said to be living apart
 
Fantasic, thanks guys!

One last problem. How do I kill the Access application as well as the database? At the moment the database is closed but leaves the Access window.

Lisette
 
Ahhh Something was missed... I think this is an Access IDE question? wrong forum.


Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
try
Code:
Application.quit
but as CasperTFG points out, you are liable to get more accurate help if in the correct forum as everyone who read this probably thought you were talking about VB as opposed to VBA. (Mainly subtle but some major differences)

"I'm living so far beyond my income that we may almost be said to be living apart
 
Is this not what the Form_Terminate event is for?
Maybe that isn't in Access VBA
 
But the OP was executing the code from a VB form...

>>I want an Access database to be closed as a VB form is closed
 
? or is that a VBA form ? [smile]

LisetteR
Read faq222-2244 for guidance on posting

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top