我在Access 2013中创建了一个查询,该查询使用两个参数
PARAMETERS blah TYPE, blah TYPE;
SELECT * FROM blah WHERE blah blah;
我想运行该查询并在列表框中显示结果。
通常我会喜欢
Me.MyListBox.RowSource="myQuery"
但当我这样做时,会弹出一个框,告诉我输入第一个参数。如何用程序指定参数?
我的第二种方法是类似
With CurrentDb.QueryDefs("myQuery")
.Parameters("param1") = 1
.Parameters("param2") = 2
Me.MyListBox.RowSource = .OpenRecordset()
End With
这让我的类型不匹配?
我该怎么做?
编辑:为了让事情变得清楚,我知道我可以连接字符串来构建我想要的查询,比如:
Me.MyListBox.RowSource="SELECT*FROM table WHERE abc Like'"&somevalue&"'"
但这正是我想要避免的,因为这会使代码难以维护和阅读。
好吧,我想我已经找到了我要找的东西。
Private Sub SomeButton_Click()
With CurrentDb.QueryDefs("myQuery")
.Parameters("firstParameter") = 2
Set Me.MyListBox.Recordset = .OpenRecordset
.Close
End With
End Sub
如果您想运行INSERT/UPDATE/DELETE,我认为您可以执行同样的操作,但不使用OpenRecordset,而是调用Execute。
根据列表框中的筛选条件是多选还是单个条目,以下操作将起作用。做出选择后,您需要重新查询ListBox。
Public Function sListBox() As String
If Not IsNull(Forms![frmListbox]![lstDate]) Then
sListBox = Forms![frmListbox]![lstDate]
End If
End Function
您的查询可能看起来像(直接引用ComboBox和ListBox:的函数
SELECT CUSTOMER.ProductNumber, CUSTOMER.LastOrder
FROM CUSTOMER
WHERE (((CUSTOMER.ProductNumber)=Forms!frmListbox!cboCustomer)
And ((CUSTOMER.LastOrder)=sListBox()));