David Morin
David Morin

Reputation: 397

Access 2010 VBA how to check value of a listbox

Alright, just a warning, I'm a complete rookie with VBA and Access, but I'm plugging away at it.

I've got a form with some listboxes on it, what I want to do is hide or show certain listboxes and fields of the form based on the "parent" listbox's selection.

I.e. "location" listbox contains many choices, one of them called "Canada"

When the user selects "Canada" I want the form to show another box called "provinces"

Current code:

Private Sub Form_Load()
MsgBox "Form Loaded"
    If Forms![Tickets]![location] = "Canada" Then
    MsgBox "location is Canada!"
End If
End Sub

The msgBox is in the if statement simply for me to see if the if statement is being triggered, whenever I figured this out I'll change that to the code I want executed.

I thought I knew how to reference the controls on the forms from VBA but I might be doing that wrong. I come from a PHP/Mysql background so I'm still grasping the language.

Cheers

Upvotes: 1

Views: 999

Answers (1)

Fionnuala
Fionnuala

Reputation: 91356

If the SQL of a listbox is set to:

SELECT Id, SomeOtherField FROM aTable

Then the value of the list box is Id, not SomeOtherField.

In this case you should say:

If Me.[location] = 1 Then  ''Where 1 is the Id you want.

To check the value of a control, you can click on an item and use ctrl+G to get the immediate window, then type

?Screen.ActiveControl 

Upvotes: 1

Related Questions