View Full Version : SQL statement on form to update table


joeyo34
12-06-2007, 10:50 AM
I am trying to write a SQL statement that will find a specific value in the table a form is based on and automatically put the current time, Now(), in automatically when someone pushes a button to make a new entry. Basically I think I'll need to use the DoCmd.RunSQL and then write an UPDATE SQL statement that finds the entry and updates it. The problem is I do not know how to refer to my table or the form and the correct syntax to use. Here is the basic code I am using...

Private Sub Form_Close()
Dim SQL As String
SQL = (UPDATE [Sample_TM_Table_1], SET [Time_Out] = Now(), WHERE [Time_Out] IsNull & [User] = Me.User)
DoCmd.RunSQL SQL
End Sub

I do not know SQL stuff or really any programming specifically, just learn as I go so sorry if the horrific syntax and such offends any of you programmers:)!

Basically I want it to look back into the table and find the entry that has a Null value for the field called "Time_Out" and the field "User" is equal to CurrentUser(). Can anyone help me get my process to work? Thanks in advance!!!

Alc
12-06-2007, 10:57 AM
Private Sub Form_Close()
Dim str_SQL As String
str_SQL = "UPDATE Sample_TM_Table_1 SET Time_Out = #" & Now() & "# WHERE IsNull(Time_Out) AND User = '" & Me.User & "';"
DoCmd.RunSQL str_SQL
End Sub

I think that should do it, but I'm sure one of the more brainy bods will point out any errors/omissions I've made.

joeyo34
12-06-2007, 11:31 AM
Worked like a charm ALC....thanks a ton!

Alc
12-06-2007, 11:32 AM
Glad to help, thanks for letting me know it worked.