record date a record is modified (1 Viewer)

hefly

New member
Local time
Today, 06:27
Joined
Jan 29, 2017
Messages
4
I have a field in table tblInstruments called ModDate. I want to record the date that a record is modified.

I have tried two things on my input form but neither are working.

First solution I tried is:

Private Sub Form_AfterUpdate()
Docmd.RunSQL UPDATE tblInstruments SET tblInstruments.ModDate = Date();

End Sub


That give me a syntex error.



I have tried:

Private Sub Form_Dirty(Cancel As Integer)
Me!ModDate.Value = Date
End Sub

That does nothing. I tried that code under AfterUPdate and the input form froze.

I am obviously a novice at VBA and have no clue to how to create code to perform my update. I have searched the web for examples and have not found anything. I'm stuck. :banghead:

Can someone please help?

Thank you.

Hefly
 

sneuberg

AWF VIP
Local time
Today, 06:27
Joined
Oct 17, 2014
Messages
3,506
The argument to Docmd.RunSQL needs to be a string and the date from the date function need to be concatenated in with date delimiters so this:

Code:
Docmd.RunSQL[COLOR="green"] UPDATE tblInstruments SET tblInstruments.ModDate = Date();[/COLOR]

would need to be something like:

Code:
DoCmd.RunSQL [COLOR="blue"]"[/COLOR]UPDATE tblInstruments SET tblInstruments.ModDate = [COLOR="red"]#[/COLOR][COLOR="blue"]"[/COLOR] & Date & "[COLOR="red"]#[/COLOR]"
 

TJPoorman

Registered User.
Local time
Today, 07:27
Joined
Jul 23, 2013
Messages
402
The argument to Docmd.RunSQL needs to be a string and the date from the date function need to be concatenated in with date delimiters so this:

Code:
Docmd.RunSQL[COLOR="green"] UPDATE tblInstruments SET tblInstruments.ModDate = Date();[/COLOR]

would need to be something like:

Code:
DoCmd.RunSQL [COLOR="blue"]"[/COLOR]UPDATE tblInstruments SET tblInstruments.ModDate = [COLOR="red"]#[/COLOR][COLOR="blue"]"[/COLOR] & Date & "[COLOR="red"]#[/COLOR]"

You don't have to break out the date in delimiters. Date() is a function that does not require being broken out.
So from the original string all that is necessary is to put quotes around the SQL string.

Code:
Docmd.RunSQL [COLOR="red"]"[/COLOR]UPDATE tblInstruments SET tblInstruments.ModDate = Date();[COLOR="Red"]"[/COLOR]
 

sneuberg

AWF VIP
Local time
Today, 06:27
Joined
Oct 17, 2014
Messages
3,506
@TJPoorman Thanks for catching that.

@Hefly. I think you need more in this update statement. Both my and TJPoorman's version will update all of the records. To update the one being modified you need to identify it in a WHERE clause. If for example ID is the primary key in the recordset then.


Code:
DoCmd.RunSQL "UPDATE tblInstruments SET tblInstruments.ModDate = Date() WHERE [ID] = " & Me.ID
if instead the primary key field is a text field let's say InstrumentsPK then the textbox would have to be in single quotes like:

Code:
DoCmd.RunSQL "UPDATE tblInstruments SET tblInstruments.ModDate = Date() WHERE [InstrumentsPK] = '" & Me.InstrumentsPK & "'"

Note that the field name are assumed to be the same as the textbox names in these examples.
 

Users who are viewing this thread

Top Bottom