vba 访问组合框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5593372/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:58:41  来源:igfitidea点击:

Access combobox value

ms-accessvba

提问by Jaanus

I have a combobox, and a button, that makes runs a query with the values it gets from combobox, but it does not seem to get the right value. enter image description here

我有一个组合框和一个按钮,可以使用它从组合框获得的值运行查询,但它似乎没有获得正确的值。 在此处输入图片说明

I tried using

我尝试使用

[Forms]![Kooli otsing]![Combobox] 

or

或者

[Forms]![Kooli otsing]![Combobox].[Text]

the query did not work, it seems like it does not get the value from combobox. because it worked with normal TextBox.

查询不起作用,它似乎没有从组合框中获取值。因为它适用于普通的 TextBox。

I ADDED EXPLAINING PICTURE!!!!! enter image description here

我加了说明图!!!!! 在此处输入图片说明

ADDED PICTURE OF VBA EDITOR enter image description here

添加了 VBA 编辑器的图片 在此处输入图片说明

ADDED PICTURE OF ERROR AND NO COMMENT AUTOCOMPLETE enter image description hereenter image description here

添加错误图片且无评论自动完成 在此处输入图片说明在此处输入图片说明

回答by mwolfe02

Based on the latest comments you posted on your question, you want to use:

根据您对问题发布的最新评论,您想使用:

[Forms]![Kooli otsing]![Combo19].Column(1)

Here's why. You said you have the following settings for your combobox:

这是为什么。你说你的组合框有以下设置:

  • column count: 2
  • bound column : 1
  • row source type : table/query
  • row source: SELECT [Haridusasutused].[ID], [Haridusasutused].[Nimetus] FROM Haridusasutused;
  • 列数:2
  • 绑定列:1
  • 行源类型:表/查询
  • 行来源: SELECT [Haridusasutused].[ID], [Haridusasutused].[Nimetus] FROM Haridusasutused;

Column count of 2 is telling Access to use the first two columns from your rowsource (the only two columns in this case). Bound column is telling access that the default value of the combobox should be the first column of the row source. In this case, that would be [Haridusasutused].[ID]. Often ID columns are autonumber fields.

列数为 2 是告诉 Access 使用行源中的前两列(在这种情况下只有两列)。绑定列告诉访问组合框的默认值应该是行源的第一列。在这种情况下,那就是[Haridusasutused].[ID]. ID 列通常是自动编号字段。

The reason you were having problems is that [Forms]![Kooli otsing]![Combo19]was returning data from the ID column (most likely a number) not "Elva Gümnaasium". By adding the .Column(1)you are telling Access to choose the data from the second column (.Columnis a zero-based array) of the rowsource, ie, "Elva Gümnaasium".

您遇到问题的原因是[Forms]![Kooli otsing]![Combo19]从 ID 列(很可能是数字)而不是“Elva Gümnaasium”返回数据。通过添加.Column(1)您告诉 Access 从行源的第二列(.Column从零开始的数组)中选择数据,即“Elva Gümnaasium”。

EDIT: Alternatively, you can change the bound column from 1 to 2and leave the rest alone (ie, you won't need the .Column(1)part at all).

编辑:或者,您可以将绑定列从 1 更改为 2并保留其余部分(即,您根本不需要该.Column(1)部分)。

回答by Mark Mooibroek

This works in my application:

这适用于我的应用程序:

[Forms]![Hour-registration]![mwkselect]

         ^form               ^combobox

Maybe try this to refresh:

也许试试这个来刷新:

Me.Requery
Me.Refresh

回答by Mikhail G

Have you tried to step through debugger and search for the value through the watch window? For instance put a breakpoint into a button click event, then add [Forms] to the watch window and look into it.

您是否尝试单步调试调试器并通过监视窗口搜索值?例如,在按钮单击事件中放置一个断点,然后将 [Forms] 添加到监视窗口并查看它。

回答by user2904400

You can use:

您可以使用:

[Forms]![Form1]![Combo1].[Text]