code to close my current form on pressing the button to open my Main Menu

John Lee

Member
Local time
Today, 13:46
Joined
Dec 1, 2024
Messages
42
I have the following forms :

frmFrontEnd
frmEmployees
frmInternalAudits
frmSOPs

In the on click event I want to: for example on clicking the button on my frmFrontEnd form to open my frmEmployees form, that on the frmEmployees form opening the frmFrontEnd form closes.

I tried this but it kept on producing an error message

Code:
Private Sub cmdOpenfrmFrontEnd_Click
On Error GoTo Err_cmdOpenfrmFrontEnd_Click

Dim stDocName As String
Dim stLinkCriteria as String

stDocName = "frmFrontEnd"
DoCmd.OpenForm stDocName,,, stLinkCriteria
DoCmd.Close acForm, Me.Name, acSaveNo

Exit_cmdOpenFrontEnd_Click
Exit Sub

Err_cmdOpenFrontEnd_Click
Msgbox Err.Description
Resume Exit_cmdOpenFrontEnd_Click
End Sub

I got the above code from an old database that I created many years ago and I still a copy of that database and that code is still working, so I'm somewhat confused as to why it's not working in the current version of Access [365].

Any assistance would be greatly appreciated.
 
Last edited by a moderator:
try stepping through the code
stDocName = "frmFrontEnd"
DoCmd.OpenForm stDocName,,, stLinkCriteria
As far as I can see "stLinkCriteria" has no value
 
Perhaps try:
DoCmd.OpenForm "frmFrontEnd"

Also, the "acSaveNo" in the following line is not required
DoCmd.Close acForm, Me.Name, acSaveNo
 
