Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


number formatting

Posted on 2000-04-09
Medium Priority
Last Modified: 2010-05-02
I wnat to print some text and a number in 2 columns with the number aligned right. How can I print the leading/trailing spaces (instead of zeros) while printing a number so that it's aligned right.

i want the output to be something like this...

xyz      $123.00
xxxxxx   $  5.00


aaa      $123.00
dasdasd    $5.00

Question by:naikv
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

Expert Comment

ID: 2698520
Function padstring(str1, str2 As String, length As Integer) As String
  Numspaces = length - (Len(str1) + Len(str2))
  padstring = str1 + Space$(Numspaces) + str2
End Function
Private Sub Command1_Click()
List1.AddItem (padstring("dave was here", "5.00", 40))
List1.AddItem (padstring("So was joe, really", "23.00", 40))
End Sub

Have fun!!

Expert Comment

ID: 2698576
DrDelphi -

Sorry - doesn't work.

Becuase of the "size" of the characters.

For example - the "S" and the "s" are different sizes, and therefore, there is an offset in the listbox.

I have tested this with both TrueType and non TT fonts with no success.

At first I wrote the exact same code, but found that my results weren't what I expected.


Author Comment

ID: 2698660
does'nt work due to different sizes of characters!
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 2698787
Can you change the font to be "Courier New" or "FixedSys" so that you can use fixed spacing?  Listboxes don't have a right-align feature, so without that I suspect you won't be able to do it too easily.

