Leading Zeros

A

Ann

I have a text field called [txtDRS]. It's a maximum of four characters. If
the maximum isn't used I need to have leading zeros. I have tried everything
I can think of to do this and still have a maximum of zero but nothing works.
Can someone help me out? Thanks in advance.
 
K

Klatuu

Numeric fields will never have leading zeros. You either need to make it a
text field or pad a numeric field for display. I would suggest it be a text
field and validate the length when the data is entered.
 
A

Ann

I already tried that and it didn't work. When I type in 90A, that's what I
get.

RonaldoOneNil said:
Put 0000 in the format row of your textbox properties.

Ann said:
I have a text field called [txtDRS]. It's a maximum of four characters. If
the maximum isn't used I need to have leading zeros. I have tried everything
I can think of to do this and still have a maximum of zero but nothing works.
Can someone help me out? Thanks in advance.
 
A

Ann

It is a text field, not a numeric and has a field size of four. How do I
validate the length when it's entered?

Klatuu said:
Numeric fields will never have leading zeros. You either need to make it a
text field or pad a numeric field for display. I would suggest it be a text
field and validate the length when the data is entered.
--
Dave Hargis, Microsoft Access MVP


Ann said:
I have a text field called [txtDRS]. It's a maximum of four characters. If
the maximum isn't used I need to have leading zeros. I have tried everything
I can think of to do this and still have a maximum of zero but nothing works.
Can someone help me out? Thanks in advance.
 
J

John W. Vinson

I have a text field called [txtDRS]. It's a maximum of four characters. If
the maximum isn't used I need to have leading zeros. I have tried everything
I can think of to do this and still have a maximum of zero but nothing works.
Can someone help me out? Thanks in advance.

You can set an Input Mask of 0000 to force input of four numeric digits, or
@@@@ to force entry of four nonblank characters.

If you want to be able to type 8 <tab> and have the leading zeros
automatically filled in, put some code in the textbox's AfterUpdate event:

Private Sub txtDRS_AfterUpdate()
Me!txtDRS = Right("0000" & Me!txtDRS, 4)
End Sub
 
K

Klatuu

You can use the control's Before Update event to do the padding.

If Not Isnumeric(Me.txtDRS) Then
MsgBox "Only Numeric Values Allowed", vbExclamation
Cancel = True
Else
Me.txtDRS = String(4 - Len(Me.txtDRS), "0")
End If

--
Dave Hargis, Microsoft Access MVP


Ann said:
It is a text field, not a numeric and has a field size of four. How do I
validate the length when it's entered?

Klatuu said:
Numeric fields will never have leading zeros. You either need to make it a
text field or pad a numeric field for display. I would suggest it be a text
field and validate the length when the data is entered.
--
Dave Hargis, Microsoft Access MVP


Ann said:
I have a text field called [txtDRS]. It's a maximum of four characters. If
the maximum isn't used I need to have leading zeros. I have tried everything
I can think of to do this and still have a maximum of zero but nothing works.
Can someone help me out? Thanks in advance.
 
A

Ann

The code did exactly what I wanted it to. Thanks so much for the help.

John W. Vinson said:
I have a text field called [txtDRS]. It's a maximum of four characters. If
the maximum isn't used I need to have leading zeros. I have tried everything
I can think of to do this and still have a maximum of zero but nothing works.
Can someone help me out? Thanks in advance.

You can set an Input Mask of 0000 to force input of four numeric digits, or
@@@@ to force entry of four nonblank characters.

If you want to be able to type 8 <tab> and have the leading zeros
automatically filled in, put some code in the textbox's AfterUpdate event:

Private Sub txtDRS_AfterUpdate()
Me!txtDRS = Right("0000" & Me!txtDRS, 4)
End Sub
 
C

Chegu Tom

Question?

Shouldn't the function be in the before_update event. This way it will go
into an infinate After-Update loop, won't it?
 
J

John W. Vinson

Question?

Shouldn't the function be in the before_update event. This way it will go
into an infinate After-Update loop, won't it?

Paradoxically, no: if you want to change a control's value you must do so
AFTERupdate - if you do it before update, it triggers beforeupdate again and
loops.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top