default value question

Lac

Registered User.
Local time
Today, 10:21
Joined
Sep 17, 2004
Messages
22
Another quick question (with an easy answer, I hope):

Is there any way to set up a form in such a way that the user, when opening the form, is asked to enter a date, and the date they enter becomes the (automatically entered) default value for the "date" field until they close the form and open it again (and are asked for a date, and enter a different date, which becomes the new default value, and so on)?

Does my question even make sense?

Thanks in advance!
 
will the newly entered date be the date of the day ? If that is the case simply use Date() as the default value.
 
No, it will almost never be the date of the day...

It doesn't even have to be a "default" per say, just prompt the user to enter the date desired and then automatically enter that date for the user in the "date field" of each new record created until they close the form...
 
Last edited:
Dim a date variable at the module level of the form - or use a hidden text box to store the date. when the form opens let the user assign a date - then just reference the above to fill in the date field with each new record.
 
umm... just call me clueless... that sounds great, but I haven't the faintest idea what you mean or how to do all that...

(I am sorry to be so dense... but my access ambitions far exceed my access skills...)
 
Code:
Private Sub Form_Open(Cancel As Integer)
    Me.MyControl.DefaultValue = [i]myDefaultValue[/i]
End Sub
 
Thank you for the code... But I'm still rather clueless...

Where should the code go?

Is it part of the "dim a variable at the module level of the form"? Or is it part of using a hidden text box to store the date? (Or is that two ways of saying the same thing?) Or is it part of the "reference the above to fill in the date field with each new record"? Or does the code do all of that? Or something else entirely that has the same result?

Can you point me to an article/FAQ on this subject so I can stop bothering you with the really stupid/basic questions?
 
you need to decide how to populate the default date - whether you use a variable in the code or just a text box. The text box is probably easier.

You could have an unbound text box (lets call it defaultdatefield) on the form which has a default set up of your best guess as to what the user will want as their default ( ie today, 2 days ago etc etc), let this text box be editable, so the user can change the default date. (look at the calender form examples on the example database section for a nice way of users picking a date)


When a new record is added set the value of the date field you want to actually store to the value of the default date field -

Me!Datefield = Me!DefaultDateField

Hope this helps
 
I will let SJ McAbney take over - I presume he is using the varaible option.
 
SJ McAbney said:
As code for the form open event.
I already have the following
Code:
DoCmd.GoToRecord , , acNewRec '****
as the form open event...
Can both bits of code go under form open event? Or will one (which one) have to be moved to the (wild guess) form load event?
 
You can use them both in the same event.

One question I do have is how are you opening this form?
  • By clicking on it the database window;
  • By using a macro
  • By using some code with the DoCmd.OpenForm method
 
Pauldohert said:
I will let SJ McAbney take over - I presume he is using the varaible option.
Ok, thanks alot all the same...
I wouldn't mind learning both methods, though... just not simultaneously...
 
SJ McAbney said:
You can use them both in the same event.

One question I do have is how are you opening this form?
  • By clicking on it the database window;
  • By using a macro
  • By using some code with the DoCmd.OpenForm method

Clicking on it in the database window.
 
Another method, while I'm looking at this would be to create a new property for the form.

i.e.

Code:
Dim mDefaultDate As Date

Public Property Get DefaultDate() As Date
    DefaultDate = mDefaultDate
End Property

Public Property Let DefaultDate(NewDate As Date)
    mDefaultDate = NewDate
End Property


With this you can now set the form's new DefaultDate property to your date and, in the form's Current event.

Code:
Private Sub Form_Current()
    If Me.NewRecord Then Me.MyDateField = Me.DefaultDate
End Sub

You would just have to remember to set the form's DefaultDate initially.
 
Lac said:
Clicking on it in the database window.

Code:
Me.MyControl.DefaultValue = myDefaultValue
DoCmd.GoToRecord , , acNewRec


Personally though, I wouldn't ever want to grant access to the database window and would create a switchboard form where you can click a button to open the form.
 
Please, guys, I'm really grateful for the help but I'm now officially, hopelessly lost.

I do not know how to create new properties for forms (more than willing to learn but please, only one method at once)...

Anyway, I'm not worried about allowing the user access to the database window for this one because there'll be only one user (my boss) and she's not likely to mess with the database, even by accident. If I do decide to create a menu (switchboard??) form instead, does that change anything?
 
Ok, pasted the code in. Changed "MyControl" to the name of the date field. What do I have to change "myDefaultValue" to to get Access to prompt the user to enter the desired date?
 
For cheap and nasty just use this code:

Code:
Private Sub Form_Open(Cancel As Integer)
    Dim DefDate As Date
    DefDate = InputBox("Please enter the default date...","Default Date")
    Me.MyControl.DefaultValue = DefDate
    DoCmd.GoToRecord , , acNewRec
End Sub

Change MyControl to the name of your textbox. That should be a start.
 
Ok, pasted in the code. The first three lines went red.
Changed "myControl" to the name of the textbox (the date box).
Saved depite the red. Closed the form. Opened Form as user would. Error message about compilation and syntax. Instead of opening the form it opened the code page and showed those three lines in red.

I think I need food... back in a few with sandwitch in hand...
 

Users who are viewing this thread

Back
Top Bottom