Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pad Leftmost positions with 0s

Status
Not open for further replies.

Sillygirl

Programmer
May 3, 2002
80
US
I am reading an integer value from a SQL Server. When printing this value to a file, I need to take up 8 positions, with the actual value (more than likely taking up 1 -3 of the 8 positions), being in the rightmost position of the 8. Is there a command in Visual Basic which pads 0s to take up the leftover positions? Also, How do I get the value into the rightmost position.

Thankyou,

[morning]


 
Usually, I do something like...

Right("00000000" & CStr(IntegerValue), 8)

This will put 8 zero's in front of the number, and then take the 8 characters from the right.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
You could add 8 0's to the integer and then take the right 8 characters (so you would always have 8 even if the field was blank e.g.
Code:
YourVariable = Cstr(Right("00000000" & CStr(YourVariable),8))
Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Ooops, sorry George. Didn't mean to double post.

And I've stuck in another CStr() by accident in my haste [blush]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
HarleyQuinn,

Lemme guess. You just wanted to help SillyGirl in a timely fashion and didn't realize I had already posted, right? No need to apologize for that!

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Correct, cheers George [smile]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Thanks. I was doing something similar. But your code cleans up my stuff! Thanks so much boys!

[wink]


 
Another way is to use the Format() function.

Um, i'll try to use it in a sentence:[tt]
For x = 1 To 1000
Debug.Print Format(x, "00000000")
Next[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top