Remove Date from a Record using SQL VBA (1 Viewer)

Bigmo2u

Registered User.
Local time
Today, 06:06
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:

vbaInet

AWF VIP
Local time
Today, 12:06
Joined
Jan 22, 2010
Messages
26,374
Re: Remove Date rom a Record using SQL VBA

You want to set a particular field within a record to Null?
 

Bigmo2u

Registered User.
Local time
Today, 06:06
Joined
Nov 29, 2005
Messages
200
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.
 

boblarson

Smeghead
Local time
Today, 04:06
Joined
Jan 12, 2001
Messages
32,059
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.
 

Bigmo2u

Registered User.
Local time
Today, 06:06
Joined
Nov 29, 2005
Messages
200
Empty string would be nice, but it is a date field in table.
 

vbaInet

AWF VIP
Local time
Today, 12:06
Joined
Jan 22, 2010
Messages
26,374
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.
 

Bigmo2u

Registered User.
Local time
Today, 06:06
Joined
Nov 29, 2005
Messages
200
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
 

vbaInet

AWF VIP
Local time
Today, 12:06
Joined
Jan 22, 2010
Messages
26,374
I can't see how or where it would throw that error. Let's see the full SQL statement.
 

Bigmo2u

Registered User.
Local time
Today, 06:06
Joined
Nov 29, 2005
Messages
200
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
 

vbaInet

AWF VIP
Local time
Today, 12:06
Joined
Jan 22, 2010
Messages
26,374
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.
 

boblarson

Smeghead
Local time
Today, 04:06
Joined
Jan 12, 2001
Messages
32,059
You don't need the variables. Just

DepartureDate = Null, ..etc
 

Bigmo2u

Registered User.
Local time
Today, 06:06
Joined
Nov 29, 2005
Messages
200
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
 

vbaInet

AWF VIP
Local time
Today, 12:06
Joined
Jan 22, 2010
Messages
26,374
Is it working now? And I noticed that "CurrentLocation ID" has a space between them. Is this correct?
 

Bigmo2u

Registered User.
Local time
Today, 06:06
Joined
Nov 29, 2005
Messages
200
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!
 

vbaInet

AWF VIP
Local time
Today, 12:06
Joined
Jan 22, 2010
Messages
26,374
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

Top Bottom