Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

What's wrong with this code?

I must be not doing something right. I've got a combo box listing values from a table. Each value corresponds to a different form. When the user makes his selection he presses a button that opens the appropriate form based on which value was selected.

Here's where I started:

Private Sub Command8_Click()
    Dim stForm1 As String
    Dim stForm2 As String
    Dim stForm3 As String
    Dim stForm4 As String
   
    stForm1 = "EngForm"
   
    If [Me.combo5.text] = "Engineering" Then
    DoCmd.OpenForm stForm1
    Else
    End If
   
End Sub

Seems like it should work to me but I'm somewhat of a beginner.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

   If [Me.combo5.text] = "Engineering" Then
is wrong...

this should work better
    If Me.combo5.text = "Engineering" Then
Avatar of Brian

ASKER

I tried that first. Couldn't get it to work.

I should have been more specific about the error I'm getting.

Run-time error '2465':
Microsoft Access can't find the field '|' referred to in your expression.


Mind you, all the field and form names are correct in the code.
Avatar of Brian

ASKER

Ok, scratch that. When I remove the brackets error I get is this:

Run-time error '2185':
You can't reference a property or method for a control unless the control has the focus.
ok. try to use .Value instead of .Text
Avatar of TRobinJames
TRobinJames

Is this VB6 or VBA or ...?
VB6
    Dim fTemp As Form
    Set fTemp = Forms("EngForm")
    fTemp.Show
Avatar of Brian

ASKER

VBA
So, is this a combobox on a userform or on a sheet?
There is no need to use a variable as I see in your example
    If [Me.combo5.text] = "Engineering" Then
            DoCmd.OpenForm "EngForm"
    End if
ASKER CERTIFIED SOLUTION
Avatar of 3_S
3_S

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

3S got it. Thanks for the help!!