Save "+" "-" date as default date.

C

Carol

Please help. I know just enough to get myself into trouble.

I would like to increment a date field with "+" or "-" keys, and use it as
a default on the next data entry form. The following code almost works. The
"+" & "-" keys work to increment the date, but when I go to the next form, it
doesn't carry over as the default. If I physically type in the date, it will
work as a default. (I also have the same problem with a numeric field, if
there is a difference.)

Thanks!!

Private Sub Date_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 43 ' Plus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case 45 ' Minus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
Case 61 ' Equal key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case 95 ' Underscore key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select

End Sub


Private Sub Date_AfterUpdate()
With Me.Date
If Not IsNull(.Value) Then
.DefaultValue = """" & .Value & """"
End If
End With
End Sub
 
G

George Nicholson

just a guess (i.e, untested), but try using date delimiters rather than
string delimiters in your concatenation:

..DefaultValue = "#" & .Value & "#"

BTW, "Date" is a reserved word. You will generally be much happier in life
if you avoid using reserved words for variable or object names (i.e.,
fields, controls, modules, procedures, etc. Well, ok a procedure isn't an
object, but don't do it anyway.) It may not be causing you a problem now,
but that doesn't mean it won't start being a problem when you least expect
it.

For an idea of what to avoid:
http://support.microsoft.com/default.aspx?scid=kb;en-us;286335

HTH,
 
C

Carol

I tried your suggestion, but still have the same problem. Thanks for the
help. Do you have any other fixes to try??

Carol
 
S

SteveS

Carol,

Also, "Date" isn't very descriptive. Which "Date"? Wedding, Hire, Fired, birth,
death, ....??? If the mdb is just for you, you might get away with it for a
while, but at some point you will start having problems. When you add the field
to a form, now there is a field named "Date", a built in function named "Date"
and a control named "Date". Which one are you referring to when you use
Me.Date? Access has to make assumptions....

If you Google "Naming Conventions", you will find hundreds of
references/posts/etc on why/how. Here is a link to one way of naming objects:

http://www.mvps.org/access/general/gen0012.htm

It takes a little more time to "do things right" (my opinion), but it is worth
it and saves you time in the long run.... (fewer problems)


OK, enough of that... on to your problem.



The AfterUpdate event only fires when something is typed into the control.
Using code (or a macro) won't fire the event.

Also, this code only sets the *control* default value and only until the form
is closed. The next time the form is opened, the default value is null (or
empty) because the *form* (control) changes were not saved.

So, using the code to set a default for use by another form won't work.

If you want to set a *field* default value, (A2K) help says that you need to
use DAO.


HTH
 
T

Tim Ferguson

Screen.ActiveControl = Screen.ActiveControl + 1

With Screen.ActiveControl
' You need to defend this code _much_ better
' remember the .Text is always a string (no nulls here!)
If Len(.Text)= 0 Then
Exit Sub

' don't try to manipulate crud. Validate later, or get
' Access to do it for you for a bound control
Else If Not IsDate(.Text) Then
Exit Sub

Else
Select Case Key_Ascii
Case 43 ' increment
' coerce the string into a date, then add one, then
' format it back into a string
.Text = Format(CDate(.Text)+1,"Short Date")

Case 35 ' etc etc etc


Ditto what other people said about naming controls or fields with
reserved words like "Date".

Hope that helps


Tim F
 
J

John Spencer

Well, you've gotten a lot of suggestions on Changing your Keypress code.

You're not setting the Default value because the code is not executing in
Date_afterUpdate. That code will only execute when you make the change in the
value directly, unless you call it in the Date_Keypress code.

Case 43
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Call Date_AfterUpdate
Case 45
...
 
C

Carol

I appreciate everyone's advice, and I continue to learn a lot. Thank you,
thank you, John. This solves the problem.
 

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