简体   繁体   English

将组合框值传递到MS Access中的SQL查询

[英]Passing combobox value into SQL query in MS Access

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 . 表单的名称是Enter Data ,组合框的名称是comboCup Should i do this: 我应该这样做:

[Forms]![Enter Data]![comboCup]![text]

or this? 或这个?

[Forms]![Enter Data]![comboCup]![value]

You should use [Forms]![Enter Data]![comboCup]. 你应该使用[Forms]![输入数据]![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. 可能需要关注的一个问题是,如果要在APPEND查询的SELECT语句中使用组合框的值。 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. 如果不这样做,则可能导致不插入行,而如果声明参数,则它将解析为Null,即引用的组合框中的值。

Neither. 都不是。 Text is only available when the control has the focus. 仅当控件具有焦点时,文本才可用。 The value of comboCup is the bound column. comboCup的值是绑定列。 Ensure that your query is looking for that value, otherwise you will need to refer to the column property of the combo. 确保您的查询正在查找该值,否则您将需要引用该组合的column属性。

Dim comboBoxText As String 

comboBoxText = Me.YourComboboxName.Column(1)

Note: Combobox column is 1 based array 注意:Combobox列是基于1的数组

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. 如果到目前为止这一切都有意义,我可以告诉你更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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