execute sql with textbox for date (1 Viewer)

NT100

Registered User.
Local time
Tomorrow, 01:07
Joined
Jul 29, 2017
Messages
148
I've the following SQL and update.

sAppt = "UPDATE tblTAppt SET ApptStart=#txtApptStart#, ApptEnd=#txtApptEnd#, Completed=chkCompleted, " & _
" AppointedBy=txtApptedBy, RankCode=fraHSR, JobPosition=txtJobPosition, Dept=sDept, Company=sCompany, " & _
" Honorarium=sHon, Allowance=sAll, FCF_No=sFCF_No, Budget=iBudget, DaysNotAvailable=sDaysNotAvailable, Remark=sRemark " & _
" WHERE tblTAppt.TRef = iID and iTApptID = txtTApptID"

CurrentDb.Execute sAppt

It runs into run-time error "3075", Syntax error in date in query expression '#txtApptStart'.

It is weird that it's "#txtApptStart#" but came out as "#txtApptStart".

Note: txtApptStart is format as "Medium Date".

I would be appreciated if you have any suggestions on this problem
 

Ranman256

Well-known member
Local time
Today, 13:07
Joined
Apr 9, 2015
Messages
4,339
I would use a query instead of SQL, it gets the syntax correct...
select * from table where [dtefld] = forms!myForm!txtDate

or
for sql , you don't need date delimeters for an update, but the textbox must be outside the quotes
or
put delimeters INSIDE the quotes, but the textbox OUTSIDE,

sAppt = "UPDATE tblTAppt SET ApptStart=#" & txtApptStart & "#, …"
 

NT100

Registered User.
Local time
Tomorrow, 01:07
Joined
Jul 29, 2017
Messages
148
Thank you for SQL construct.

Is there any alternative to implement the SQL?

Best.
 

Micron

AWF VIP
Local time
Today, 13:07
Joined
Oct 20, 2018
Messages
3,478
EVERY variable has to be outside of the quotes, not just the control references. Since all is in quotes, Access will see

...AppointedBy=txtApptedBy...
not
...AppointedBy= 1254...


which raises another point. Any text values (not numbers) have to be delimited also. So your sql may need to look more like
Code:
sAppt = sAppt & " AppointedBy= " & txtApptedBy & ", RankCode= " & fraHSR & ", JobPosition= " & txtJobPosition
sAppt = sAppt & ", Dept= " & sDept & ", Company= " & sCompany & ", Honorarium= " & sHon & ", Allowance= " & sAll
sAppt = sAppt & ", FCF_No= " & sFCF_No & ", Budget= " & iBudget & ", DaysNotAvailable= " & sDaysNotAvailable
sAppt = sAppt & ", Remark= " & sRemark & " WHERE tblTAppt.TRef = " & iID & " AND iTApptID = " & txtTApptID
I'm showing another method of writing sql for vba. I don't like to use line continuation characters - just my preference I guess. However, I'm assuming there are no text values that need to be delimited. As for a better way, what you're doing is common; it just takes practice to get the concatenation correct. Suggest you debug.print sql before trying to execute. Then you can either just look at it in the immediate window, or copy/paste into a new query to test as well. I would get in the habit of switching to datasheet view rather than executing the test query because you may not want to accidentally run an update query that you're testing.
 

Users who are viewing this thread

Top Bottom