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 ;-)