简体   繁体   中英

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.

Ie "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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM