Default value calculated from other records

R

Renraf

I am looking to have a Start time field automatically default to the Stop
time field from the last related record. Here is the structure:

PrimaryTaskLogs table:
*Task ID (primary key)
*LogID (foreign key to DailyLogs table)
*Start (time)
*Stop (time)

I want the Start time for a new record to default to the Stop time from
whichever of these is easier (B is preferable)
A) The most recently added (highest TaskID) record in the PrimaryTaskLogs
table with the same LogID as the current record
B) The last chronological record (highest Stop time) in the PrimaryTaskLogs
table with the same LogID as the current record

If such a record exists (not the first record for a given LogID).

Did that make sense? And would I do this through the entry form or through
the table?

Wish I'd taken the time to learn Visual Basic years ago instead of finding
out that I have to use it in a crunch . . .
 
D

David H

In the _AfterUpdate event of the control, you can capture the value entered
and then change the .DefaultValue of the same control or another as in...

Off the top of my head...
Sub [Control Name with the Value You Want to Capture]_AfterUpdate

[Forms]![formName]![Control That You want to Set].DefaultValue = Chr(34) &
[Forms]![formName]![Control Name with the Value You Want to
Capture].DefaultValue & Chr(34)

End sub

Before you implement this though, see my response to your other post.
 
R

Renraf

David, thank you for your reply, but I have a couple of concerns about the
code:
1) This permanently changes the default value for that field? What if two
users are simultaneously entering data? There's no segregation, correct?
2) What if a user enters the last value for the day? When the form is next
opened, will the start time populate from the completely unrelated stop time
(e.g. 5:30pm, when the user actually wants to start a new day at 8am)?

This is supposed to be a nice-to-have time-saving feature, but if it
requires a user override as often as not, it becomes rather pointless.

Looking at your solution makes me think I will probably put this into the
AfterUpdate action for the LogID, so it can pull the stop times from data
with identical LogIDs . . . I just don't know how to pull that data.

David H said:
In the _AfterUpdate event of the control, you can capture the value entered
and then change the .DefaultValue of the same control or another as in...

Off the top of my head...
Sub [Control Name with the Value You Want to Capture]_AfterUpdate

[Forms]![formName]![Control That You want to Set].DefaultValue = Chr(34) &
[Forms]![formName]![Control Name with the Value You Want to
Capture].DefaultValue & Chr(34)

End sub

Before you implement this though, see my response to your other post.

Renraf said:
I am looking to have a Start time field automatically default to the Stop
time field from the last related record. Here is the structure:

PrimaryTaskLogs table:
*Task ID (primary key)
*LogID (foreign key to DailyLogs table)
*Start (time)
*Stop (time)

I want the Start time for a new record to default to the Stop time from
whichever of these is easier (B is preferable)
A) The most recently added (highest TaskID) record in the PrimaryTaskLogs
table with the same LogID as the current record
B) The last chronological record (highest Stop time) in the PrimaryTaskLogs
table with the same LogID as the current record

If such a record exists (not the first record for a given LogID).

Did that make sense? And would I do this through the entry form or through
the table?

Wish I'd taken the time to learn Visual Basic years ago instead of finding
out that I have to use it in a crunch . . .
 
D

David H

See below...

Renraf said:
David, thank you for your reply, but I have a couple of concerns about the
code:
1) This permanently changes the default value for that field? What if two
users are simultaneously entering data? There's no segregation, correct?

No. This only changes the .DefaultValue for the instance that the user is
working with. When you're using a FE/BE combination, this will not impact any
other user. I've never tried with a shared front end, but I suspect that its
instance-specific.
2) What if a user enters the last value for the day? When the form is next
opened, will the start time populate from the completely unrelated stop time
(e.g. 5:30pm, when the user actually wants to start a new day at 8am)?

Changes made to the property on the fly are not saved when the form closes.
If you set the .DefaultValue to a function that returns the value that you
need, that value will be used each time the form is opened. Then as records
are added the _AfterUpdate event will overright the change.

In the property window for the control, add =DMax(FieldName, Table,
Criteria) to the Default Property as =DMax("StopTime", "Orders", "StopTime =
#6/1/2009# AND UserId = 'David'")

You'll have to massage the criteria to reference the Date() to pull the
correct date. Something along the lines of...

=DMax("StopTime","Orders","StopTime=" & DateAdd(-1,"d",Date))
This is supposed to be a nice-to-have time-saving feature, but if it
requires a user override as often as not, it becomes rather pointless.

Looking at your solution makes me think I will probably put this into the
AfterUpdate action for the LogID, so it can pull the stop times from data
with identical LogIDs . . . I just don't know how to pull that data.

If each user will have their own PC and will be entering tasks as they go,
as opposed to after the fact, all you have to do is grab the controls value
as in...

Sub FirstName_AfterUpdate

[Forms]![OrderEntry]![FirstName].DefaultValue = Chr(34)
[Forms]![OrderEntry]![FirstName].Value & Chr(34)

End sub

Otherwise, its one of the domain functions such as a DLookup() or DMax().
BUT pulling the value from the previous record will always be faster.
David H said:
In the _AfterUpdate event of the control, you can capture the value entered
and then change the .DefaultValue of the same control or another as in...

Off the top of my head...
Sub [Control Name with the Value You Want to Capture]_AfterUpdate

[Forms]![formName]![Control That You want to Set].DefaultValue = Chr(34) &
[Forms]![formName]![Control Name with the Value You Want to
Capture].DefaultValue & Chr(34)

End sub

Before you implement this though, see my response to your other post.

Renraf said:
I am looking to have a Start time field automatically default to the Stop
time field from the last related record. Here is the structure:

PrimaryTaskLogs table:
*Task ID (primary key)
*LogID (foreign key to DailyLogs table)
*Start (time)
*Stop (time)

I want the Start time for a new record to default to the Stop time from
whichever of these is easier (B is preferable)
A) The most recently added (highest TaskID) record in the PrimaryTaskLogs
table with the same LogID as the current record
B) The last chronological record (highest Stop time) in the PrimaryTaskLogs
table with the same LogID as the current record

If such a record exists (not the first record for a given LogID).

Did that make sense? And would I do this through the entry form or through
the table?

Wish I'd taken the time to learn Visual Basic years ago instead of finding
out that I have to use it in a crunch . . .
 

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