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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Automatically Fill Preceding Digits In Lookup Form 2

Status
Not open for further replies.

redbase

Technical User
Nov 21, 2002
21
GB
I have a lookup form made in access where users can search for specific records in a separate database. The two reference fields each consist of eight digit numbers. Any of these numbers less than 10000000 must have preceding zeros typed into search form, for example 00000345.

I was wondering if it is possible for users just to be able to type in 345 and for the preceding zeros to be automatically added based on the fact the numbers will always be 8 digits long.

Please can anyone provide me with some guidance? I have looked at a few ways round the problem but not managed to solve it.

Many thanks

Red
 
Just tried this out and it seems to give what you want. In the AfterUpdate event of ypur reference box, which I've called txtRef, try this:

Do Until Len(Me.txtRef) = 8
Me.txtRef = "0" & Me.txtRef
Loop

HTH

Nigel
Didn't someone say work is supposed to be fun? They didn't have computers then I guess....
 
If you want something more generic you could use this function...

This will pad a string with a particular character to a particular length.

Public Function gstrPadLeft(ByVal strString As String, ByVal intToLength As Integer, ByVal strWithChar As String) As String

gstrPadLeft = String$(intToLength - Len(strString), strWithChar) & strString

End Function

When you call the function the first parameter is the string you want to add to then second is the length you want it to be the third is the character to pad out with.
Example below returns "0000000345"


Sub mTest()
MsgBox gstrPadLeft("345", 10, "0")
End Sub

There are two ways to write error-free programs; only the third one works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top