Prefill Coding Error

S

SITCFanTN

I have a form that I have a few data entry fields prefill the last text
entered. For example, there is a field for Zip Code. Once the zip code is
entered, and the record saved, the after update command retains the zip code
entered. I use the code of

Private Sub Zip_AfterUpdate()

Me.Zip.DafaultValue = Me.Zip

End Sub

My problem is I have one of the fields that is not retaining the data
entered. Here is the code I'm using

Privagte Sub AssignedChap_AfterUpdate()

Me.AssignedChap.DefaultValue = Me.AssignedChap

End Sub

In the data entry field on the form, the text, "#Name?" displays instead of
the text previously entered. What does this mean and why is my text not
displaying.

As always I appreciate your help, thank you.
 
B

BruceM via AccessMonster.com

Does the code compile?

I would give the text boxes different names than the fields, to make it
clearer if nothing else. Perhaps you are trying to apply DefaultValue to a
field rather than a text box?
 
M

Marshall Barton

SITCFanTN said:
I have a form that I have a few data entry fields prefill the last text
entered. For example, there is a field for Zip Code. Once the zip code is
entered, and the record saved, the after update command retains the zip code
entered. I use the code of

Private Sub Zip_AfterUpdate()

Me.Zip.DafaultValue = Me.Zip

End Sub

My problem is I have one of the fields that is not retaining the data
entered. Here is the code I'm using

Privagte Sub AssignedChap_AfterUpdate()

Me.AssignedChap.DefaultValue = Me.AssignedChap

End Sub

In the data entry field on the form, the text, "#Name?" displays instead of
the text previously entered. What does this mean and why is my text not
displaying.


Because the DefaultValue property is is a string that will
be evaluated as an expression when it is applied to a new
record, you need to enclose the value in quotes so Access
can understand what you want it to do:

Me.AssignedChap.DefaultValue = """" & Me.AssignedChap & """"

While that is not strictly required for numeric values (e.g.
12345), it is still a good idea.

For date values, because a machine's regional date format
setting can be almost anything, you should format the
datevalue to an unambiguous style to make sure Access
interprets it the way you intended. E.g.

Me.AssignedChap.DefaultValue = Format(Me.AssignedChap,
"\#yyyy-m-d\#")
 
S

SITCFanTN

The value is text, does that matter?

Marshall Barton said:
Because the DefaultValue property is is a string that will
be evaluated as an expression when it is applied to a new
record, you need to enclose the value in quotes so Access
can understand what you want it to do:

Me.AssignedChap.DefaultValue = """" & Me.AssignedChap & """"

While that is not strictly required for numeric values (e.g.
12345), it is still a good idea.

For date values, because a machine's regional date format
setting can be almost anything, you should format the
datevalue to an unambiguous style to make sure Access
interprets it the way you intended. E.g.

Me.AssignedChap.DefaultValue = Format(Me.AssignedChap,
"\#yyyy-m-d\#")
 
S

SITCFanTN

This didn't work Marshall, I put """ just like you instructed. The prefill
was my code, not the data I entered before the record was saved. I don't
understand why this code works in 3 other form fields but not this one. Do
you have any other suggestions. The data entry that I'm trying to have
prefill from the previous record is a name, both first and last name. Is
that the issue? Thanks again
 
M

Marshall Barton

If you coded what you posted, you have the wrong number of
quotes. It's four quotes in both places.

It doesn't matter what the value is (unless it contains a ")

If you leave out the quotes, you will get #Name unless the
value is a number, which is probably what you have in the
other forms.
 
S

SITCFanTN

Hi Again Marshall,

I'm still confused, the assignedchap is text, not numeric. I'm still
getting the error "#Name?". Here is the code I'm currently using.

Private Sub AssignedChap_AfterUpdate()

Me.AssignedChap.DefaultValue = " Me.AssignedChap "

End Sub

I've tried these variations and they are not prefilling the previous data
entry either.

Me.AssignedChap.DefaultValue = """ Me.AssignedChap """
Me.AssignedChap.DefaultValue = """ & Me.AssignedChap & """

