Insert a create date and Modified date

I have an Access 2013 front end writing to a SQL 2008R2 table, and I am trying to add the date that the record is created via VBA, however the data saves but the Date does not. Also I want to to track any changes made to the record and add a Modified date .
Private Sub btnSave_Click()
Dim LResponse As Integer

LResponse = MsgBox("Do You Want to Save This Record?", vbYesNo + vbQuestion, "Continue")
If LResponse = vbYes Then
'---------------------------------------
'saves current Record
'---------------------------------------
      sSQLInsert = "INSERT INTO dbo_ATI_STDLR(CREDATE) VALUES (#" & Now() & "#);"
     RunCommand acCmdSaveRecord
     
'---------------------------------------
'load a new record after the save.
'---------------------------------------
    DoCmd.GoToRecord , , acNewRec
'---------------------------------------
Else
DoCmd.RunCommand acCmdUndo
    Me.ADDRESS1.SetFocus
    Me.CUSTOMER_NO.SetFocus
     
    Exit Sub
End If
    
End Sub

Open in new window

skull52Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PatHartmanCommented:
The CreateDate does not require any VBA code at all.  Simply give it a default of Now() in the table.  Whenever a new record is added, the CreateDate will be populated with the current date and time.  If you prefer just to use date, then use Date() rather than Now().

UpdateDate requires only a single line of code but that code MUST go into the Form's BeforeUpdate event.

Me.UpdateDate = Now()

Therefore, any time a record is updated, the UpdateDate will be modified to include the current date and time.  NO other event is appropriate for this code.  The Form's BeforeUpdate event is the LAST event to run before a record is saved and there is no way to bypass it so it always runs.  Code in button click events or other events can always be bypassed, either accidentally or intentionally.  The BeforeUpdate event is foolproof.

I'm not sure what you are trying to do with the Insert query since technically you want to modify an existing record not add a new one.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skull52Author Commented:
Thanks Pat, Yeah... I was trying to be fancy with the VBA code but that was unnecessary. There is no MODDATE control on the form just a column in the table will that still work.
0
PatHartmanCommented:
Neither field needs to be bound to a control on the form but they do need to be included in the Form's RecordSource table/query.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.