Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close current form and open a different one in Access?

I'm trying to create a macro that closes the current form and opens the main form.

Since I have so many forms and I need the same button with this macro in all of them, I want to create a macro just once and not for each form separately.

Is there any way to do so?

like image 644
Yarden Avatar asked Jan 14 '14 13:01

Yarden


People also ask

How do I close active form in Access VBA?

You can close the active form (or other Access object) using DoCmd. Close without any arguments. However if you want to be sure the intended form is closed it is better to be explicit.

What does on current mean in access?

The On Current event occurs when the focus moves to a new or different record making it the current record, or when the Form is Refreshed or Requeried. This Event occurs when a form is opened, whenever the focus leaves one record and moves to another, and when the Form's underlying Table or Query is requeried.


1 Answers

Create a code module and add the following function:

Public Sub CloseMeAndOpenMain(frmMe As Form)
    DoCmd.Close acForm, frmMe.Name
    DoCmd.OpenForm "frmMain" 'Replace this with the actual name of your Main form
End Sub

Now, each time you have a button that should close the current form and open the "frmMain" form, you would write the click event handler like so:

Private Sub btnCloseForm_Click()
    CloseMeAndOpenMain Me
End Sub

Me is a reference to the current form, and each form has a Name property. Therefore you can write a function to close it without having to hardcode the name as a String (except of course for the main form).

like image 184
Blackhawk Avatar answered Sep 18 '22 03:09

Blackhawk