Link to home
Start Free TrialLog in
Avatar of hcougar
hcougar

asked on

Format number field to zero's

I am currently printing out a field that is 12 characters long.  How do I need to write the code so that there are leading zeros in the field.  Basically, what is the code to make it look like the following:

1234567  ==> 000001234567

Always starts off with zeros on the left side and amount on left could vary but always 12 characters, could also be:

123456789 ==> 000123456789
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

newstring=Format("1234567", "000000000000")
Avatar of ture
ture

hcougar,
Use the FORMAT function, as shown below:

  Dim x As Double
  x = 12345678
  Text1.Text = Format(x, "000000000000")

Ture Magnusson
Karlstad, Sweden
Avatar of hcougar

ASKER

Crazyman, the field is going to vary.  It won't always be "1234567".  I am reading data from a txt file.
Avatar of hcougar

ASKER

ture, that is always going to give me 12345678.  The field will vary.
So in real terms you could have a small function.

Public function ChangeString(OldString as string,NumOfZeros as integer)as string

ChangeString=format(oldString,string(numofzeros,chr(48)))
end function


then use it like

Private Sub Command1_Click()
    MsgBox ChangeString("1234567", 12)
End Sub
it will work on any number string not just 1234567 ..just replace it.
1234567 was meant as an example, it will work on any string of numbers you just need to pass what ever your numbers are in place of 1234567
Avatar of hcougar

ASKER

Maybe I'm not making this clear.  Here is the data I want to print out.

000000123456
000004521458
000215465542
000000001248
000045236554
000004542125
hcougar,
The answer both ture and crazyman have gave you, is the right answer.
You should use the Format function for what you want

<textout>=Format(<Value>,"000000000000")

Gordon
ASKER CERTIFIED SOLUTION
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Function AlterString(sText As String) As String
Dim strText As String
Dim lngLen As Long

strText = sText
lngLen = Len(strText)
AlterString = String(12 - lngLen, "0") & strText


End Function

call the function as follows

text1.text=alterstring(text1.text)