getting a form field to default to the value entered in the last record

  • Thread starter Thread starter Paul James
  • Start date Start date
P

Paul James

Is there a way to get a form field to default to the value entered in the
last record?

For example, if a user is entering many records, and each record requires
them to enter their name, it would be nice if they could avoid having to
keep re-typing their name or pressing Ctrl+"". Is there an expression you
can put in the Default Value property of the form field to make it easier on
the user?

Thanks in advance,

Paul
 
Paul said:
Is there a way to get a form field to default to the value entered in the
last record?

For example, if a user is entering many records, and each record requires
them to enter their name, it would be nice if they could avoid having to
keep re-typing their name or pressing Ctrl+"". Is there an expression you
can put in the Default Value property of the form field to make it easier on
the user?


Use the control's AfterUpdate event to set its own
DefaultValue property:

Me.textbox.DefaultValue = """" & Me.textbox & """"
 
I do have a question about this, Marsh. What is the reason for
concatenating the double quotation marks before and after Me.textbox? I
would have thought it would work without them.

Paul
 
Paul said:
I do have a question about this, Marsh. What is the reason for
concatenating the double quotation marks before and after Me.textbox? I
would have thought it would work without them.


The DefaultValue property is a string. There are times when
you can get away without using them, but only when Access
makes the right guess in its automatic conversion process.
Since the Default value is evaluated as an expression, this
is a very iffy affair. The quotes are not required for
numeric values, but it is especially confusing with date
literals where the automatic conversions can change the date
to a different format depending on the local settings.
 
So then if it might look like a date to Access, the quotes guarantee that
Access will know the value is a string.

But then why would we need four double quotes in each place? Why would not
one single quote be enough?
 
Paul said:
So then if it might look like a date to Access, the quotes guarantee that
Access will know the value is a string.

But then why would we need four double quotes in each place? Why would not
one single quote be enough?


Let's say you want the default value to be the word "Date".

If you use:
txtbox.DefaultValue = Date
Access will evaluate the Date function, return a Date type
value, convert it to a string (e.g. 10/8/2004) using your
local settings and store that in the property. Then, when
you go to a new record, the string will be evaluated (in
this case as an arithmetic expression with two divisions)
and the field will have a value of .00062375249500998.

Now, let's try this instead:
txtbox.DefaultValue = "Date"
Access will store the letters D, a, t and e in the property.
When you go to a new record, the string will be evaluated
and you'll get a #Name? error because the expression service
does not recognize the Date function without its
parenthesis.

The right way to do this is:
txtbox.DefaultValue = """Date"""
so that "Date" ends up in the property and when on a new
record, the evaluation of "Date" is just the string Date.

In your case, you have a variable containing an unknown
string of characters. How do you code the assignment so
that the DefaultValue property has your characters inside
quotations to prevent unwanted evaluation of the string?
The rule for using a quote inside quotes is to double up the
inside quote, E.g. the string
She said, "Hi"
would be written as
strvar = "She said, ""Hi"""

But, in your case, you're just trying to make sure there is
a quote at the beginning and end of your string, so you want
to concatenate a string containing a quote on both ends of
your variable, which I suggested using """" as a way to
get a single quote into the result string. Another way
would be to use Chr(34), where 34 is the ascii code for ".
So, if you prefer:
Me.textbox.DefaultValue = Chr(34) & Me.textbox & Chr(34)

Or you could go one more step and define a string constant
with the character "

Const Quote As String = """"

and use:
Me.textbox.DefaultValue = Quote & Me.textbox & Quote

Your call, as long as the DefaultValue property ends up with
a quote at the beginning and end (so the expression
evaluation doesn't try to evaluate your string in some goofy
way.

Now, to take it to the next level. What if your string
variable just might contain a " character? You could end up
with unbalanced quotes in the property. So, to be totally
safe you can use:
Me.textbox.DefaultValue = _
"""" & Replace(Me.textbox, """", """""") & """"

Which is as confusing as it can get, but perfectly logical
when you get used to quoting quotes ;-)
 
Wow.

Handling the syntax in string concatenation has bedeviled me for the past
few years. I've gotten through it by cloning examples from the wonderful
contributors to these newsgroups, but more often than not, I didn't
understand the reasoning behind it. Your mini-tutorial in the last reply is
an eye-opener.

Thanks (again), Marsh.
 
Marsh - I tried using the expression

Me.textbox.DefaultValue = """" & Me.textbox & """"

to set the default value of the field named "textbox" and discovered
something interesting:

If the value of the field is:

John

then the expression will store the default value as

"John."

That is, when you look at the text box in the form, you actually see those
double quotes around the letters J o h n. If you look at the field in the
table, you can also see the double quotes around the characters in the
field.

Granted, the field has a text data type, and in code, you enclose literal
text in double quotes like that. But when you concatenate the form field
with the four double quotes on each side as in the expression above, VBA
actually stores the double quotes in the field along with the letters in the
text string. This of course is not what I want. I found that two double
quotation marks on each side seem to produce the desired result. There is
no chance my users will be entering dates or any function names like "Date"
because the control is actually a combo box, and the Limit to List property
is set to "Yes."

I realize the solution you provided was intended to cover the most general
case, where there was no way of knowing what values the user might type in.
But I'm glad you explained the use of the syntax as it will be helpful in
other situations. However, I thought you might be interested in the results
I obtained with the use of the four quotes.

