Close Current Form and Open a New One Code

CSCS

Registered User.
Local time
Today, 02:20
Joined
Jan 10, 2004
Messages
91
Hi

I need a button code in the OnClick event.

I want when clicking on the button, to close the currently opened form and open another one.

Any help will be very much appreciated!

Thanks guys!
C.
 
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "YourNewFormName"

;)
 
it didn't work.

Here is my code:

Private Sub Command20_Click()
On Error GoTo Err_Command20_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "SearchResult"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Command20_Click:
Exit Sub

Err_Command20_Click:
MsgBox Err.Description
Resume Exit_Command20_Click

End Sub

where exactly should I put "DoCmd.Close".
 
Stop using the form wizards and write your own code. This is all you need to close the current form and open the SearchResult form...
Code:
Private Sub Command20_Click()
On Error GoTo Err_Command20_Click

     DoCmd.Close acForm, Me.Name
     DoCmd.OpenForm "SearchResult"

Exit_Command20_Click:
Exit Sub

Err_Command20_Click:
MsgBox err.number & " - " & Err.Description
Resume Exit_Command20_Click

End Sub
 
Hi! I've been trying to use the suggested code but in my particular case it doesn't do the trick. I'm using a combo box that when changed opens a new form with filtered results using the data selected in the combobox.
To do this I use this code:

Private Sub cboSearch_Change()
DoCmd.OpenForm "AutQuery"
Me.cboSearch = ""
End Sub

It works perfectly by itself. However, I'm having trouble adding the 'close' command. I tried:

Private Sub cboSearch_Change()
On Error GoTo Err_cboSearch_Change
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "CatQuery"
Exit_cboSearch_Change:
Exit Sub
Err_cboSearch_Change:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_cboSearch_Change
End Sub

AND also:

Private Sub cboSearch_Change()
DoCmd.Close
DoCmd.OpenForm "CatQuery"
Me.cboSearch = ""
End Sub

Both codes actually work in the sense that they close the current form and open a new one. The problem lies in the fact that the value selected through the combobox does not get 'sent' to the new form and a window asking for the cboSearch value opens up.
Any ideas?? Thanks!
 
Hi! I've been trying to use the suggested code but in my particular case it doesn't do the trick. I'm using a combo box that when changed opens a new form with filtered results using the data selected in the combobox.
To do this I use this code:

Private Sub cboSearch_Change()
DoCmd.OpenForm "AutQuery"
Me.cboSearch = ""
End Sub

It works perfectly by itself. However, I'm having trouble adding the 'close' command. I tried:

Private Sub cboSearch_Change()
On Error GoTo Err_cboSearch_Change
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "CatQuery"
Exit_cboSearch_Change:
Exit Sub
Err_cboSearch_Change:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_cboSearch_Change
End Sub

AND also:

Private Sub cboSearch_Change()
DoCmd.Close
DoCmd.OpenForm "CatQuery"
Me.cboSearch = ""
End Sub

Both codes actually work in the sense that they close the current form and open a new one. The problem lies in the fact that the value selected through the combobox does not get 'sent' to the new form and a window asking for the cboSearch value opens up.
Any ideas?? Thanks!
 
I always code my open form command before the close form command. You need to include the where condition in the open form command. Search the Access VBA help files for the OpenForm Method for more details and examples.
 
How do you call it now? With an OnClick() of a button? How ever you want to call it will run the code.
 

Users who are viewing this thread

Back
Top Bottom