Solved

Using Format in loops

Posted on 2001-07-29
7
135 Views
Last Modified: 2010-05-02
I have a loop

for a=1 to 5

printer.print format(firstvalue(a),"####.##"), format(secondvalue(a),"###.00"), format(thirdvalue(a)," ###.##")

next a

How do I get the following layout using format?

   7.89      0.34    6.12
1245.00   3452.56   34.00
    .01     70.00    1.34

etc

also variation for no decimal places in say firstvalue?

Thank you
0
Comment
Question by:bemson
  • 4
  • 3
7 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6332386
Use this mask: "####.00"
0
 

Author Comment

by:bemson
ID: 6332430
format shown after submission not the same as I typed it

using your suggestion for following values

78,2456,0.12
23.34,0.12,346

I get the following
- represents a space in case of what I type is not clear

78.00,2456.00,.12
23.34,.12----,346.00

- represents a space in case of what I type is not clear

I want the following


78.00,2456.00,----.12
23.34,----.12, 346.00

thank you

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6332470
You can use something like this:

Private Sub Command1_Click()
Debug.Print EMFormat(7.89, 7) & "," & EMFormat(0.34, 7) & "," & EMFormat(6.12, 7)
End Sub

Private Function EMFormat(ByVal psngValue As Single, ByVal pbytLen As Byte) As String
    EMFormat = Right$(Space$(pbytLen) & Format$(psngValue, "####.00"), pbytLen)
End Function
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:bemson
ID: 6332509
OK,

However I would like to vary the function capabilty
by changing 7 to 4 to get the following
for the first two values, ie 78 first line, 23 second line

Printer.Print EMFormat(78, 4), EMFormat(2456, 7), EMFormat(0.12, 7)
Printer.Print EMFormat(23.34, 4), EMFormat(0.12, 7), EMFormat(346, 7)

although 23 should be 24 if figure was 23.78

78,2456.00,---.12
23,----.12,346.00

what I get is:

8.00,2456.00,---.12
3.34,----.12,346.00

thank you
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 6332552
In this case, use this:

Private Sub Command1_Click()
Debug.Print EMFormat2(7.89, 4) & "," & EMFormat(0.34, 7) & "," & EMFormat(6.12, 7)
End Sub

Private Function EMFormat(ByVal psngValue As Single, ByVal pbytLen As Byte) As String
   EMFormat = Right$(Space$(pbytLen) & Format$(psngValue, "####.00"), pbytLen)
End Function

Private Function EMFormat2(ByVal psngValue As Single, ByVal pbytLen As Byte) As String
   EMFormat = Right$(Space$(pbytLen) & Format$(psngValue, "###0"), pbytLen)
End Function
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6332602
Why a B when I give you exactly what you want?
0
 

Author Comment

by:bemson
ID: 6336017


I now have to figure out how to use your function to perform a function that vb should have for all other combinations with both format and number of characters,
in any case by using your function, if Format2 and using "###0" then default obviously would be 4, so why specify pbytLen?

if I pass 346.3,2 to your function I get 46, no error that it should be 346

in dos with qbasic
the statement "print using "####", firstvalue
provided the spacing

I would have got " 346"



obviously whoever designed vb has missed the point of formatting

basically I (and others) need a single function to provide spaces to properly format numbers

So for every format I need I have to create a new function for every possibility, both for number formatting & how many numbers to display

If you have a better solution I am willing to give 100
points and an A

even so, thanks for your help
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

770 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