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?
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With