Saving Variables in VB events

R

Richard Harison

Hello!
I am trying to devise a VB event which will save a date, so that when a user
adds a record, the date he/she enters will appear automatically in each new
record addition until the user manually changes it, and then that date will
repeat, and so forth. I am working with the Click event on an "add record"
button (DoCmd.GoToRecord , , acNewRec) and have a static
variable SavDate, but I can't figure how to pass it back to the Date field in
the form when the user changes the date. Thanks in advance
 
R

Richard Harison

But what happens when the user finishes adding records with that date, enters a
new one, and wants that value to repeat?
 
A

Albert D.Kallal

The suggestion was to not use a variable, and simply set the default value
of the control.

So, use the controls after update event, and simply go



Me.BirthDate.DefaultValue = """" & Me.BirthDate & """"

So, if the user changes the value, then the default value for the control
will change, and the next record added will show this value...
 
R

Richard Harison

Brilliant...Thank You!!! I understand the logic: set the default value to what
is already found in the control. BUT...why all the quotes and the
concatenation? (Me!SchDate.DefaultValue = """" & Me!SchDate & """") I admit to
only a modest knowledge of VB, but I am accomplished in the old MS compiler
BASIC. Just curious---again thanks for solving the problem!!
 
A

Albert D.Kallal

Brilliant...Thank You!!! I understand the logic: set the default value
to what is already found in the control. BUT...why all the quotes and the
concatenation? (Me!SchDate.DefaultValue = """" & Me!SchDate & """") I
admit to only a modest knowledge of VB, but I am accomplished in the old
MS compiler BASIC. Just curious---again thanks for solving the problem!!


If you look close at a form in desing mode, to enter a DEFAULT value for a
contorl on a form, you have to use


"07/02/2006"

You can NOT use

07/02/2006

So, simply put, the default setting for a control on a form needs to be
surrounded by quotes

To enter a single quote in the middle of a string, you use

" this is some text with a quote "" in the middle"

When the above is printed, you would get:

this is some text with a quote " in the middle

So, two quotes in the middle of a string gives you a single quote....hence

"""" = "
If you been exposed to older basic languages, then I could have used:

Me.BirthDate.DefaultValue = chr$(34) & Me.BirthDate & chr$(34)

The above code would have also worked just fine, and likely is what I should
have used to avoid confusing you.
 
R

Richard Harison

Au contraire! You did a fine job. Yes the syntax of strings certainly has
changed over the years! To make matters worse, ASP uses single quotes rather
than the traditional double quotes. (Arrrrgh) Quite familiar with chr$(34)!
The key was when you explained that the default value had to be enclosed in
quotes, as it is a string. Once understood--the mists cleared! Thanks again!
 
M

Marshall Barton

Richard said:
Au contraire! You did a fine job. Yes the syntax of strings certainly has
changed over the years! To make matters worse, ASP uses single quotes rather
than the traditional double quotes. (Arrrrgh) Quite familiar with chr$(34)!
The key was when you explained that the default value had to be enclosed in
quotes, as it is a string. Once understood--the mists cleared! Thanks again!


Now that you've mastered DefaultValue Lesson 2, let's move
on to Lesson 3 ;-)

The DefaultValue property is an **expression** that will be
evaluated when it is used in a new record. (Even though
Help says to use an = sign to get evaluation, I have found
the = sign to be optional.) This means that setting the
default value of a text box to something like either:

textbox.DefaultValue = "2+3"
or
textbox.DefaultValue = "=2+3"

will result in the new record having a 5 in the text box's
value. To actually get 2+3 as a text string in the text
box's value, you must use either:

textbox.DefaultValue = """2+3"""
or
textbox.DefaultValue = "=""2+3"""

Keep this in mind when you just want a string as the default
value. For example:

textbox.DefaultValue = "ABC"

will result in #Name because ABC is not a known name.
Again, you need to use:

textbox.DefaultValue = """ABC"""

OTOH, this may be useful(?) in a case where you want the
default value of a text box to be the concatenation of two
or more other unbound controls. Something of this nature
perhaps:
textbox.DefaultValue = "txtModel & ""-"" & txtSerialNo"
which could result in ABC-1234

This whole discussion can get rather complex/confusing with
default values that look like a date. For example:

textbox.DefaultValue = "7/4/06"
resulting value in textbox 0.291666666666667

textbox.DefaultValue = "#7/4/06#"
regulating value in textbox 7/4/06

textbox.DefaultValue = """7/4/06"""
regulating value in textbox 7/4/06

Note that the last example above is arrived at in a
roundabout way since the default value is really a string
that might, in some situations, be converted to a date
using the Windows date settings. I think(?) this is safe,
but there might(?) be some combinations of Windows settings
that would result in 7 Apr 06 so I always use the # date
delimiter as in the second example.

Think about all this and when the headache eventually
subsides, move on to Lesson 4 where you have to take into
account the data type of the field bound to the text box
and/or the text box's Format property ;-))
 
R

Richard Harison

Thanks again. I was puzzled at the need to use 2+3 just to put a 5 as default.
But performing math using other controls or concatenating
strings....hmmm...there are possibilities here!
 
Top