alsoascientist
Registered User.
- Local time
- Today, 15:27
- Joined
- Mar 26, 2012
- Messages
- 39
Hi all,
I've seen a number of threads about this but most are pretty old and don't really address what I'm looking for.
I have the code below which generates a string that I pass as SQL to create a query to bring the information into a subform. This works perfectly well, however I am now trying to adapt it for a different form and use just the ListBox part for over 25 different listboxes. I would like to make it as dynamic as possible, and short of writing the same code for every listbox and stringing them together, I've tried just about everything I can think of.
Does anyone have any ideas? Am I trying to be lazy or efficient?!
I've been playing around with this too but can't get it working!
I've seen a number of threads about this but most are pretty old and don't really address what I'm looking for.
I have the code below which generates a string that I pass as SQL to create a query to bring the information into a subform. This works perfectly well, however I am now trying to adapt it for a different form and use just the ListBox part for over 25 different listboxes. I would like to make it as dynamic as possible, and short of writing the same code for every listbox and stringing them together, I've tried just about everything I can think of.
Does anyone have any ideas? Am I trying to be lazy or efficient?!
Code:
Private Function BuildFilter() As Variant
'Declarations
Dim varWhere As Variant
Dim varColor As Variant
Dim varItem As Variant
Dim intIndex As Integer
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.
'Values
select1 = Forms![SearchForm]![Selection1].Value
select2 = Forms![SearchForm]![Selection2].Value
select3 = Forms![SearchForm]![Selection3].Value
'Creates Null Values
varWhere = Null
varColor = Null
'Searches in ComboBoxes
If Me.Search1 > "" Then
varWhere = varWhere & "[" & select1 & "] LIKE ""*" & Me.Search1 & "*"" AND "
End If
'Sets a date range
If Me.Date1 > "" Then
varWhere = varWhere & "[" & select2 & "] >= " & Format(Me.Date1 + 1, conJetDate) & " AND "
End If
If Me.Date2 > "" Then
varWhere = varWhere & "[" & select2 & "] < " & Format(Me.Date2 + 1, conJetDate) & " AND "
End If
'Sets the OR for a ListBox
For Each varItem In Me.ListSelect.ItemsSelected
varColor = varColor & "[" & select3 & "] = """ & Me.ListSelect.ItemData(varItem) & """ OR "
Next
'checks to see if there is a listbox selected
If IsNull(varColor) Then
Else
'Takes off the last OR
If Right(varColor, 4) = " OR " Then
varColor = Left(varColor, Len(varColor) - 4)
End If
'Puts in the parenthesis
varWhere = varWhere & "( " & varColor & " )"
End If
'Checks to see if any selections have been made
If IsNull(varWhere) Then
varWhere = ""
Else
'Adds it all together
varWhere = " WHERE " & varWhere
'Takes off the last AND
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
'End Result
BuildFilter = varWhere
End Function
I've been playing around with this too but can't get it working!
Code:
For Each ctrl In Me.ReportsForm.Controls
If ctrl.ControlType = acListBox Then
For Each varItem In ctrl.ItemsSelected
varWhere = varWhere & "ctrl.name = """ & ctrl.ItemData(varItem) & """ OR "
Next