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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Marshall Barton (marshbarton_at_wowway.com)
Date: 10/08/04


Date: Fri, 08 Oct 2004 09:06:26 -0500

Paul James wrote:

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

-- 
Marsh
MVP [MS Access]


Relevant Pages

  • Re: Mandis Quotes (aka retiring """ and )
    ... characters not be allowed, ... Then I remembered string ... Any binary data can be inside the quote. ...
    (comp.lang.python)
  • Re: D.E. Knuths strlen
    ... In order to locate the end of a string quickly, ... I had to check this quote, ... FWIW, translated into C, you get a 1:1 ratio of characters to "words". ...
    (comp.lang.c)
  • Re: Difference between and : symbols
    ... lisp> ... You can inhibit evaluation with the special operator QUOTE. ... The specific thing that confused you is why ASDF functions ... But what ASDF really wants is a string. ...
    (comp.lang.lisp)
  • Re: The Rise of "Logical Punctuation".
    ... Americans are getting used to seeing logical punctuation, ... [begin quote] ... I want you first to consider the string 'the string' and the string ... the quotation mark must follow the punctuation mark. ...
    (alt.usage.english)
  • Re: fastest way to change case of string
    ... string rather than concatenating a new string is indeed much faster. ... Dim quoted As Collection ... Const QUOTE = "'" ... ChangeCaseX = StrConv ...
    (microsoft.public.vb.general.discussion)