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

John Lee

New member
Local time
Today, 08:31
Joined
Dec 1, 2024
Messages
3
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...
 
Also, the "acSaveNo" in the following line is not required
In fact, it can be downright dangerous unless you are anal about saving and compiling before testing any code change.

acSaveNo refers to the form object NOT the data. Access ALWAYS saves the dirty data when a form closes. Users should NEVER be modifying a form object so it should never be in a "dirty" state in normal production. However, if you modify a form and forget to save it and go right to test and that test includes closing the form, Access will obey the command and discard any changes you made to the form object. OOPS:(
 
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 also use a single form at a time design and don't close a form when I open another. I simply hide the calling form by setting it's .visible property to False and opening the new form modal. That way code from the calling form stops running when the OpenForm line completes and resumes at the next line when the modal form closes. This is very convenient when the modal form changed data that is visible on the calling form because I can include code that requeries the calling form so the changed data is always visible..
 
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
 
You have a compile error. You need to fix that. Look at the name it is telling you that is not defined. You have a typo or have copied code that refers to a procedure that you didn't also copy and so it is missing.

It is also a good habit to save and compile any code BEFORE you try to run it. ALWAYS fix compile errors immediately. They will not go away and they can cause unusual errors in other parts of the application.
 
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.
 
DoCmd.OpenForm "FrmEmployees", acNormal
Although this works, it is illogical. Open the new form then close the current form

Code:
DoCmd.OpenForm "FrmEmployees", acNormal
DoCmd.Close acForm, Me.Name

I prefer Me.Name rather than hardcoding the form name. Saves copy/paste and typo errors.
 

Users who are viewing this thread

Back
Top Bottom