VBA SQL Syntax Error

Littlemalky

Board Regular
Joined
Jan 14, 2011
Messages
223
Hi, I'm having trouble nailing down the syntax in this SQL statement. I'm trying to trigger this code to run upon clicking a button; however, I'm getting a syntax error.

I'm essentially trying to update three fields in a table: Employee Name, Modified By, Modified Date. This was working fine before I added in the Modified By and Modified Date. I have a UDF for UserName().

Names:

Table: [tbl_EMPLOYEE_COST_CENTER_MAP]
Field 1: [EMPLOYEE_FULL_NICK_NAME]
Field 2: [MODIFIED_BY]
Field 3: [MODIFIED_DATE]

Trying to set the [MODIFIED_BY] equal to the environment username and the [MODIFIED_DATE] equal to NOW() so i get a date/time stamp.

Code:
strSQL_EENAME = "UPDATE [tbl_EMPLOYEE_COST_CENTER_MAP] SET [EMPLOYEE_FULL_NICK_NAME] = '" & Forms![frm_SALES_EMPLOYEE_MANAGEMENT]!txtEMPLOYEE_NICKNAME_CHANGE & "'" & _                    
",[MODIFIED_BY] = '" & UserName() & "', [MODIFIED_DATE] = '" & Now() & "'," & _
"WHERE [EMPLOYEE_ID] = '" & Forms![frm_SALES_EMPLOYEE_MANAGEMENT]!cmbEMPLOYEE_ID & "'"
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
now() needs to be surrounded by #
not single quotes

date fields are always surrounded by #

strings are always surround by single (or double) quotes
 
Upvote 0
Hi, thank you for your response. I tried that and I'm still getting a syntax error. I'm not sure if it's related to the Username Part or the Now() part. My public function is as such:

Code:
Public Function UserName()    UserName = Environ$("UserName")
End Function
 
Upvote 0
You have a comma in front of WHERE.
Code:
strSQL_EENAME = " UPDATE [tbl_EMPLOYEE_COST_CENTER_MAP] SET [EMPLOYEE_FULL_NICK_NAME] = '" & Forms![frm_SALES_EMPLOYEE_MANAGEMENT]!txtEMPLOYEE_NICKNAME_CHANGE & "'" & _
",[MODIFIED_BY] = '" & UserName() & "', [MODIFIED_DATE] = #" & Now() & "# " & _
" WHERE [EMPLOYEE_ID] = '" & Forms![frm_SALES_EMPLOYEE_MANAGEMENT]!cmbEMPLOYEE_ID & "'"

Change that to a space and along with James' suggestion you should be OK.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,570
Messages
6,120,297
Members
448,954
Latest member
EmmeEnne1979

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top