Passing combobox value into SQL query in MS Access
I have a combobox i开发者_如何学运维n a form, and I want the text of the combobox to be passed into a query.
My query is:
select..from..where something=[Forms]![Enter Data]![comboCup]
The form's name is Enter Data
and the combobox's name is comboCup
. Should i do this:
[Forms]![Enter Data]![comboCup]![text]
or this?
[Forms]![Enter Data]![comboCup]![value]
You should use [Forms]![Enter Data]![comboCup].
As @Remou has said, the .Text property of an Access control is available only when the control has the focus.
The .Value property is redundant, as it's the default property of all Access controls, so these two are equivalent:
[Forms]![Enter Data]![comboCup]
[Forms]![Enter Data]![comboCup].Value
(note also that properties like .Text and .Value are separated by the dot operator and not the bang, which delineates collections)
One issue that can be of concern is if you want to use the value of the combo box in the SELECT statement of an APPEND query. In that case, you would be advised to declare the combo box as a parameter in your saved query. If you do not, it can cause the row to not be inserted, whereas if you declare the parameter, it will resolve to the Null that is the value in the referenced combo box.
Neither. Text is only available when the control has the focus. The value of comboCup is the bound column. Ensure that your query is looking for that value, otherwise you will need to refer to the column property of the combo.
Dim comboBoxText As String
comboBoxText = Me.YourComboboxName.Column(1)
Note: Combobox column is 1 based array
If you'll work it in the Form module, you can do something like this (pseudo code only):
Event comboCup_afterUpdate()
strCup = Me!comboCup
strSQL = "SELECT ... etc ... ='" & strCup & "'"
End Sub
If in a different module, then still use variables as shown above; in that case however, your syntax for identifying the field in the form needs a lot of work. i can tell you more about that if this all makes sense so far.
精彩评论