Remove Date from a Record using SQL VBA

Bigmo2u

Registered User.
Local time
Today, 15:00
Joined
Nov 29, 2005
Messages
200
I am trying to do an update query via vba sql . I have tried setting the variable to Null and to "" and throws an error, I am sure that this is an easy solution, but my brain is fried and tried searching see if anyone has had this issue.

Code:
With db
    .Execute "UPDATE tblSerialNumber " & _
             "Set ModelID = " & mdi & ", PickupLocationID = " & pul & ", DeliveredByID = " & dbi & ", " & _
             "PickupTime = #" & pkt & "#, PickupDate = #" & pud & "#, ArrivalDate = #" & ald & "#, " & _
             "ArrivalTime = #" & ald & "#, ArrivalTime = #" & alt & "#, eDriverID = " & edi & ", " & _
             "CurrentLocation ID = " & cli & ", ReturnLocationID = " & rli & ", DepartureDate =  " & _
             "#" & dpd & "#, DepartureTime = #" & dpt & "# " & _
             "WHERE SerialNumberID = '" & ssn & "'"
End With

The ones that are giving me issues is dpd and dpt, they are declared dates. I don't want to delete the record just remove the date and time from the record.

Thank you in advance for the help.
 
Last edited by a moderator:
Re: Remove Date rom a Record using SQL VBA

You want to set a particular field within a record to Null?
 
Re: Remove Date rom a Record using SQL VBA

Basically yes, If there is a date in that field I want the date removed and to be blank or Null as you put it.
 
Basically yes, If there is a date in that field I want the date removed and to be blank or Null as you put it.

Blank (empty string) is different from Null.

Date/Time fields can either have a value or be Null, you can't store an empty string in one.
 
Empty string would be nice, but it is a date field in table.
 
1. Add extra criteria to get those records with non-empty dates. That is,
Code:
... AND Not [DateField] Is Null
2. Set the field to Null in your query
Code:
"Set [DateField] = Null, " & ...etc
Note how that is written above.
 
I tried

dpd = Null
dpt = Null

and it said invalid use of Null.

Would SQL look like this:

Code:
DepartureDate = Null, DepartureTime = Null

In SQL string
 
I can't see how or where it would throw that error. Let's see the full SQL statement.
 
Code:
With db
    .Execute "UPDATE tblSerialNumber " & _
             "Set ModelID = " & mdi & ", PickupLocationID = " & pul & ", DeliveredByID = " & dbi & ", " & _
             "PickupTime = #" & pkt & "#, PickupDate = #" & pud & "#, ArrivalDate = #" & ald & "#, " & _
             "ArrivalTime = #" & ald & "#, ArrivalTime = #" & alt & "#, eDriverID = " & edi & ", " & _
             "CurrentLocation ID = " & cli & ", ReturnLocationID = " & rli & ", DepartureDate =  " & _
             "#" & dpd & "#, DepartureTime = #" & dpt & "# " & _
             "WHERE SerialNumberID = '" & ssn & "'"
End With
 
That's the same query you have in your first post. I would like to see your amended query based on my suggestions in post #6. You haven't written DepartureDate = Null there.

Perhaps what you're doing is setting the value of the variable to Null and trying to assign it to the SQL string, but that wasn't what I advised.
 
You don't need the variables. Just

DepartureDate = Null, ..etc
 
Sorry vbaInet, I miss understood. it now looks like:

Code:
With db
    .Execute "UPDATE tblSerialNumber " & _
             "Set ModelID = " & mdi & ", PickupLocationID = " & pul & ", DeliveredByID = " & dbi & ", " & _
             "PickupTime = #" & pkt & "#, PickupDate = #" & pud & "#, ArrivalDate = #" & ald & "#, " & _
             "ArrivalTime = #" & ald & "#, ArrivalTime = #" & alt & "#, eDriverID = " & edi & ", " & _
             "CurrentLocation ID = " & cli & ", ReturnLocationID = " & rli & ", DepartureDate =  " & _
             "Null, DepartureTime = Null " & _
             "WHERE SerialNumberID = '" & ssn & "'"
End With
 
Is it working now? And I noticed that "CurrentLocation ID" has a space between them. Is this correct?
 
Not only did I have a space there I had ArrivalTime twice... i swear my brain is fried. Thank you vbaInet and Boblarson, the code works as intened! Thank you so much!
 
You might find it helpful to first build your query in the Query Builder/Designer then get a copy of the SQL statement by going to SQL View. That will help you to avoid problems like this.

Happy developing!
 

Users who are viewing this thread

Back
Top Bottom