将组合框值传递给 MS Access 中的 SQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2845087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing combobox value into SQL query in MS Access
提问by l--''''''---------''''''''''''
I have a combobox in 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:
表单的名称是Enter Data
,组合框的名称是comboCup
。我应该这样做:
[Forms]![Enter Data]![comboCup]![text]
or this?
或这个?
[Forms]![Enter Data]![comboCup]![value]
回答by David-W-Fenton
You should use [Forms]![Enter Data]![comboCup].
您应该使用 [Forms]![Enter Data]![comboCup]。
As @Remou has said, the .Text property of an Access control is available only when the control has the focus.
正如@Remou 所说,仅当控件具有焦点时,Access 控件的 .Text 属性才可用。
The .Value property is redundant, as it's the default property of all Access controls, so these two are equivalent:
.Value 属性是多余的,因为它是所有 Access 控件的默认属性,所以这两个是等效的:
[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)
(还要注意,像 .Text 和 .Value 这样的属性由点运算符分隔,而不是由划定集合的 bang 分隔)
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.
一个值得关注的问题是,如果您想在 APPEND 查询的 SELECT 语句中使用组合框的值。在这种情况下,建议您在保存的查询中将组合框声明为参数。如果不这样做,可能会导致无法插入行,而如果声明参数,它将解析为 Null,即引用组合框中的值。
回答by Fionnuala
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.
两者都不。文本仅在控件具有焦点时可用。comboCup 的值是绑定列。确保您的查询正在寻找该值,否则您将需要引用组合的列属性。
回答by Pritesh
Dim comboBoxText As String
comboBoxText = Me.YourComboboxName.Column(1)
Note: Combobox column is 1 based array
注意:组合框列是基于 1 的数组
回答by Smandoli
If you'll work it in the Form module, you can do something like this (pseudo code only):
如果您将在 Form 模块中使用它,则可以执行以下操作(仅限伪代码):
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.
如果在不同的模块中,那么仍然使用如上所示的变量;但是,在这种情况下,用于标识表单中字段的语法需要大量工作。如果到目前为止这一切都有意义,我可以告诉你更多。