And thank you again for the clear explanation.

Paul
 
Paul, I can't reproduce that behavior. Are you sure that
the you posted is the actual (Copy/Paste) code you're using?
 
Here's a direct copy and paste from the actual code I'm using:

Me!txtPreparer.DefaultValue = "" & Me!txtPreparer & ""

That code works fine.

When I tried using

Me!txtPreparer.DefaultValue = """" & Me!txtPreparer & """"

I encountered the problem I described in my previous message. I thought
this might be a fluke because txtPreparer is actually a combo box that looks
up values in a table. So I tried the expression on text box field, and it
did the same thing. When I use the four double quotes on each side, it
literally puts double quotes around the values stored in both the form
control and the table field. So two quotes on each side work fine, but four
do not.

I also tried it in the Immediate window:

?"" & forms!frmReceipt_NoInvoice!txtPreparer & "" produces
John

But

?"""" & forms!frmReceipt_NoInvoice!txtPreparer & """" produces

"John"

in both the form field and the table.

I wonder why you're getting different results.

Paul
 
See comments inline:
--
Marsh
MVP [MS Access]


Paul said:
Here's a direct copy and paste from the actual code I'm using:

Me!txtPreparer.DefaultValue = "" & Me!txtPreparer & ""

That code works fine.

Concatenating zero length strings onto a value only
guarantees that Access returns a string (not a number or
date). If txtPreparer is a Text field (string), then it was
already a string and the concatenations accomplish nothing.

When I tried using

Me!txtPreparer.DefaultValue = """" & Me!txtPreparer & """"

I encountered the problem I described in my previous message. I thought
this might be a fluke because txtPreparer is actually a combo box that looks
up values in a table. So I tried the expression on text box field, and it
did the same thing. When I use the four double quotes on each side, it
literally puts double quotes around the values stored in both the form
control and the table field. So two quotes on each side work fine, but four
do not.

Two quotes on either side doesn't accomplish anything beyond
what you would get by using:

Me!txtPreparer.DefaultValue = CStr(Me!txtPreparer)

The mystery is why concatenating a quote on each end is
being evaluated with the quotes still there. The only thing
I can think of that would produce the results you're seeing
is if the value of txtPreparer already has quotes around it.
I think I would like to know the **exact** value of the
txtPreparer combo box. Try using this and let's look at it:

Me!txtPreparer.DefaultValue = """" & Me!txtPreparer & """"
Debug.Print "V /" & Me!txtPreparer & "/"
Debug.Print "D /" & Me!txtPreparer.DefaultValue & "/"

I also tried it in the Immediate window:

?"" & forms!frmReceipt_NoInvoice!txtPreparer & "" produces
John

But

?"""" & forms!frmReceipt_NoInvoice!txtPreparer & """" produces

"John"

Yes, that's what I'm saying we want in the DefaultValue
property.

in both the form field and the table.

I wonder why you're getting different results.

Me too!
 
When I run these lines . .

Me!txtPreparer.DefaultValue = """" & Me!txtPreparer & """"
Debug.Print "V /" & Me!txtPreparer & "/"
Debug.Print "D /" & Me!txtPreparer.DefaultValue & "/"

in the After Update event of the txtPreparer combo box, I get this . . .

V /Smith, John/
D /"Smith, John"/

in the Immediate Window. And because I have this . . .

Me!txtPreparer = Me!txtPreparer.DefaultValue

in the Form Current event, VBA stores this . . .

"Smith, John"/

in the table fields. For the record that's current when I update
txtPreparer,

Smith, John

is stored in the table field. But as I move to other records,

"Smith, John"

appears in the table fields for those records.

The format property of txtPreparer in the form is blank, and Preparer is a
text field in the table.

Are you still getting different results than this?

Thanks for giving this issue your attention, Marsh.

Paul
 
There's a typo in the last post. In the text below, I wrote "Smith, John"
erroneously as "Smith, John"/ with a foreslash. It should have been written
"Smith, John" as below without the foreslash. Sorry for any confusion.

And because I have this . . .

Me!txtPreparer = Me!txtPreparer.DefaultValue

in the Form Current event, VBA stores this . . .

"Smith, John"

in the table fields.
 
Paul said:
When I run these lines . .

Me!txtPreparer.DefaultValue = """" & Me!txtPreparer & """"
Debug.Print "V /" & Me!txtPreparer & "/"
Debug.Print "D /" & Me!txtPreparer.DefaultValue & "/"

in the After Update event of the txtPreparer combo box, I get this . . .

V /Smith, John/
D /"Smith, John"/

in the Immediate Window. And because I have this . . .

Me!txtPreparer = Me!txtPreparer.DefaultValue

in the Form Current event


AH HA! That's the thing that's causing all this trouble.

When you copy the DefaultValue (which is supposed to have
those quotes) to the value, you are copying the quotes too.
If you get rid of that statement in the form's Current event
and let Access supply the default value in the normal
fashion, you would not have the extra quotes.

I can't imagine a reason for you to have that line of code,
especially since it dirties the record before the user even
gets a chance to see it. Even worse, if you run that line
of code without checking for a new record, then that value
will overwrite any existing value in all records just by
navigating through a bunch of records.
 
You're right. That was the problem.

Ok, I removed it, and it's working just fine.

Thanks again, Marsh.
 
Back
Top