Can you use the RichTextBox control (Richtx32.ocx)?  This may help you, although I never have used it for this purpose.  Check out the RightMargin and SelAlignment properties.  This should work in the following cases:
  1) you don't have anything else on the line, and
  2) you always have the same number of digits to the right of the decimal. (All MS fonts have fixed-pitch digits; if you're using a non-MS font this may still be an issue for you.)

Hope this help,
LVL 14

Expert Comment

ID: 2699140
This Function will text align data into a single string that can be used for Printing or a Listbox.

1. Open a New Standard.Exe Project.
2. Add a Listbox (List1) to Form1.
3. Add a Picturebox (Picture1) to Form1.
4. Copy/Paste the following into the Form1 code window.
5. Press F5 to Run.

<----- Code Begin ----->

Option Explicit

Private Sub Form_Load()

   Form1.Move _
      Screen.Width * 0.1, _
      Screen.Height * 0.1, _
      Screen.Width * 0.8, _
      Screen.Height * 0.8
   ' Set Working Picture Box To Hidden
   Picture1.Visible = False

   Dim varArray As Variant
   Dim strReturn As String
   ' Set Column Alignment L=Left R=Right C=Center
   varArray = Array("L", "R", "C")
   strReturn = x_FormatOutput("Align", varArray)
   ' Set Font Data
   varArray = Array( _
      List1.FontBold, _
      List1.FontItalic, _
      List1.FontName, _
      List1.FontSize, _
      List1.FontStrikethru, _
   strReturn = x_FormatOutput("Font", varArray)
   ' Set Column Widths
   varArray = Array(500, 500, 500)
   strReturn = x_FormatOutput("Width", varArray)
   ' Process Headings
   varArray = Array( _
      "Left", _
      "Right", _
   List1.AddItem x_FormatOutput("Print", varArray)
   ' Process Detail
   Dim intIndex As Integer
   For intIndex = 1 To 20
      varArray = Array( _
         intIndex * 2, _
         intIndex * 30, _
         intIndex * 400)
      List1.AddItem x_FormatOutput("Print", varArray)
   Next intIndex

End Sub
Private Sub Form_Resize()
   List1.Move _
      ScaleWidth * 0.05, _
      ScaleHeight * 0.05, _
      ScaleWidth * 0.9, _
      ScaleHeight * 0.9

End Sub

Private Function x_FormatOutput _
(ByVal strCommand As String, _
ByVal varData As Variant) _
As String
   Static varAlign As Variant
   Static varWidth As Variant
   If strCommand = "Align" _
      varAlign = varData
      Exit Function
   End If
   If strCommand = "Font" _
      Picture1.FontBold = varData(0)
      Picture1.FontItalic = varData(1)
      Picture1.FontName = varData(2)
      Picture1.FontSize = varData(3)
      Picture1.FontStrikethru = varData(4)
      Picture1.FontUnderline = varData(5)
      Exit Function
   End If
   If strCommand = "Width" _
      varWidth = varData
      Exit Function
   End If
   If UBound(varData) <> UBound(varAlign) _
   Or UBound(varData) <> UBound(varWidth) _
      MsgBox "x_FormatOutput: Data Does NOT Match Formatting"
      Exit Function
   End If
   Dim lngIndex As Integer
   Dim lngLeft As Long
   Dim lngRight As Long
   Dim lngWidth As Long
   Dim strWork() As String
   ReDim strWork(UBound(varData))
   For lngIndex = 0 To UBound(varData)
      lngWidth = Picture1.TextWidth(varData(lngIndex))
      Select Case varAlign(lngIndex)
         Case "C" ' Center
            lngLeft = ((varWidth(lngIndex) - lngWidth) / 2) _
               / Picture1.TextWidth(" ")
            lngRight = ((varWidth(lngIndex) - lngWidth) - lngLeft) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = Space(lngLeft) _
               & varData(lngIndex) _
               & Space(lngRight)
         Case "R" ' Right
            lngLeft = (varWidth(lngIndex) - lngWidth) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = Space(lngLeft) _
               & varData(lngIndex)
         Case Else ' Left
            lngRight = (varWidth(lngIndex) - lngWidth) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = varData(lngIndex) _
               & Space(lngRight)
      End Select
   Next lngIndex
   x_FormatOutput = Join(strWork, " ")
End Function

<----- Code End ----->

Expert Comment

ID: 2699463
I use this method

While RSMetodi.EOF <> True
   RSMetodi!codice &  Space(20 - Len(Str(RSMetodi!codice))) & RSMetodi!dollars

I put a number of space caracter respect of the len of value the result is:


Space(20 - Len(Str(RSMetodi!codice)))

20 is a max len of your 'yz' or    
'xxxxxx' fileds
LVL 14

Expert Comment

ID: 2699911
Since you're talking about a listbox, try this...Add the following to a MODULE:

   Public Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
   Public Const LB_SETTABSTOPS = &H192

Then you can set up tabs in a listbox like this... This example sets up 3 columns:
   Dim Retcode As Long
   ReDim TabStop(1 To 3) As Long
   TabStop(1) = 100
   TabStop(2) = 200
   TabStop(3) = 300
   Retcode = SendMessageArray(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(1))

Then you can add items to the listbox in columns like this:

   List1.AddItem "Hello" & vbTab & "World" & vbTab & "Goodbye"

Hope this helps!

LVL 14

Expert Comment

ID: 2699918
By the way, make sure the listbox is wide enough to see the 3 column in the example I gave...

LVL 14

Expert Comment

ID: 2700968
Questioner wants a right justify in one of the columns.. as such.. a TextWidth and space padding have to be added somewhere.. <sigh>. You can grab something from my code if you like.. <smile>.
LVL 14

Accepted Solution

mcrider earned 40 total points
ID: 2703905
So try this example:

Start a new project, and add a listbox and a command button to Form1, the paste the following into the "Declarations Section" of Form1:

Private Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_SETTABSTOPS = &H192
Function RightJustify(ByVal Source As String, Size As Long) As String
    Do While Me.TextWidth(Source) < Size
        Source = " " + Source
    RightJustify = Source
End Function
Private Sub Command1_Click()
   Dim Retcode As Long
   ReDim TabStop(1 To 3) As Long
   TabStop(1) = 50
   TabStop(2) = 100
   TabStop(3) = 150
   Retcode = SendMessageArray(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(1))

    List1.AddItem "Hello" & vbTab & RightJustify("100.00", 1440) & vbTab & "Goodbye"
    List1.AddItem "Hello" & vbTab & RightJustify("1.05", 1440) & vbTab & "Goodbye"
    List1.AddItem "Hello" & vbTab & RightJustify("15.78", 1440) & vbTab & "Goodbye"
End Sub

LVL 14

Expert Comment

ID: 2703907
Alot of work for just 10 points... ;-)

LVL 14

Expert Comment

ID: 2704601
<laughing>.. Yeah, ain't it a b*tch.. <smile> and a <wink>
LVL 14

Expert Comment

ID: 2706129

Featured Post

Independent Software Vendors: 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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

610 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