The data entry is a text name, for example it would be Tom Jones....yet when
I save the form the name Tom Jones is not displaying in the form field when
it is reloaded. Is there another way to accomplish the prefill?

Thank you for your patience while I learn this.

Marshall Barton said:
If you coded what you posted, you have the wrong number of
quotes. It's four quotes in both places.

It doesn't matter what the value is (unless it contains a ")

If you leave out the quotes, you will get #Name unless the
value is a number, which is probably what you have in the
other forms.
--
Marsh
MVP [MS Access]

This didn't work Marshall, I put """ just like you instructed. The prefill
was my code, not the data I entered before the record was saved. I don't
understand why this code works in 3 other form fields but not this one. Do
you have any other suggestions. The data entry that I'm trying to have
prefill from the previous record is a name, both first and last name. Is
that the issue?
.
 
B

BruceM via AccessMonster.com

Note that Marshall suggested using quotes and ampersands. The ampersands are
used to concatenate, or combine, several elements into a single text string.
" Me.AssignedChap " will assign the literal value " Me.AssignedChap ", not
the field value.

Does the code compile correctly?

Try adding a message box at the end of the After Update code:

MsgBox Me.AssignedChap.DefaultValue

Try the same code in the form's Current event.

There are other ways to go about this, but in any case the idea is to test
the code to be sure the expected values are being produced.

Another way of testing as you go is to open the VBA editor, then click in the
vertical bar to the left of a line of code. A dot should appear on the
vertical bar, and the line of code highlighted. This is called setting a
break point. When you run the code it will stop at the selected line. You
can hover the mouse over things such as Me.AssignedChap to see the current
value. Press F8 to move to the next line of code. This procedure is known
as stepping through the code.
Hi Again Marshall,

I'm still confused, the assignedchap is text, not numeric. I'm still
getting the error "#Name?". Here is the code I'm currently using.

Private Sub AssignedChap_AfterUpdate()

Me.AssignedChap.DefaultValue = " Me.AssignedChap "

End Sub

I've tried these variations and they are not prefilling the previous data
entry either.

Me.AssignedChap.DefaultValue = """ Me.AssignedChap """
Me.AssignedChap.DefaultValue = """ & Me.AssignedChap & """

The data entry is a text name, for example it would be Tom Jones....yet when
I save the form the name Tom Jones is not displaying in the form field when
it is reloaded. Is there another way to accomplish the prefill?

Thank you for your patience while I learn this.
If you coded what you posted, you have the wrong number of
quotes. It's four quotes in both places.
[quoted text clipped - 51 lines]
 
M

Marshall Barton

The DefaultValue property is confusing because you use VBA
with an expression to construct a string to set the
property. Then, when the first character is entered on a
new record, the string in the property is evaluated as
another expression and the control's value is set to the
result.

When you used:
Me.AssignedChap.DefaultValue = " Me.AssignedChap "
the property is set to the string:
Me.AssignedChap
There are multiple reasons why Access (as opposed to VBA)
can not evaluate that as an expression the way it would be
evaluated in a form's VBA code. The most important reason
is that, as in queries, control expressions are evaluated by
something called the Expression Service. VBA variables and
other constructs (such as Me) are not within the scope of
the Expression Service (public functions in standard modules
are the *only* VBA items that the expression service
understands). Without going into all the details, it's
clear that Me.AssignedChap is not something that the
expression service understands so you get the #Name error.

OTOH, if you use the VBA statement I posted earlier:
Me.AssignedChap.DefaultValue = """" & Me.AssignedChap &
""""
then the DefaultValue property is set to something like the
string:
"whatever"
where the quotes tells the expression service that whatever
is just a text value.

Just to add to the confusion, when you type whatever
directly into the DefaultValue property, Access adds the
quotes around it for you. This leads a lot of people astray
because it's so easy when all the subtleties are hidden from
you. If you type =2+3 (with no quotes) directly into the
DafaultValue property, Access sees the = sign and does not
add quotes around the 2+3, which is clearly an expression
that will end up setting the control's value to 5 (not the
text "2+3").
 

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