Results 1 to 12 of 12

Thread: [RESOLVED] Concatenating in a loop

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Resolved [RESOLVED] Concatenating in a loop

    I loop the recordset and add "," between the numbers.
    Code:
    Dim sn As String
    
    rsTemp.MoveFirst
    While Not rsTemp.EOF
        sn = sn + "," + Format$(rsTemp("SampleNumber"))       
        rsTemp.MoveNext
    Wend
    This = ",123,456,789" and then i do this to remove the first ","

    Code:
    Dim snF As String
    
    snF = "'" & Mid$(sn, 2, Len(sn)) & "'"
    This = "123,456,789".

    Is there a better way to add the "," between the numbers in the loop so i dont have to use the Mid$? or what i have is ok?

  2. #2
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Concatenating in a loop

    Another way to cut off the beginning:
    Right$(sn, Len(sn) - 1)
    Not really that much better

  3. #3
    Member
    Join Date
    Nov 2008
    Posts
    55

    Re: Concatenating in a loop

    This may be more appropriate........

    Dim sn As String

    rsTemp.MoveFirst
    While Not rsTemp.EOF
    sn = sn + Format$(rsTemp("SampleNumber"))
    rsTemp.MoveNext
    Wend

    sn = format(sn, "##,##")

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Concatenating in a loop

    One option you have is to put a 'flag' (a boolean variable) in your loop that it is entering it the first time though I still prefer your current method since checking the flag for each iteration of the loop would entail performance degradation specially if you will be running a very long loop...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Concatenating in a loop

    Code:
    Dim sn As String
    
    rsTemp.MoveFirst
    Do Until rsTemp.EOF
        If LenB(sn) Then
            sn = sn & "," & rsTemp("SampleNumber")
        Else
            sn = rsTemp("SampleNumber")
        End If
        rsTemp.MoveNext
    Loop
    Checking for length is blazingly fast. Concatenating strings this way is slow. However, it is very unlikely this needs to be optimized in any way: I'm only providing a slightly cleaner code here.
    Last edited by Merri; Nov 8th, 2008 at 06:06 AM.

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Concatenating in a loop

    An ADODB recordset also has a GetString method that would automatically retrieve you the recordset in a defined format just like what you want, have a search!

    A sample of which is...

    Code:
    GetString(adClipString,-1, ",", ",", "(NULL)")
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    Member
    Join Date
    Aug 2008
    Posts
    50

    Re: Concatenating in a loop

    vb Code:
    1. Dim sn As String
    2.  
    3. rsTemp.MoveFirst
    4. While Not rsTemp.EOF
    5.     sn = sn + Format$(rsTemp("SampleNumber"))     + ","  
    6.     rsTemp.MoveNext
    7. Wend

    Put the comma at the end of the line and it should help

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Concatenating in a loop

    Quote Originally Posted by Tony Aston
    vb Code:
    1. Dim sn As String
    2.  
    3. rsTemp.MoveFirst
    4. While Not rsTemp.EOF
    5.     sn = sn + Format$(rsTemp("SampleNumber"))     + ","  
    6.     rsTemp.MoveNext
    7. Wend

    Put the comma at the end of the line and it should help
    He would then have to remove the trailing comma?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9
    Member
    Join Date
    Aug 2008
    Posts
    50

    Re: Concatenating in a loop

    To remove the trailing comma

    sn = left$(sn,len(sn)-1)

    Tony

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Concatenating in a loop

    Quote Originally Posted by Tony Aston
    To remove the trailing comma

    sn = left$(sn,len(sn)-1)

    Tony
    You just reversed the thread starter's current method...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    Member
    Join Date
    Aug 2008
    Posts
    50

    Re: Concatenating in a loop

    Yes, you are correct. I made the comment in haste.

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Concatenating in a loop

    Define the delimiter as in post #6. This is comparable to joining elements of string arrays on a given deilmiter, e.g. Join(strArray, ",")

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