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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to open a form based on combo box selection

Status
Not open for further replies.

NC1977

Programmer
Nov 26, 2007
22
CA
Hi everyone,

I have a combo box on my switchboard with a list of forms that can be opened. Once the user selects the form they need, I have a command button that opens the form. I've tried adding the if statement to my code, It doesn't seem to work:

Private Sub Command134_Click()
On Error GoTo Err_Command134_Click

Dim stDocName As String
Dim stLinkCriteria As String

If stDocName = "Account Receivable" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria

If stDocName = "GeneralLedger" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria

If stDocName = "Interface" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria

If stDocName = "Account Payable" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria

Help! What am I doing wrong???
 
You need something on the lines of:

Code:
Private Sub Command134_Click()
On Error GoTo Err_Command134_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = Me.MyComboBoxName 
    DoCmd.OpenForm stDocName, , , stLinkCriteria
End Sub

Always call controls something meaningful. You will thank yourself for it later.
 
You forgot the else statements -


Private Sub Command134_Click()
On Error GoTo Err_Command134_Click

Dim stDocName As String
Dim stLinkCriteria As String

If stDocName = "Account Receivable" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria

ElseIf stDocName = "GeneralLedger" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria

ElseIf stDocName = "Interface" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria

ElseIf stDocName = "Account Payable" Then
DoCmd.OpenForm stDocName, , , stLinkCriteria

End If
 
You dim stDocName but then do not set it to anything. It's null so doesn't meet any of your conditions. Also, you do not set stLinkCriteria as anything, so really have a null WHERE string. Maybe you do not understand the OpenForm statement?

In any case, get rid of all of your code. All you need is this:
Code:
DoCmd.OpenForm Me.cboFormName, acNormal

where "cboFormName" is the name of the combo box.

Also I suggest naming your button something other than "Command134", something like "btnOpenForm" or something sensible so when you are looking at a long code sheet later, you don't have a bunch of things named generically that are difficult to pick through.



Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
THANK YOU GingerR...It works perfect and have taken your advice about the naming convention! I haven't worked in Access in a long long time...And now have 2 db to create...Thanks for your help I appreciate it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top