Results 1 to 8 of 8

Thread: Join function (user types)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    269

    Join function (user types)

    Hi. I have a variable array of a type i made:

    (this is not waht i have... but it will do as an example)

    --------------------
    Type AllData
    Name as string
    LastName as String
    Age as Integer
    End Type

    Dim Employee(3) as AllData
    --------------------

    Now: I want to join all the names with "-" as delimeter
    I've tryed everything but can't make it, is it possible??

    Thanks

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

    Re: Join function (user types)

    VB Code:
    1. Dim lngA As Long, strNames As String
    2.  
    3. strNames = Employee(0).Name
    4. For lngA = 1 To UBound(Employee)
    5.     strNames = strNames & "-" & Employee(lngA).Name
    6. Next lngA
    Last edited by Merri; Mar 14th, 2006 at 11:57 AM.

  3. #3
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: Join function (user types)

    VB Code:
    1. Dim i as long
    2. Dim strNames as string
    3. strNames = ""
    4. For i = 0 to 3
    5.      strNames = strNames & Employee(i).Name & " " Employee(i).LastName & "-"
    6. Next i
    7. strNames = Left(strNames, Len(strNames) - 1)

    HTH

    Edit: Merri's is a little Nicer. Just a little...not much really...lol
    Using VB6 or VB.net 2008 with .net 3.5
    "Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    269

    Re: Join function (user types)

    thanks. good idea....
    but now suppose i want to join lot of stuff, so i need to make a function in a module.
    Also i dont know how many big the array wil be.

    So a parameter should be the array to join and then use LBound and UBound, but i cant declare a parameter as an Array and use the L/UBound....
    how do i do that??

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Join function (user types)

    And yet another way...

    Code:
    Dim i as long
    Dim strNames as string
    strNames = ""
    strDelim = ""
    For i = 0 to 3
         strNames = strNames & strDelim & Employee(i).Name & " " & Employee(i).LastName
         If i = 0 Then strDelim = "-"
    Next i
    ' strNames = Left(strNames, Len(strNames) - 1) ** don't need this anymore

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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

    Re: Join function (user types)

    Quote Originally Posted by Kanbei
    So a parameter should be the array to join and then use LBound and UBound, but i cant declare a parameter as an Array and use the L/UBound....
    how do i do that??
    Here is a function for the job:

    VB Code:
    1. Public Function EmployeeJoin(Employee As udtMyType, Separator As String) As String
    2.     Dim strOut As String, lngA As Long
    3.     ' check for existing items
    4.     If (Not Employee) = True Then Exit Function
    5.     ' insert the first item
    6.     strOut = Employee(LBound(Employee)).Name
    7.     ' insert the remainder
    8.     For lngA = LBound(Employee) + 1 To UBound(Employee)
    9.         strOut = strOut & Separator & Employee(lngA).Name
    10.     Next lngA
    11.     EmployeeJoin = strOut
    12. End Function

    For more information on checking if array is initialized, see the FAQ for How do I check if array has been initialized?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Posts
    269

    Re: Join function (user types)

    thanks, ill chekc all the ideas you posted and post again

    And sorry space_monkey i started to write the 2nd post b4 you posted yours

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Join function (user types)

    This should be faster than the previous examples when joining lots of records.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type AllData
    4.     Name As String
    5.     LastName As String
    6.     Age As Integer
    7. End Type
    8.  
    9. Private Employee(3) As AllData
    10.  
    11. Private Function JoinAllData(Data() As AllData, ByVal Separator As String) As String
    12.     Dim K As Long, StrOut As String, Pos As Long
    13.     Dim LenName As Long, LenSep As Long
    14.    
    15.     LenSep = Len(Separator)
    16.     StrOut = String(5000, 0)
    17.     Pos = 1
    18.    
    19.     For K = LBound(Data) To UBound(Data)
    20.         LenName = Len(Data(K).Name)
    21.        
    22.         If (Pos + LenSep + LenName) > Len(StrOut) Then
    23.             StrOut = StrOut & String(5000, 0)
    24.         End If
    25.        
    26.         Mid$(StrOut, Pos, LenSep) = Separator
    27.         Pos = Pos + LenSep
    28.        
    29.         Mid$(StrOut, Pos, LenName) = Data(K).Name
    30.         Pos = Pos + LenName
    31.     Next K
    32.    
    33.     JoinAllData = Mid$(StrOut, LenSep + 1, Pos - LenSep - 1)
    34. End Function
    35.  
    36. Private Sub Form_Load()
    37.     Employee(0).Name = "faskdjfhsdkjfh"
    38.     Employee(1).Name = "nbnbmnbmnbmnb"
    39.     Employee(2).Name = "asdf"
    40.     Employee(3).Name = "23423423"
    41.    
    42.     Debug.Print JoinAllData(Employee, "-")
    43. End Sub

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