Syntax Error while defining Date Variable

Firasath

Board Regular
Joined
Feb 20, 2011
Messages
134
Office Version
  1. 365
Hi,

I am picking up a date from the form in VBA Code and trying to execute SQL Statement. I am getting "Run Time Error "3075". Syntax Error (missing operator) in query expression". Please find below the respective Code.

Code:
Private Sub btnDeldt_Click()

Dim dt As Date

dt = Me.txtDelDt

Dim sql As String
sql = " Delete tblCompDB.ActualClosedDate WHERE (((tblCompDB.ActualClosedDate)>= #" & dt & "# "
DoCmd.RunSQL sql

sql = " Update tblCompDB.ComplaintStatus = 'Pending' WHERE (((tblCompDB.ActualClosedDate)>= #" & dt & "# "
DoCmd.RunSQL sql

End Sub

Kindly suggest.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I don't think the date is the problem.

The DELETE statement should look like this.

sql = " Delete tblCompDB WHERE tblCompDB.ActualClosedDate>= #" & dt & "# "

The UPDATE statement should look like this.

sql = " UPDATE tblCompDB SET ComplaintStatus = 'Pending' WHERE tblCompDB.ActualClosedDate>= #" & dt & "# "
 
Last edited:
Upvote 0
Thanks. But it is giving the same error.

I think even if it is get executed it might delete the whole records wherever date is more than specified criteria. My purpose is to remove the date only from ActualClosedDate field.
 
Upvote 0
If you just want to delete a field value use an UPDATE query and set the field to an empty string.
 
Upvote 0
As per your advised Nourie, I have changed the query as below.

Code:
Private Sub btnDelDt_Click()

Dim sql As String
Dim dt As Date

dt = Me.txtDelDt

sql = " Update tblCompDB " _
        & " Set ActualClosedDate = "", ComplaintStatus = 'Pending' " _
        & " where ActualClosedDate >= #" & dt & "# "

DoCmd.RunSQL sql

End Sub

Now I am getting "Run Time Error '3075': Syntax Error in string in query expression.... " error. Any advise.
 
Upvote 0
Could it be because I have the date format "dd/mm/yyyy h\:nn\:ss AM/PM" in the Table and in the form I am selecting Short Date for the Text Box.
 
Upvote 0
I don't think the date format would cause a syntax error, more likely the results of the queries would be incorrect.

Have you tried printing your SQL to the Immediate Window using Debug.Print, copying it and running it manually?
 
Upvote 0
I will try that and update you.

In the meantime, I have made it work by creating a Query in Access.
 
Upvote 0
Code:
Private Sub btnDelDt_Click()


Dim sql As String
Dim dt As Date


dt = Me.txtDelDt


sql = " Update tblCompDB " _
        & " Set ActualClosedDate = "", ComplaintStatus = 'Pending' " _
        & " where ActualClosedDate >= #" & dt & "# "


DoCmd.RunSQL sql


End Sub


I think the problem is
Set ActualClosedDate = ""
I'm surprised it runs at all


wouldn't the first double quote there
close this string
& " Set ActualClosedDate = "


try this


Code:
Private Sub btnDelDt_Click()


Dim sql As String
Dim dt As Date


dt = Me.txtDelDt


sql = " Update tblCompDB " _
        & " Set ActualClosedDate = null, ComplaintStatus = 'Pending' " _
        & " where ActualClosedDate >= #" & dt & "# "


DoCmd.RunSQL sql


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,131
Members
449,097
Latest member
mlckr

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