PDA

View Full Version : Solved: Syntax for referring to the value in a combo box?



AKK
07-06-2009, 12:53 PM
I have done a good bit of VBA in Excel, but I'm now using it with Access for the first time, and it looks like some of the syntax & names for things is a bit different. I know this must be pretty basic, but I've searched and searched and can't find the answer anywhere.

I've defined a variable called sProject (it's a string), and the first thing my code does is call up a user form with a combo box. The user then picks a project from the drop-down list. I want to tell Access to make their choice the value for the variable sProject. ie, if I were working in Excel, I think I'd write it something like...

Sub CallEmailList()

Dim sProject As String

Dim sGroup As String

Form_frmGetEmailList.Visible = True

Set sProject = Forms.Form_frmGetEmailList.Visible.SelectedProject.Value

'Form_frmGetEmailList is the name of the form & SelectedProject
'is the name of the combo box where the user picks the value

{do some stuff with the value that's selected}

End Sub

Clearly Set sProject = Forms.Form_frmGetEmailList.Visible.SelectedProject.Value is not the way to go! Can anyone help?

Thanks,

AKK

CreganTur
07-06-2009, 01:03 PM
Wait...:think: are you trying to assign a default value to the combobox, or capture the user's selection from the combobox in a VBA Variable? Can't tell from your post.

AKK
07-06-2009, 01:13 PM
I think capture the user's selection from the combobox in a VBA Variable. (The combo box lists several choices -- Project1, Project2, Project3, etc. If the user selects Project2 from the list, for example, I want the variable sProject to take on the value Project2 & then use that value to carry out some other tasks.)

CreganTur
07-06-2009, 01:26 PM
As long as the code to do this is behind the same form as your object, you can cut out a lot of coding by using the 'Me.' keyword- Me is shorthand that refers to the current, active form that code is behind.

So you can use:
sProject = Me.Combobox
Just replace Combobox with the name of your combobox object. You do not have to use the .Value property- this will automatically pull in the selected value from the object.

Also, you only need to use the Set keyword when you are working with object variables.

Please wrap your code in VBA tags (click the green VBA button) it will format your code according to VBIDE and make it easier to read :thumb