vba 如何根据查询结果填充文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15333974/
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
How to populate a textbox from the result of a query
提问by Kaja
I have a combobox and a Textbox on my form.
我的表单上有一个组合框和一个文本框。
If a user chooses a value from the Combobox for example: Cvaluethen I want to populate the textbox with the results of a query, for example :
如果用户从组合框中选择一个值,例如:Cvalue然后我想用查询的结果填充文本框,例如:
Select S1 From Test where Name=Cvalue
How can I do that?
我怎样才能做到这一点?
回答by Dan Metheus
If the textbox you are populating is unbound (doesn't have to update a field in the underlying form data source) then you can use DLOOKUP
in the text box's control source and avoid VBA:
如果您正在填充的文本框未绑定(不必更新底层表单数据源中的字段),那么您可以DLOOKUP
在文本框的控件源中使用并避免使用 VBA:
=DLOOKUP("S1", "Test", "cvalue='" & forms!MyForm!Combo0 & "'")
回答by Dan Metheus
Private Sub Command4_Click()
Dim con As ADODB.Connection
Set con = Application.CurrentProject.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
ssql = "select s1 from test where cvalue='" & Combo0.Value & "'"
rs.Open ssql, con
Do Until rs.EOF = True
Text2.SetFocus
Text2.Text = rs.Fields!s1
rs.MoveNext
Loop
End Sub
回答by bs0d
txtBox1.text = myResults.GetString
txtBox1.text = myResults.GetString
Provided txtBox1 is your textbox, and myResults are the recordset from your query. No loop required.
提供的 txtBox1 是您的文本框,而 myResults 是您查询的记录集。不需要循环。