position of variables in a list

Posted on 2003-03-16
Medium Priority
Last Modified: 2010-05-01

I've a small problem.
I'm making a very simple cashregister program.  It this program, I use a list to add items.
Each item in that list have to contain 3 variables.
For example:

3   Cola                                  4,5
5   Fanta                                 6,5

'and so on

As in the example the '3' is stored in a variable1, the 'cola' in variable2 and the '4,5' in variable3.

Now I do the following:

List1.AddItem variable1 & vbTab & variable2 & vbTab & vbTab & vbTab & vbTab & vbTab & variable3

The problem is, when the characters or numbers stored in some of these variables ar longer I get this:

3   Cola                                  4,5
5   Fanta                                 6,5
3   The best beer                                9,5
5   Fristi                                8,0

As you can see in the 3th line, the 9,5 sticks out.  Does anyone have the solution to this problem.
(keep in mind, it's a very easy program so I dont want to use (or connect to) a database)
For example: Is it possible to declare each position of each variable, starting from the left edge of the list??)

Thanks in advance,

Question by:vincentregent
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

Sweat earned 240 total points
ID: 8147300

Easy "problem" to fix.

To properly align the three variables, you need to insure that all of the rows added to the listbox are of the same length.  So when you are dealing with the "3" or Variable1, then if you have more than ten rows and a value of two characters, then the "3" must be "3 ".

The same is true for the other two variables.  You'll need to pick a length, say 20 characters and insure that each variable2 is the same length.

Consider the following method:

    Variable1 = "Cola"
    Variable1 = Variable1 & Space$(20 - Len(Variable1))
    would become: "Cola                "


    Variable1 = "The best beer"
    Variable1 = Variable1 & Space$(20 - Len(Variable1))
    would become: "The best beer       "

Then when you build the Additem line, each line will be of the same length and therefore lined up.

Let me know if this isn't clear.



Author Comment

ID: 8147440
Thanks a lot Sweat !!!!!!


Expert Comment

ID: 8147480

If you need to stay with the Tab as a separator, you could use a function like this:

Private Sub Command1_Click()

a = 3: b = "The best beer": c = "9,5"
List1.AddItem CheckWidth(a, "1234") & CheckWidth(b, "1234567890") & c

a = 3: b = "Cola": c = "4.5"
List1.AddItem CheckWidth(a, "1234") & CheckWidth(b, "1234567890") & c

a = 5: b = "Fanta": c = "6, 5"
List1.AddItem CheckWidth(a, "1234") & CheckWidth(b, "1234567890") & c

End Sub
Function CheckWidth(strT, strMaxWidth) As String
 'We put at least one vbTab
 strMaxWidth = strMaxWidth & vbTab
 Me.Font = List1.Font
 Me.FontSize = List1.FontSize
 If TextWidth(strT & vbTab) > TextWidth(strMaxWidth) Then
  MsgBox "Text too long :" & strT
separator = ""
Do While TextWidth(strT & separator) < TextWidth(strMaxWidth)
 separator = separator & vbTab
 Do While TextWidth(strT & separator) < TextWidth(strMaxWidth)
 If separator = "" Then Exit Do
 separator = Mid(separator, 2)
End If

CheckWidth = strT & separator
End Function

Author Comment

ID: 8152448
Thanx for your help DocM.


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question