And you cannot tell us what that error message is? :-(
 
In the on click event I want to: for example on clicking the button on my frmFrontEnd form to open my frmEmployees form, that on the frmEmployees form opening the frmFrontEnd form closes.

I tried this but it kept on producing an error message
Hi. Welcome to AWF!

Agree, knowing the error message might give us some clue. Also, try closing the current form first before opening the other one (in case you ended up closing the form you just opened). Just a thought...
 
Unless you have a VERY small amount of RAM on your computer, it should be perfectly OK for you NOT close your main form (frmFrontEnd?) but just allow the other forms to "move to front". On my biggest project, I had a "dispatcher" form that never closed. There are technical reasons for a setup like that, including that it would always be the last form to close when exiting and would be the perfect place for shut-down actions, if any are needed. I always had my users on a single-form-at-a-time design, so I maximized the child forms, effectively hiding - but not closing - the dispatcher. Once you exit the child form, focus automatically snaps back to the last control you used on the main form - probably the "Launch form X" button. Two events that might help you on forms being launched or closing are the Activate and Exit events. You could look them up and see if they hold any value for you.

By the way, that was your first post. Welcome to the forums.
 
I have the following forms :

frmFrontEnd
frmEmployees
frmInternalAudits
frmSOPs

In the on click event I want to: for example on clicking the button on my frmFrontEnd form to open my frmEmployees form, that on the frmEmployees form opening the frmFrontEnd form closes.

I tried this but it kept on producing an error message

Private Sub cmdOpenfrmFrontEnd_Click
On Error GoTo Err_cmdOpenfrmFrontEnd_Click

Dim stDocName As String
Dim stLinkCriteria as String

stDocName = "frmFrontEnd"
DoCmd.OpenForm stDocName,,, stLinkCriteria
DoCmd.Close acForm, Me.Name, acSaveNo

Exit_cmdOpenFrontEnd_Click
Exit Sub

Err_cmdOpenFrontEnd_Click
Msgbox Err.Description
Resume Exit_cmdOpenFrontEnd_Click
End Sub

I got the above code from an old database that I created many years ago and I still a copy of that database and that code is still working, so I'm somewhat confused as to why it's not working in the current version of Access [365].

Any assistance would be greatly appreciated.
Code:
DoCmd.Close acForm, "FrmFrontEnd"
DoCmd.OpenForm "FrmEmployees", acNormal, , , , acWindowNormal
 
What is the error message?
Apologies I forgot to put the error message, When the button is click the on click event highlights the following bit of code:

Exit_cmdopenFrontEnd_Click and a pop up dialog box with the following message appears:

Dialogue box Title: Microsoft Visual Basic for Applications
a yellow triangle with an exclamation mark in it and the words Compile Error: Sub or Function not defined with an "OK" button and a "Help" button.

my VB knowledge is very limited, but I'm willing to give it a go.

Your assistance is most appreciated.
 
see this link about error handling and the syntax required

Exit_cmdOpenFrontEnd_Click - not sure what is is supposed to be doing since the next line exits the sub - but perhaps that also needs a colon suffix
Exit Sub

Err_cmdOpenFrontEnd_Click:
Msgbox Err.Description
Resume Exit_cmdOpenFrontEnd_Click
End Sub

recommend you get in the habit of indenting code - makes it much easier to read
 
Apologies I forgot to put the error message, When the button is click the on click event highlights the following bit of code:

Exit_cmdopenFrontEnd_Click and a pop up dialog box with the following message appears:

Dialogue box Title: Microsoft Visual Basic for Applications
a yellow triangle with an exclamation mark in it and the words Compile Error: Sub or Function not defined with an "OK" button and a "Help" button.

my VB knowledge is very limited, but I'm willing to give it a go.

Your assistance is most appreciated.
Make sure you have Option Explicit is at the top of all your existing modules.
Set the option for any new ones.
 
Apologies I forgot to put the error message, When the button is click the on click event highlights the following bit of code:

Exit_cmdopenFrontEnd_Click and a pop up dialog box with the following message appears:

Dialogue box Title: Microsoft Visual Basic for Applications
a yellow triangle with an exclamation mark in it and the words Compile Error: Sub or Function not defined with an "OK" button and a "Help" button.

my VB knowledge is very limited, but I'm willing to give it a go.

Your assistance is most appreciated.

I have the following forms :

frmFrontEnd
frmEmployees
frmInternalAudits
frmSOPs

In the on click event I want to: for example on clicking the button on my frmFrontEnd form to open my frmEmployees form, that on the frmEmployees form opening the frmFrontEnd form closes.

I tried this but it kept on producing an error message

Code:
Private Sub cmdOpenfrmFrontEnd_Click
On Error GoTo Err_cmdOpenfrmFrontEnd_Click

Dim stDocName As String
Dim stLinkCriteria as String

stDocName = "frmFrontEnd"
DoCmd.OpenForm stDocName,,, stLinkCriteria
DoCmd.Close acForm, Me.Name, acSaveNo

Exit_cmdOpenFrontEnd_Click
Exit Sub

Err_cmdOpenFrontEnd_Click
Msgbox Err.Description
Resume Exit_cmdOpenFrontEnd_Click
End Sub

I got the above code from an old database that I created many years ago and I still a copy of that database and that code is still working, so I'm somewhat confused as to why it's not working in the current version of Access [365].

Any assistance would be greatly appreciated.
Okay folks, thanks for your responses, perhaps my attempt at pinching some code from an older project isn't the correct way to go, so what I am trying to achieve is when I click the button on my frmFrontEnd form say for example the button the opens the frmEmployees form, I want the frmFrontEnd form to be closed once that form is opened, the reason I want to do this, is to only have in view the current form that the end user will be using, I don't want any other forms to be in view.
 
Okay folks, thanks for your responses, perhaps my attempt at pinching some code from an older project isn't the correct way to go, so what I am trying to achieve is when I click the button on my frmFrontEnd form say for example the button the opens the frmEmployees form, I want the frmFrontEnd form to be closed once that form is opened, the reason I want to do this, is to only have in view the current form that the end user will be using, I don't want any other forms to be in view.
Have you tried:

Code:
DoCmd.Close acForm, "FrmFrontEnd"
DoCmd.OpenForm "FrmEmployees", acNormal
 
Last edited:
You could just have the second form as a popup.
You could hide the mainform as previously advised.
I am not sure you can close a form wher the code is running.
You could also pass in the formname to the second form and close it from there. Then you would also know which form to open when you close that form.
 
Hi @John Lee,

You can simplify handling the opening and closing of forms using the following snippet:
Code:
Private Sub cmdOpenfrmFrontEnd_Click()
    DoCmd.BrowseTo acBrowseToForm, "frmFrontEnd"
End Sub
The BrowseTo command also supports a Where clause, which is useful if you need to specify the record the form should display. It's a handy approach for what you're trying to achieve.

Let me know if you need further clarification.
 

Users who are viewing this thread

Back
Top Bottom