Results 1 to 3 of 3

Thread: [RESOLVED] Listbox - trailing comma for multiple records

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Resolved [RESOLVED] Listbox - trailing comma for multiple records

    I have the following code:

    vb Code:
    1. strSQL = strSQL & "WHERE MerchantID in ("
    2.         For f = 0 To lstMerchants.ListCount - 1
    3.        
    4.         If lstMerchants.Selected(f) Then
    5.                     strSQL = strSQL & (lstMerchants.ItemData(f)) & ","
    6.            
    7.             End If
    8.        
    9.         Next f
    10.         strSQL = strSQL & ")"

    At the moment, it lists merchants in a listbox and allows the user can select as many merchants as desired. It worked well in Access and there was no probs, but when trying against a SQL database the trailing comma throws the query. If the user selects one record, then no comma, and also shouldn't be a comma on the last selected merchant. I've tried SelCount but couldn't figure out the best way to use it.

    I'm sure its simple but whats the best way to counteract this problem?

    Thanks in advance!

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Listbox - trailing comma for multiple records

    This is what I use somewhere. Just always add a comma even for one selection and then get rid of it.

    Code:
    ilen = Len(strUpdateClause)
     strUpdateClause = Mid(strUpdateClause, 1, InStrRev(strUpdateClause, ",") - 1) & " " & _
     Mid(strUpdateClause, InStrRev(strUpdateClause, ",") + 1, ilen - InStrRev(strUpdateClause, ","))

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2008
    Posts
    40

    Re: Listbox - trailing comma for multiple records

    Excellent piece of code. Worked like a charm!

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width