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

Format or Input Mask Text Leading Zero 1

Status
Not open for further replies.

LOKIDOG

Technical User
Apr 25, 2001
150
US
I want to set up a text field so that a leading zero is automatically entered in the data. The field will have 2 characters. If you enter 1 the it will default to 01 if you enter 23 is will just be 23.

I know I've seen this before and seems like something that is often needed. Any suggestions.
 
In the AfterUpdate event of the text box, put the following code:
Code:
Me![TextboxName] = Format(Me![TextboxName], "00")

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thanks, I was actually trying to put it in the design of the table. But this will work.
 
Another field I'm trying to work with - but again with the Table and the input mask is in the form of two numbers then a letter

I would really like to be able to have people enter 5E or 15E and then it makes it into 05E and 15E but...

It requires that the e be entered - as a letter. I'm not seeing any way to do this. Even better would be to make the e part only allow E,S,N, and W. I could separate the number from the letter into separate fields.... I know...but is there a way to accomplish this with Format statements, Validation Rules, and or Input Masks. Help for this is rather poorly documented - and I have several books on MS Access as well.
 
Depends on how much coding you want to do. Using the form, you could use the AfterUpdate event again. You would do several steps...check length, check letters/numbers, and format as necessary. Something like this should get you started if you want to do it through the form:

Code:
    Dim strNumbers As String
    Dim strLetter As String
    
    Select Case Len(Me.Text0)
        Case 2
            strNumbers = Left(Me.Text0, 1)
            strLetter = Right(Me.Text0, 1)
        Case 3
            strNumbers = Left(Me.Text0, 2)
            strLetter = Right(Me.Text0, 1)
        Case 0
            Exit Sub
        Case Else
            MsgBox "The value entered is not valid", vbCritical, "Invalid"
            Me.Text0 = ""
            Exit Sub
    End Select
    
    If IsNumeric(strNumbers) Then
        If CInt(strNumbers) < 10 Then
            strNumbers = "0" & strNumbers
        End If
    Else
        MsgBox "The value entered is not valid", vbCritical, "Invalid"
        Me.Text0 = ""
        Exit Sub
    End If

    If strLetter = "E" Or strLetter = "S" Or strLetter = "N" Or strLetter = "W" Then
        strLetter = StrConv(strLetter, vbUpperCase)
    Else
        MsgBox "The value entered is not valid", vbCritical, "Invalid"
        Me.Text0 = ""
        Exit Sub
    End If
    
    Me.Text0 = strNumbers & strLetter

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
I implemented this code into 4 fields in my form - works great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top