Remove Leading Comma

sharrell

Registered User.
Local time
Today, 14:55
Joined
Sep 20, 2002
Messages
14
I have a multi select list box. I copied the corresponding code from a functioning database. It runs correctly in the new database until LOCNCODE. I then get an "input box" to enter the LOCNCODE. LOCNCODE should be pulled from the list box. I have tried removing the statement, but then the code doesn't complete because of commas. I have searched these boards and the internet to no avail. Any ideas?
Code:
  ' if nothing is selected, display message and exit.
  If lstDataType.ItemsSelected.Count = 0 Then
    MsgBox "Select location(s) first"
    Exit Sub
  End If
  
  Dim SQL As String
  Dim sCriteria As String
  Dim varItem As Variant
   
  ' loop through list box selections.
  For Each varItem In lstDataType.ItemsSelected
    sCriteria = sCriteria & ",'" & lstDataType.ItemData(varItem) & "'"
  Next varItem
  
  ' build criteria string.
  For Each varItem In Me.lstDataType.ItemsSelected
      sLocation = sLocation & ",'" & Me.lstDataType.ItemData(varItem) & "'"
  Next varItem
  sLocation = Mid(sLocation, 2)
  ' remove leading comma.
  sLocation = " [LOCNCODE] in (" & sLocation & ")"

  
  ' build SQL Statement.
  SQL = " SELECT * " & _
      " FROM qryResults " & _
      " WHERE " & sLocation
  
  Set db = CurrentDb
  
  ' delete query qryDataType if exists.
  On Error Resume Next
  db.QueryDefs.Delete "qrySlowMov"
  On Error GoTo 0
  
  ' create and run query qryDataType.
  Set qDef = db.CreateQueryDef("qrySlowMov", SQL)
  
  DoCmd.OpenQuery "qrySlowMov"

PS This was originally written by someone here. Other than changing field names, I removed 2 Dim statements as they would not function in the new database (though they do in the other).
 
Last edited by a moderator:
LOCNCODE would need to be a field returned by qryResults. Is it?
 
No, it wasn't. I have added it now. (Qry is still running on my slow PC). I didn't get any errors or the parameter box. Thank you.
 

Users who are viewing this thread

Back
Top Bottom