Results 1 to 4 of 4

Thread: Tab alignment

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    13

    Tab alignment

    Hi,

    I really need some help aligning my records. I am using the code below to fill up my listbox. But since each field lenth varies from record to record the alignment gets messed up. How do i do it so that each field starts in the right place all the time and end at a certain point before the next field is listed so that they are all aligned.

    Thanks.

    Sub Fill_HistoryList()

    With rs

    List2.Clear

    Do While Not rs.EOF
    formatDate = rs!Date
    FormatTime = rs!Time
    formatDate = (Format(formatDate, "Medium Date"))
    FormatTime = (Format(FormatTime, "Medium Time"))

    List2.AddItem " " & rs!ID & " " & formatDate & " " &
    FormatTime & " " & rs!Name & " " & rs!History
    rs.MoveNext
    Loop

    End With

    End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Tab alignment

    This is what I would do (it will need a slightly wider listbox)

    VB Code:
    1. Sub Fill_HistoryList()
    2.  
    3.    'commented out this bit because you don't use it (& isn't really needed!)
    4. ' With rs                
    5.  
    6.  List2.Clear
    7.  
    8.   Do While Not rs.EOF
    9.                'No point putting these into the variables first
    10.     'formatDate = rs!Date
    11.     'FormatTime = rs!Time
    12.  
    13.     formatDate = (Format(rs!Date, "Medium Date"))
    14.     FormatTime = (Format(rs!Time, "Medium Time"))
    15.  
    16. 'NB: the _ at the end tells VB that it is really just one line (easier to read!)
    17.     List2.AddItem "   " & rs!ID & " " & vbtab _
    18.                         & formatDate & " " & vbtab _
    19.                         & FormatTime & " " & vbtab _
    20.                         & rs!Name & " " & vbtab _
    21.                         & rs!History
    22.     rs.MoveNext
    23.   Loop
    24.  
    25. ' End With
    26.  
    27. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    13
    Hi,

    I tried that but the results are almost the same.
    You see, the User name varies in lengh ie some people have short name while others have long names so the History that comes after that starts at different positions.

    Is there anyway, that i can make the name take up a fixed length before the history is displayed. I just want each colomn to start at a certain position and end at a certain position before the next field is displayed.

    Any ideas on how to do this.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    assuming the ID's are fixed length, something like this should do it:

    VB Code:
    1. List2.Clear
    2.  
    3.   max_len = [b]{length of longest string that can be returned for rs!Name}[/b]
    4.  
    5.   Do While Not rs.EOF
    6. ...
    7.     List2.AddItem "   " & rs!ID & " " & vbtab _
    8.                         & formatDate & " " & vbtab _
    9.                         & FormatTime & " " & vbtab _
    10.                         & rs!Name [b]& space ((max_len-rs!Name)*3) [/b]& " " & vbtab _
    11.                         & rs!History
    12. ...
    This isn't the most accurate way of doing it, but unless you only have a few rows of data it is the best you can do in a list box (you could always use a grid instead!)

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