Close Form

  • Thread starter Thread starter Clarence
  • Start date Start date
C

Clarence

How do I code the onclick event for a button on a form
that will close the current form and open another form...

for ex....

Form1 (currently open)

onclick of button1, Form1 closes and Form2 opens....


Thanks,
Clarence
 
Private Sub button1_Click()
DoCmd.OpenForm "Form2", , , stLinkCriteria
DoCmd.Close acForm "Form1"
End Sub
 
If you are closing a bound form, *please* explicitly save the record first!

There is a bug in Access that can cause it to lose your entry if you don't.
Details in this link:
http://allenbrowne.com/bug-01.html

The Click event procedure for your button will end up as something like
this:

Private Sub button1_Click()
If Me.Dirty Then
RunCommand acCmdSaveRecord
End If
DoCmd.OpenForm "Form2"
DoCmd.Close acForm, Me.Name
End Sub

The code will give errors if the record cannot be saved, so you need to
include error handling.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top