我有一个组合框,我想将其值与 SQL WHERE 子句一起使用。如何根据组合框值在 VBA 中运行SELECT 语句?
问问题
191868 次
3 回答
29
如果您希望使用绑定的列值,您可以简单地参考组合:
sSQL = "SELECT * FROM MyTable WHERE ID = " & Me.MyCombo
也可以参考列属性:
sSQL = "SELECT * FROM MyTable WHERE AText = '" & Me.MyCombo.Column(1) & "'"
Dim rs As DAO.Recordset
Set rs = CurrentDB.OpenRecordset(sSQL)
strText = rs!AText
strText = rs.Fields(1)
在文本框中:
= DlookUp("AText","MyTable","ID=" & MyCombo)
*已编辑
于 2012-06-13T14:41:30.010 回答
7
Access 2007 可能会丢失 CurrentDb:请参阅http://support.microsoft.com/kb/167173,因此如果示例中出现“对象无效或不再设置”,请使用:
Dim db as Database
Dim rs As DAO.Recordset
Set db = CurrentDB
Set rs = db.OpenRecordset("SELECT * FROM myTable")
于 2013-10-16T10:31:08.910 回答
3
这是在 VBA 中使用 SQLSELECT
语句的另一种方法:
sSQL = "SELECT Variable FROM GroupTable WHERE VariableCode = '" & Me.comboBox & "'"
Set rs = CurrentDb.OpenRecordset(sSQL)
On Error GoTo resultsetError
dbValue = rs!Variable
MsgBox dbValue, vbOKOnly, "RS VALUE"
resultsetError:
MsgBox "Error Retrieving value from database",VbOkOnly,"Database Error"
于 2013-04-07T13:36:54.377 回答