您需要在vba中建立一個sql語句,然後使用記錄集運行它,以獲得結果。
顯然,我不知道你需要得到的數據是,或者從表中你;會得到它,但是在where子句需要如下就可以了:
dim wc as string
wc = wc & iif(lst_costcenter.ItemsSelected.Count = 0, "", " AND " & InClause(lst_costcenter, "tablename.columnname", false))
wc = wc & iif(lst_itemnumber.ItemsSelected.Count = 0, "", " AND " & InClause(...
最後,當構建SQL語句,你需要砍第一「和」 WC &與「WHERE」
wc = iif(wc <> "", " WHERE " & mid(trim(wc), 5), "")
InClause是,你需要一個模塊中添加,或在查詢的功能取代它的表格本身: 它使用3個參數: 1.用於爲 構建in子句的列表框控件2.包含tablename-dot-columnname的字符串,該字符串是要針對在列表框中選擇的值進行過濾的表/列,& 3.根據是否爲True/False列的數據類型爲字符串(真)或數字(假)
Public Function InClause(lst as ListBox, tblcol as string, isAlpha as boolean)
Dim si As String
Dim vv As Variant
For Each vv In lstBox.ItemsSelected
If isAlpha Then
si = si & "," & Chr(34) & lstBox.Column(0, vv) & Chr(34)
Else
si = si & "," & lstBox.Column(0, vv)
End If
Next vv
If si <> "" Then
si = "(" & tblcol & " IN (" & mid(si, 2) & "))"
End If
InClause = si
End Function
希望這有助於