mark curtis
Registered User.
- Local time
- Today, 15:41
- Joined
- Oct 9, 2000
- Messages
- 457
Dear all,
I have a form with four independent list boxes on. A report is generated based on the criteria selected from one or more of the list boxes.
I use the code below to first build a string for each list box and then piece together all the strings into one string for my SQL string:
This works fine if I only select from the first list but I get an error if I select only from other lists. I know that the error is the AND gets caught up in the string but I don't know how to structure the code?
' build criteria string for selected Task Period.
For Each varItem In Me.lstTaskPeriod.ItemsSelected
sTaskPeriod = sTaskPeriod & ",'" & Me.lstTaskPeriod.ItemData(varItem) & "'"
Next varItem
sTaskPeriod = Mid(sTaskPeriod, 2) ' remove leading comma.
sTaskPeriod = "TaskPeriod in (" & sTaskPeriod & ")"
MsgBox (sTaskPeriod)
' build criteria string for selected Site.
For Each varItem In Me.lstSite.ItemsSelected
sSite = sSite & ",'" & Me.lstSite.ItemData(varItem) & "'"
Next varItem
sSite = Mid(sSite, 2) ' remove leading comma.
sSite = "Site in (" & sSite & ")"
MsgBox (sSite)
' build criteria string for selected AssetPosition.
For Each varItem In Me.lstassetPosition.ItemsSelected
sAssetPosition = sAssetPosition & ",'" & Me.lstassetPosition.ItemData(varItem) & "'"
Next varItem
sAssetPosition = Mid(sAssetPosition, 2) ' remove leading comma.
sAssetPosition = " AssetPosition in (" & sAssetPosition & ")"
MsgBox (sAssetPosition)
'' build criteria string for selected AssetNo.
For Each varItem In Me.lstAssetNo.ItemsSelected
sAssetNo = sAssetNo & ",'" & Me.lstAssetNo.ItemData(varItem) & "'"
Next varItem
sAssetNo = Mid(sAssetNo, 2) ' remove leading comma.
sAssetNo = " tblAssetTask.AssetNo in (" & sAssetNo & ")"
MsgBox (sAssetNo)
If Me.lstTaskPeriod.ItemsSelected.Count > 0 Then
sCriteria = sTaskPeriod
End If
If Me.lstSite.ItemsSelected.Count > 0 Then
sCriteria = sCriteria & " AND " & sSite
End If
If Me.lstassetPosition.ItemsSelected.Count > 0 Then
sCriteria = sCriteria & " AND " & sAssetPosition
End If
If Me.lstassetPosition.ItemsSelected.Count > 0 Then
sCriteria = sCriteria & " AND " & sAssetNo
End If
Thanks
Mark
I have a form with four independent list boxes on. A report is generated based on the criteria selected from one or more of the list boxes.
I use the code below to first build a string for each list box and then piece together all the strings into one string for my SQL string:
This works fine if I only select from the first list but I get an error if I select only from other lists. I know that the error is the AND gets caught up in the string but I don't know how to structure the code?
' build criteria string for selected Task Period.
For Each varItem In Me.lstTaskPeriod.ItemsSelected
sTaskPeriod = sTaskPeriod & ",'" & Me.lstTaskPeriod.ItemData(varItem) & "'"
Next varItem
sTaskPeriod = Mid(sTaskPeriod, 2) ' remove leading comma.
sTaskPeriod = "TaskPeriod in (" & sTaskPeriod & ")"
MsgBox (sTaskPeriod)
' build criteria string for selected Site.
For Each varItem In Me.lstSite.ItemsSelected
sSite = sSite & ",'" & Me.lstSite.ItemData(varItem) & "'"
Next varItem
sSite = Mid(sSite, 2) ' remove leading comma.
sSite = "Site in (" & sSite & ")"
MsgBox (sSite)
' build criteria string for selected AssetPosition.
For Each varItem In Me.lstassetPosition.ItemsSelected
sAssetPosition = sAssetPosition & ",'" & Me.lstassetPosition.ItemData(varItem) & "'"
Next varItem
sAssetPosition = Mid(sAssetPosition, 2) ' remove leading comma.
sAssetPosition = " AssetPosition in (" & sAssetPosition & ")"
MsgBox (sAssetPosition)
'' build criteria string for selected AssetNo.
For Each varItem In Me.lstAssetNo.ItemsSelected
sAssetNo = sAssetNo & ",'" & Me.lstAssetNo.ItemData(varItem) & "'"
Next varItem
sAssetNo = Mid(sAssetNo, 2) ' remove leading comma.
sAssetNo = " tblAssetTask.AssetNo in (" & sAssetNo & ")"
MsgBox (sAssetNo)
If Me.lstTaskPeriod.ItemsSelected.Count > 0 Then
sCriteria = sTaskPeriod
End If
If Me.lstSite.ItemsSelected.Count > 0 Then
sCriteria = sCriteria & " AND " & sSite
End If
If Me.lstassetPosition.ItemsSelected.Count > 0 Then
sCriteria = sCriteria & " AND " & sAssetPosition
End If
If Me.lstassetPosition.ItemsSelected.Count > 0 Then
sCriteria = sCriteria & " AND " & sAssetNo
End If
Thanks
Mark