Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Adding Leading Zeroes

Posted on 1999-07-14
Medium Priority
253 Views
Last Modified: 2012-05-04
My table has a text field which contains numeric data. The data starts off with 1....etc. My question is this: How can I get the data that reads 1..... to read 01.... That is how can I add a zero to the begining of all the data that starts w/a 1 ?? I assume I'll have to state some sort of special criteria in a query. I've already tried: New field name: 0+[old field name] with no luck.
0
Question by:MICHAED
[X]
###### 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
• 3
• 3
• 2
• +3
11 Comments

Author Comment

ID: 2000302
Edited text of question.
0

LVL 6

Expert Comment

ID: 2000303
Use the following function in a query and pass the column that reads as 1 to this function and the second argument does not affect you so True or false is OK.
This has to work.

Function FrmtNum(intNum%, Optional threedgts As Boolean) As Variant

On Error GoTo FrmtPymntReqLnNum_err

'* Modified here for 3 digit or 4 digit numbers. If the           *
'* optional argument is true then the funcction returns 3 digits. *
'******************************************************************

Select Case (Val(intNum))
Case 0
FrmtPymntReqLnNum = "0000"
Case 1 To 9
FrmtPymntReqLnNum = "000" & Val(intNum)
Case 10 To 99
FrmtPymntReqLnNum = "00" & Val(intNum)
Case 100 To 999
FrmtPymntReqLnNum = "0" & Val(intNum)
Case Else
FrmtPymntReqLnNum = Trim(Val(intNum))

End Select

If threedgts = True Then
FrmtPymntReqLnNum = Right(FrmtPymntReqLnNum, 3)
End If

FrmtPymntReqLnNum_exit:
Exit Function

FrmtPymntReqLnNum_err:
MsgBox Err & " " & Err.Description
Resume FrmtPymntReqLnNum_exit

End Function

0

LVL 7

Expert Comment

ID: 2000304
Use the Format Function:

Format([YourFieldName],"00")

lmerrell

0

LVL 6

Expert Comment

ID: 2000305
Sorry I changed the name of the function and forgot change it all over.

Disregard the previous function and use the following.

Function FrmtNum(intNum%, Optional threedgts As Boolean) As Variant

On Error GoTo FrmtPymntReqLnNum_err

'* Modified here for 3 digit or 4 digit numbers. If the           *
'* optional argument is true then the funcction returns 3 digits. *
'******************************************************************

Select Case (Val(intNum))
Case 0
FrmtNum= "0000"
Case 1 To 9
FrmtNum= "000" & Val(intNum)
Case 10 To 99
FrmtNum= "00" & Val(intNum)
Case 100 To 999
FrmtNum= "0" & Val(intNum)
Case Else
FrmtNum= Trim(Val(intNum))

End Select

If threedgts = True Then
FrmtNum= Right(FrmtNum, 3)
End If

FrmtPymntReqLnNum_exit:
Exit Function

FrmtPymntReqLnNum_err:
MsgBox Err & " " & Err.Description
Resume FrmtPymntReqLnNum_exit

End Function

0

LVL 1

Expert Comment

ID: 2000306
You can first change the text field to be a numeric fields like you said.  Or you can do it in your query by add a column with something like this

expr1:CLng([field]).
and order by expr1.

0

LVL 1

Expert Comment

ID: 2000307
Changed it for text field type.
0

Author Comment

ID: 2000308
Thank you devtha...that did the trick. Thanks to you also...Ghis68,Elad22, and Imerrell...Your help is most appreciated. To devtha: I don't have a green "proposed answer" box to click on and accept your answer...any suggestions ??
0

LVL 1

Expert Comment

ID: 2000309
Michaed,

I like simple code.  Do you try my suggestion ? .  Is it work for what you need. Clng([field1]) and order by this column in your query.
0

LVL 6

Accepted Solution

devtha earned 280 total points
ID: 2000310
I am Glad it helped.
Accept this as an answer.

0

Author Comment

ID: 2000311
Thanks everyone !!! To Ghis68...actually I did not try your code...I just tried devtha's code and it worked....I will try yours though...it's a lot smaller! Thanks again to everyone !!!
0

Expert Comment

ID: 2064837
Thanks!
0

## Featured Post

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are â€¦
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will diâ€¦
Whatâ€™s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macroâ€™s, and VBA code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a qâ€¦
###### Suggested Courses
Course of the Month7 days, 15 hours left to enroll

#### 715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.