Hi,
I setup a very basic form to show a list of tasks ("to-do" items) from a table : tblTask_Listing.
The form is setup as a Continous Form; it contains the following fields from the aforementioned table:
TASK_DESCRIPTION (Short Text), TASK_ADDED_BY (Short Text), TASK_DATE (Date), TASK_COMPLETED (True/False).
There's also a TASK_ID field in the table which is the Primary Key (AutoNumber), though I'm not actually displaying that field on the form.
Instead of users needing to manually populate the TASK_ADDED_BY / TASK_DATE fields, I want those to automatically populate with their UserID and Today's Date, respectively. The controls associated with those 2 fields are simply "txtUser" and "txtDate".
Thus, I placed the below code in the "After Update" event.
When I add a new record at the bottom of the form, those 2 controls *do* indeed update with UserID + Date, as intended.. but when closing out of the form, I keep receiving an error message which states, "You can't save this record at this time. Microsoft Access may have encountered an error when trying to save a record (etc.)"
So clearly there's something that I'm missing in the code that it doesn't like. If I remove the After Update event/code, the form works fine - but obviously the UserID and Date fields must be manually populated which is what I'm trying to avoid.
Any help would be appreciated. Thanks!
Code:
Private Sub Form_AfterUpdate()
Dim strUserID As String
Dim strDate As String
strUserID = Environ("Username")
strDate = Format(Date, "mm/dd/yyyy")
Me!txtUser = strUserID
Me!txtDate = strDate
End Sub