Question : Input Mask in Access

Ok guys, this should be easy but it is kicking my butt. I need to force my users to enter the correct format of an account number.
If the user enters 123456789, I need it to be displayed as 1234567-89. The last to digits should always have a dash before it. The first set of numbers can be different in length, so don't want to restrict that. Also will it be possible, if the user enters 0001225635, can I have access format it as 12256-35. Thus removing the leading zero's.

Thanks in advance.

Answer : Input Mask in Access

if u didnt want to change it from text to number, I can see why considering you use - then here is sample code that validates the entry, changes to format expected

So u have text0

Private Sub Text0_BeforeUpdate(Cancel As Integer)

    'ensure its in right format
    Dim sLeft As String
    Dim sRight As String
    Dim sNewVal As String
    Dim i As Integer
   
    If Trim$(Nz(Text0.Value, "")) = "" Then
        MsgBox "You must enter a value"
        Cancel = True
    Else
        'check - entered, if not add one in
        i = InStr(Text0.Value, "-")
        If i = 0 Then
            If Not IsNumeric(Text0.Value) Then
                MsgBox "Only numerics allowed"
                Cancel = True
            Else
                If Len(Text0.Value) > 2 Then
                    sNewVal = Val(Left$(Text0.Value, Len(Text0.Value) - 2)) & "-" & Right$(Text0.Value, 2)
                Else
                    sNewVal = "0-" & Format(Text0.Value, "00")
                End If
            End If
            i = InStr(sNewVal, "-")
        Else
            sNewVal = Text0.Value
        End If
       
        If Left$(sNewVal, "1") <> "-" Then
            sLeft = Left$(sNewVal, i - 1)
        Else
            sLeft = "0"
        End If
        sRight = Mid$(sNewVal, i + 1)
       
        If Not IsNumeric(sLeft) Then
            MsgBox "Numeric required"
            Cancel = True
        ElseIf Not IsNumeric(sRight) Then
            MsgBox "Numeric required"
            Cancel = True
        ElseIf Len(sRight) > 2 Then
            MsgBox "Only 2 numbers allowed after -"
            Cancel = True
        ElseIf Len(sLeft) > 7 Then
            MsgBox "Only 7 numbers allowed before -"
            Cancel = True
        Else
            sNewVal = Val(sLeft) & "-" & Format(sRight, "00")
        End If
    End If

    'set the new value on the tag property so that the afterupdate method can pick it up
    Text0.Tag = sNewVal
End Sub

Private Sub Text0_AfterUpdate()
    If Text0.Tag <> "" Then
        Text0.Value = Text0.Tag
        Text0.Tag = ""
    End If
End Sub
Random Solutions  
 
programming4us programming4us