Sat 8 Apr 2006
MS Access: Multiselect in Queries
Posted by Ralf under IT, VBA
[28] Comments
In MS Access verwendet man in den Benutzerschnittstellen gern Listen mit Multiselect-Funktion, um dem Anwender flexiblere Auswahlmöglichkeiten zu bieten.
In Queries ist es jedoch etwas schwierig diese Optionen abzufragen. Folgende VBA-Funktion schafft da Abhilfe:
Function InMultiSelect(frms, ctrl As String, col As Integer, data As Variant, ParamArray OtherArgs()) As Boolean
'Checks whether a Variant (data or OtherArgs) is included in the specified column (col) of a ListBox (ctrl)
'in a certain Form (frms)
On Error GoTo Error_InMultiSelect
Dim varItm As Variant
Dim index As Integer
Dim ctl As Control
Dim frm As Form
Set frm = Forms(frms)
Set ctl = frm.Controls(ctrl)
InMultiSelect = False
For Each varItm In ctl.ItemsSelected
If InMultiSelect = True Then Exit For
If CStr(data) = CStr(ctl.Column(col, varItm)) Then InMultiSelect = True
For index = LBound(OtherArgs) To UBound(OtherArgs)
If InMultiSelect = True Then Exit For
If CStr(OtherArgs(index)) = CStr(ctl.Column(col, varItm)) Then InMultiSelect = True
Next index
Next varItm
Exit Function
Error_InMultiSelect:
InMultiSelect = False
Exit Function
End Function
In den Abfragen (Queries) selbst wird diese Funktion dann beispielsweise so aufgerufen:
SELECT *
FROM [tblTabelle]
WHERE InMultiSelect("[frmFormMitMultiSelectAuswahl]","[lstListeMitMultiSelectAuswahl]",0,[tblTabelle].[Spalte]))<>False);
28 Responses to “ MS Access: Multiselect in Queries ”