help getting leading zero to show

  • Thread starter Thread starter Karen
  • Start date Start date
K

Karen

I have the following code in the onfocus event of a control whose
datasource is a 20 character text field...

Dim mylot
mylot = DatePart("ww", Date, 1, 1) & Right(Format(Date, "yy"), 1)
Me![lotno] = mylot

currently mylot is '107'
I need it to show me a leading zero when the week is less than 10. Can
anyone help me?

Thanks,

Karen S
 
I actually don't know how you get "107" since the year only contributes "7"
and not "07" due to the Right function???

Try:

mylot = Format(DatePart("ww", Date), "00") & Format(Date, "yy")

This only works if the Control [lotno] is bound to a Text Field. If it is
bound to a Numeric Field, you will need to use the Format Property of the
Control to display the leading zero.
 
If DatePart("ww",Date) < 10 Then
mylot = "0" & DatePart("ww",Date) & Right(Date,1)
Else
mylot = DatePart("ww",Date) & Right(Date,1)
End If

There are any number of other approaches you could use. This is one way.
You don't need the 1,1 at the end of DatePart since those are the default
values. Also, the rightmost digit of the date is almost surely the last
digit of the year, so you can leave out the Format function.
Having said that, it looks as if mylot will change from one day to the next
if the text box gets the focus. Is that the intention? Also, on any given
day mylot will be the same for all records. What is the connection between
mylot and the 20-character text field?
 
Oops. I missed that part about the 07 in my reply.

Van T. Dinh said:
I actually don't know how you get "107" since the year only contributes "7"
and not "07" due to the Right function???

Try:

mylot = Format(DatePart("ww", Date), "00") & Format(Date, "yy")

This only works if the Control [lotno] is bound to a Text Field. If it is
bound to a Numeric Field, you will need to use the Format Property of the
Control to display the leading zero.

--
HTH
Van T. Dinh
MVP (Access)



Karen said:
I have the following code in the onfocus event of a control whose
datasource is a 20 character text field...

Dim mylot
mylot = DatePart("ww", Date, 1, 1) & Right(Format(Date, "yy"), 1)
Me![lotno] = mylot

currently mylot is '107'
I need it to show me a leading zero when the week is less than 10. Can
anyone help me?

Thanks,

Karen S
 
My typo - I was getting '17'.

I used the following...

mylot = Format(DatePart("ww", Date), "00") & right(Format(Date, "yy"),
1)

and get the correctly formatted lot number of '017'.

Thanks.
I actually don't know how you get "107" since the year only contributes "7"
and not "07" due to the Right function???

Try:

mylot = Format(DatePart("ww", Date), "00") & Format(Date, "yy")

This only works if the Control [lotno] is bound to a Text Field. If it is
bound to a Numeric Field, you will need to use the Format Property of the
Control to display the leading zero.

--
HTH
Van T. Dinh
MVP (Access)



Karen said:
I have the following code in the onfocus event of a control whose
datasource is a 20 character text field...

Dim mylot
mylot = DatePart("ww", Date, 1, 1) & Right(Format(Date, "yy"), 1)
Me![lotno] = mylot

currently mylot is '107'
I need it to show me a leading zero when the week is less than 10. Can
anyone help me?

Thanks,

Karen S
 
It is intended that all products made during the week get the same lot
number. mylot only changes from week to week.

mylot is filled in as the value for the 20 character text field.

Karen
If DatePart("ww",Date) < 10 Then
mylot = "0" & DatePart("ww",Date) & Right(Date,1)
Else
mylot = DatePart("ww",Date) & Right(Date,1)
End If

There are any number of other approaches you could use. This is one way.
You don't need the 1,1 at the end of DatePart since those are the default
values. Also, the rightmost digit of the date is almost surely the last
digit of the year, so you can leave out the Format function.
Having said that, it looks as if mylot will change from one day to the next
if the text box gets the focus. Is that the intention? Also, on any given
day mylot will be the same for all records. What is the connection between
mylot and the 20-character text field?

Karen said:
I have the following code in the onfocus event of a control whose
datasource is a 20 character text field...

Dim mylot
mylot = DatePart("ww", Date, 1, 1) & Right(Format(Date, "yy"), 1)
Me![lotno] = mylot

currently mylot is '107'
I need it to show me a leading zero when the week is less than 10. Can
anyone help me?

Thanks,

Karen S
 
It will also change if you revisit an old record and tab or click into that
text box. Let me suggest that the code could be included in the form's
Current event after testing for a new record:
If Me.NewRecord Then
mylot = Format(DatePart("ww", Date), "00") & Right(Date, 1)
End If

Karen said:
It is intended that all products made during the week get the same lot
number. mylot only changes from week to week.

mylot is filled in as the value for the 20 character text field.

Karen
If DatePart("ww",Date) < 10 Then
mylot = "0" & DatePart("ww",Date) & Right(Date,1)
Else
mylot = DatePart("ww",Date) & Right(Date,1)
End If

There are any number of other approaches you could use. This is one way.
You don't need the 1,1 at the end of DatePart since those are the default
values. Also, the rightmost digit of the date is almost surely the last
digit of the year, so you can leave out the Format function.
Having said that, it looks as if mylot will change from one day to the
next
if the text box gets the focus. Is that the intention? Also, on any
given
day mylot will be the same for all records. What is the connection
between
mylot and the 20-character text field?

Karen said:
I have the following code in the onfocus event of a control whose
datasource is a 20 character text field...

Dim mylot
mylot = DatePart("ww", Date, 1, 1) & Right(Format(Date, "yy"), 1)
Me![lotno] = mylot

currently mylot is '107'
I need it to show me a leading zero when the week is less than 10. Can
anyone help me?

Thanks,

Karen S
 
okay, thanks. I see what you mean. It doesn't really apply to this
because I have the app written so that the users select the record they
are working on from a list form and then enter the QC information on
the QC form (where the lot number control is) and save it. Once it has
been QC'd, they can't get the record back on the form again to edit it.

Karen


It will also change if you revisit an old record and tab or click into that
text box. Let me suggest that the code could be included in the form's
Current event after testing for a new record:
If Me.NewRecord Then
mylot = Format(DatePart("ww", Date), "00") & Right(Date, 1)
End If

Karen said:
It is intended that all products made during the week get the same lot
number. mylot only changes from week to week.

mylot is filled in as the value for the 20 character text field.

Karen
If DatePart("ww",Date) < 10 Then
mylot = "0" & DatePart("ww",Date) & Right(Date,1)
Else
mylot = DatePart("ww",Date) & Right(Date,1)
End If

There are any number of other approaches you could use. This is one way.
You don't need the 1,1 at the end of DatePart since those are the default
values. Also, the rightmost digit of the date is almost surely the last
digit of the year, so you can leave out the Format function.
Having said that, it looks as if mylot will change from one day to the
next
if the text box gets the focus. Is that the intention? Also, on any
given
day mylot will be the same for all records. What is the connection
between
mylot and the 20-character text field?

I have the following code in the onfocus event of a control whose
datasource is a 20 character text field...

Dim mylot
mylot = DatePart("ww", Date, 1, 1) & Right(Format(Date, "yy"), 1)
Me![lotno] = mylot

currently mylot is '107'
I need it to show me a leading zero when the week is less than 10. Can
anyone help me?

Thanks,

Karen S
 

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

Back
Top