Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access Command Buttons 2

Status
Not open for further replies.

voltarei

Technical User
Oct 25, 2006
40
GB
Hi,
As a new Access user (I know I started late !!), I would like to know if it is possible to have one command button on a form that will open up a seperate form and close the existing one when clicked so that the user doesn't end up with a load of open forms on screen.

Many thanks
 
You can use the wizard to build code to open a form and also to close a form. You can cut the code from the close button and paste it into the open button.
 
Thanks for the response.
I have already tried to do that, but it wouldn't work and gave me a code error.
Maybe there is a certain order the code has to be in - I don't know !!
 
Here is the code from the code builder:

Private Sub Add_Vehicle_Details_Command_Button_Click()
On Error GoTo Err_Add_Vehicle_Details_Command_Button_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Vehicle Specification Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Private Sub Add_Vehicle_Details_Command_Button_Click()
On Error GoTo Err_Add_Vehicle_Details_Command_Button_Click


DoCmd.Close

Exit_Add_Vehicle_Details_Command_Button_Click:
Exit Sub

Err_Add_Vehicle_Details_Command_Button_Click:
MsgBox Err.Description
Resume Exit_Add_Vehicle_Details_Command_Button_Click


Exit_Add_Vehicle_Details_Command_Button_:
Exit Sub


End Sub
 
Here is an explanation:

Code:
Private Sub Add_Vehicle_Details_Command_Button_Click()
'What to do if an error occurs, that is, error trapping
On Error GoTo Err_Add_Vehicle_Details_Command_Button_Click

'Variable to hold the name of the form 
    Dim stDocName As String
'Variable to hold any criteria needed, not used
    Dim stLinkCriteria As String

'Put the name of the form in the variable
    stDocName = "Vehicle Specification Form"
'Open the form 
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    
'This is the name of the sub, so you do not need to 
'use it again.   
'Private Sub Add_Vehicle_Details_Command_Button_Click()
'You already have instuctions for error trapping,
'so you do not need more.
'On Error GoTo Err_Add_Vehicle_Details_Command_Button_Click

'The action to perform, you need this. You should 
'add a little to it.
    DoCmd.Close [blue]acForm, Me.name[/blue]

'What to do if an error does not happen, that is, 
'end of story.
Exit_Add_Vehicle_Details_Command_Button_Click:
    Exit Sub

'What to do if an error happens, part 2 
Err_Add_Vehicle_Details_Command_Button_Click:
    MsgBox Err.Description
    Resume Exit_Add_Vehicle_Details_Command_Button_Click
    
'You already have one of these    
'Exit_Add_Vehicle_Details_Command_Button_:
'    Exit Sub
  
End Sub

I hope that will help you with merging other bits of wizard code.
 
Obviously, you have two Subs with the same name. Try removing the second instance of
Code:
 Private Sub Add_Vehicle_Details_Command_Button_Click()
On Error GoTo Err_Add_Vehicle_Details_Command_Button_Click
and the extra

Exit_Add_Vehicle_Details_Command_Button_:
Exit Sub



Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top