vba 刷新/重新查询组合框问题

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

Refresh/Requery Combobox problems

vbams-accessaccess-vbams-access-2003

提问by Michael Carn-Bennett

Afternoon,

下午,

I have having problems getting my ComboBox to update while the form it is on is open.

当我的 ComboBox 所在的表单处于打开状态时,我在更新 ComboBox 时遇到问题。

My data changes while the form is open, so the ComboBox needs to be refreshed as such but I can't work out how. It seems the only way is to close and then reopen the form, but I don't real

我的数据在表单打开时发生变化,因此需要刷新 ComboBox,但我不知道如何刷新。似乎唯一的方法是关闭然后重新打开表单,但我不真实

The ComboBox's raw source is a Simple Select query. I have tried using requery, but it doesn't seem to do anything.

ComboBox 的原始来源是一个简单的选择查询。我试过使用 requery,但它似乎没有做任何事情。

Sub ComboBox_GotFocus()

Me.ComboBox.Requery

End Sub

Any ideas?

有任何想法吗?

Cheers, Michael

干杯,迈克尔

回答by Trace

Empty and re-fill the combobox.
The easiest would be:

清空并重新填充组合框。
最简单的是:

sSQL_Select = "SELECT * FROM SOMETABLE" 

Me.lstListBox.RowSource = "" 
Me.lstListBox.RowSource = sSQL_Select

Instead of using the SQL Query, you can also explicitly add values to a listbox.
In this you can do something like:

除了使用 SQL 查询,您还可以显式地向列表框添加值。
在这种情况下,您可以执行以下操作:

Dim iList_Cnt As Integer
Dim iCnt As Integer

iList_Cnt = Me![lstListBox].ListCount

For iCnt = 0 To iList_Cnt - 1
    Me![lstListBox].RemoveItem 0
Next

Followed by refilling the listbox:

其次是重新填充列表框:

lstListbox.AddItem("Smtg_Col1;Smtg_Col2;Smtg_Col3")

Loop through the combobox for adding multiple rows.

循环组合框以添加多行。