Link to home
Start Free TrialLog in
Avatar of mlcktmguy
mlcktmguyFlag for United States of America

asked on

Default Date on New Record On Continuous form

I have a date stored in a field called gDate.

 As the user creates a new record in the table I want to make this the default value of the date entry field called TTrack_Date_In.
I have the following logic in the Current event of the form:

if me.newrecord then'
    me.TTrack_Date_In.DefaultValue = gDate
endif

This results is a very strange value (12/30/1899) in TTrack_Date_In.  I remember that defaultvalues always have to be characters but I'm not sure how to get the date in gDate to show up correctly as the default value in TTrack_Date_In.

Any one know the syntax?
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

the field "TTrack_Date_In" must be Date/Time data type

then use this

if me.newrecord then
    me.TTrack_Date_In  = Date()
end if
private sub form_current()

if me.newrecord then
    me.TTrack_Date_In  = Date()
end if

end sub
Avatar of mlcktmguy

ASKER

Thanks but that's not really it.

I want to det the value to the date that is already in the field gDate.  

I want to use the 'DefaultValue' property because a record is not created if the user inadvertently tabs or goes to the new record.  The user then has to delete it.
private sub form_current()

if me.newrecord then
    me.TTrack_Date_In.DefaultValue  = me.gDate
end if

end sub

is field gDate in the same table as the record source of the form?
private sub form_current()

if me.newrecord then
    me.TTrack_Date_In.DefaultValue  = "=gDate"
end if

end sub
or this

private sub form_current()

if me.newrecord then
    me.TTrack_Date_In.DefaultValue  = "=Date()"
end if

end sub
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I set several other 'defaultvalue' porpeties in the current event and have been doing so for many years.  It always works whne you are setting a string field to a string value.

The issue in tis case is that I am tryiong to set a date field defaultvalue form a variable defined as 'date'.  I know that I have doen this bfore but it requires some manipulation to get the date into a character format.  I thought asking the question here might be more efficient than trying to find the solution in my old code.  I don't do it that often and I'm not sure which app I used this in.

Capicorn1
me.TTrack_Date_In.DefaultValue  = me.gDate
does not work as explained in my original question.  

"I have a date stored in a field called gDate. "
Did you try my post at http:#a37570869?  It's a different method, but it does work for what you described.

If gDate is a field on a form, you would use the full form reference to that field in the 'wrapper function'.
I had not tried it.  When I first read it, I thought you were suggesting using the function in the current event.  When I re-read it I saw that you are not suggesting that at all.  I did as you suggested and it worked perfectly.

Thank you for the suggestion and the tactful prodding to re-read and try again.

Issue resolved.
Use the Form BeforeInsert event, which is exactly what it's intended for ... to set Default values:

Private Sub Form_BeforeInsert (Cancel As Integer)
    Me.TTrack_Date_In = gDate ' or whatever date from where ever.
End Sub

The BI event will trigger as soon as any Bound field is edited in a New Record.

Setting bound control values in the Form Current event is a bad idea, because ... as you scroll through existing records (or come to the new record position), the Form becomes Dirty unnecessarily ... especially when there is no intent to edit an existing record being scrolled through.

mx
mlcktmguy,

Glad to help out.



mx,

He wasn't setting the value of the control itself - he was setting the 'default value' property of the textbox.

I've just been testing that out myself using strings - its a neat solution actually (and since you're not setting the value property, it doesn't make the form dirty.

(Learn something new every day :-) )
As I explained in the original post, I only set the default values on a new record.  The existing records do not become dirty as they are scrolled thru.

if me.newrecord then
    me.TTrack_Date_In.DefaultValue = gDate
endif

Even the new record does not become dirty because I am using the 'DefaultValue' property instead of assinging a value to the field.

I think using your format in whatever event will always make the record dirty.  If the user inadvertently goes to a new record I'm pretty sure your method will create a record that they will have to remove.  You may want to use the 'Defaultvalue' property to avoid that.

Learned it after many complaints from user that had wandered into the 'new' record at the end of a continuous form.
" If the user inadvertently goes to a new record I'm pretty sure your method will create a record that they will have to remove. "
On the contrary. That does not happen. Nothing changes until ... the user decides to *start* a new record by entering data into a Bound Control.  This is when the BI event triggers.

I never use the DefaultValue property because ... when you come to the New Record position, I do not want *any* data showing in *any* field ... until ... the user begins a new record by entering data in a bound field.  

What you get by using the DV property is data setting in a control *before* a record has actually been started ... which makes it *look* ... like there is already a record present, especially if there are multiple DV's.

Not using the DV property and instead using the BI event results in a much cleaner looking UI.

mx