VB code for Unbound Combo Box with table referenced drop down list

S

Such a Beginner

The following code always results in the first command being executed,
regardless of the Item, Anthocyanins, Liver_Lipids, Plasma, or ORAC being
updated. Can someone help me diagnose my code errors?

Private Sub Combo0_AfterUpdate()
If Unbound = Anthocyanins Then
DoCmd.OpenForm "frmAnthocyanins", acNormal
ElseIf Unbound = Liver_Lipids Then
DoCmd.OpenForm "frmLiverLipids", acNormal
ElseIf Unbound = Plasma Then
DoCmd.OpenForm "frmPlasma", acNormal
ElseIf Unbound = ORAC Then
DoCmd.OpenForm "frmORAC", acNormal
End If


End Sub
 
N

NetworkTrade

if your selection is actually those words then they should be surrounded by "
" in your vb

however your selection maybe the bound item in a combobox which is not
actually the text but a number or someother ID...in which case you would use
those values in your vb
 
S

Steve Sanford

This was answered by Clif 4/11/08 4:09PM PST
Subject: Need Help with Code (included in message)

but here is my answer....

The first problem is that the items "Anthocyanins", "Liver_Lipids", "Plasma"
and "ORAC" need to have quotes around them in the code:

Private Sub Combo0_AfterUpdate()
If Unbound = "Anthocyanins" Then
DoCmd.OpenForm "frmAnthocyanins"
ElseIf Unbound = "Liver_Lipids" Then
DoCmd.OpenForm "frmLiverLipids"
ElseIf Unbound = "Plasma" Then
DoCmd.OpenForm "frmPlasma"
ElseIf Unbound = "ORAC" Then
DoCmd.OpenForm "frmORAC"
End If
End Sub

This is assuming there is a text box named "Unbound". If you don't have a
text box named "Unbound", then your code should look something like this:

Private Sub Combo0_AfterUpdate()
If Me.Combo0 = "Anthocyanins" Then
DoCmd.OpenForm "frmAnthocyanins"
ElseIf Me.Combo0 = "Liver_Lipids" Then
DoCmd.OpenForm "frmLiverLipids"
ElseIf Me.Combo0 = "Plasma" Then
DoCmd.OpenForm "frmPlasma"
ElseIf Me.Combo0 = "ORAC" Then
DoCmd.OpenForm "frmORAC"
End If
End Sub


Another option would be using "Select Case" instead of the IF() function:

Private Sub Combo0_AfterUpdate()
Select Case Me.Combo0
Case "Anthocyanins"
DoCmd.OpenForm "frmAnthocyanins"
Case "Liver_Lipids"
DoCmd.OpenForm "frmLiverLipids"
Case "Plasma"
DoCmd.OpenForm "frmPlasma"
Case "ORAC"
DoCmd.OpenForm "frmORAC"
End Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top