Solved

Format number field to zero's

Posted on 2000-02-29
11
154 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:hcougar
11 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 2568450
newstring=Format("1234567", "000000000000")
0
 
LVL 22

Expert Comment

by:ture
ID: 2568458
hcougar,
Use the FORMAT function, as shown below:

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

Ture Magnusson
Karlstad, Sweden
0
 

Author Comment

by:hcougar
ID: 2568462
Crazyman, the field is going to vary.  It won't always be "1234567".  I am reading data from a txt file.
0
 

Author Comment

by:hcougar
ID: 2568469
ture, that is always going to give me 12345678.  The field will vary.
0
 
LVL 13

Expert Comment

by:crazyman
ID: 2568470
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:crazyman
ID: 2568478
it will work on any number string not just 1234567 ..just replace it.
0
 
LVL 13

Expert Comment

by:crazyman
ID: 2568491
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
0
 

Author Comment

by:hcougar
ID: 2568497
Maybe I'm not making this clear.  Here is the data I want to print out.

000000123456
000004521458
000215465542
000000001248
000045236554
000004542125
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2568530
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
0
 
LVL 13

Accepted Solution

by:
crazyman earned 50 total points
ID: 2568531
Here are the results i got using my function and passing the numbers above WITHOUT the zero's..


123456 --> 000000123456
4521458 -> 000004521458
215465542->000215465542
1248 ----->000000001248
45236554 ->000045236554
5452125--->000004542125


If you pass the second parameter as 12 each time my function will always return a 12 digit string with as many leading zero's as is needed, i cant see what is wrong.





0
 
LVL 1

Expert Comment

by:Kurvy
ID: 2568547
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)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need VBS version of Excel date conversion 4 65
Windows 10 start screen issues 9 52
vbModal 12 37
Excel Automation VBA 19 38
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now