如何用查询结果填充组合框 [英] How to populate a ComboBox with query results

查看:90
本文介绍了如何用查询结果填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问vba中从查询结果填充ComboBox时遇到问题。我的目标是查询表的一列,并将结果中的每条记录用作组合框的选项。

I had a problem populating a ComboBox from query results in access vba. My goal was to query for one column of a table and use every record from the result as an option for the combobox.

关于ComboBoxes的属性,您需要了解一些事情,并进行适当的分配才能使其正常工作。这是我的代码,似乎包含正确的信息,但未在下拉列表中显示任何内容:

There are some things about the properties of ComboBoxes you need to be aware of and assign properly to make this work. Here's my code that seemed to hold the correct information but did not display anything in the dropdown list:

Dim RS As DAO.Recordset
Dim SQL As String

'Clean-up. not sure if it's needed but I'm "clearing" the old data (if there's any) before putting the new in
combox.RowSourceType = "Table/Query"
combox.RowSource = ""

SQL = "SELECT [some_value] FROM [a_table] WHERE [another_value] = '" & argv(0) & "'"

combox.RowSource = SQL

这是<一个href = https://stackoverflow.com/questions/56579800/is-there-a-way-to-write-to-edit-a-otherwise-read-only-recordset-for-the-context>另一个问题我问过,但为了更清晰的结构,我将此问题(及其答案)移到了该线程。

This was part of another question I asked but for clearer structure I'm moving this question (and it's answer) to this thread.

推荐答案

您的第一次尝试很好,您只需重新查询组合框即可实际加载查询结果。

Your first attempt is fine, you just need to requery the combo box to actually load in the results of the query.

Dim SQL As String
combox.RowSourceType = "Table/Query"
combox.RowSource = ""

SQL = "SELECT [some_value] FROM [a_table] WHERE [another_value] = '" & argv(0) & "'"

combox.RowSource = SQL
combox.requery 'Clears old data, loads new data

这篇关于如何用查询结果填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