using VBA to populate date/time fields on a subform

jd9913

Registered User.
Local time
Yesterday, 17:46
Joined
Sep 12, 2012
Messages
14
Hi,
I have a form that contains a subform. On the main form, there are 3 fields: [IncidentNumber], [OpenDate] and [OpenTime]. The subform, [CtrlLogDetail] , contains the log entry details for each incident number. Every incident number can have many log entries. Two of the fields on the subform are [EntryDate] and [EntryTime]. The fields work correctly and the forms are fine. The issue I’m having is when a new incident number is created the [OpenDate], [OpenTime] and [EntryDate], [EntryTime] MUST be equal because of some filter queries for statistics. Right now the user must physically type in the date/times in these 4 fields when they create a new incident number, which means I'm having lots of data entry errors.
Using default value on either [EntryDate] or [EntryTime] on the subform doesn’t work because it doesn’t create a primary key for the underlying table and the subform will not allow any other entry (due to some other linked values) until that PK is created. The forms are based on tables linked by [Activity_ID] as the primary key.
I want to create some VBA code to see that if the Incident number is a new number (meaning the user must type in the new incident number creating the primary key for [Activity_ID]), that access will automatically populate the [EntryDate], [EntryTime] fields to match the [OpenDate], [OpenTime] fields on the main form. The user will manually populate the [OpenDate], [OpenTime].
If the number is an existing number (and the user is simply adding a log entry to the incident number) then I want [EntryDate], on the subform to default to the system date/time ([EntryTime] should remain blank for user entry) while keeping [OpenDate], [OpenTime] unchanged. When the [EntryDate], [EntryTime] fields are populated on creation of a new incident number, I want the PK for the underlying table ([Log_ID]) to be generated.
I have no idea even how to begin to create the code for this type of problem. I’m a beginner in VBA and haven’t been able to find a solution when searching the internet and forums. Any assistance either with how to approach this coding problem or directing me to an existing solution would be appreciated.
Thank you.
 
Using default values should work.
The pk will be created when you put data in any field.
 
Nope, the default value does not work. When I use it, it does not create the PK so that the other linked forms function. The PK is only created, at this point, when the user physically types the value into the field. When I place the default value, the form appears to place the value into the field, however it does not create that PK unless the user deletes the default and retypes it. I've done all I know to make that work and it doesn't do what I need it to.

I also don't want the [OpenDate], [OpenTime] to be the default for subsequent entries (only the inital entry should default to the [OpenDate], [OpenTime] values) because we can have an incident open for multiple days, it would cause confusion and data problems if the value defaults to the previous date for log entries that are made later. Subsequent log entries (the 2nd one on) should have the [EntryDate] default to the system date and the [EntryTime] remain null for user input. At the very least I think I need some sort of IIF statement, but I'm not sure of syntax or even where to place it--I think because of the complexity of what I want the form to do, VBA seems like the best answer.
 

Users who are viewing this thread

Back
Top Bottom