Enter Null into a date field

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Enter Null into a date field

Post by Peter Kinross »

I have tried this every which way, but if I try to enter a Null, "", vbNullstring, Empty or vbNull into a date field I get an error. There must be a way of setting a date field so it has nothing in it in code, but it eludes me. I can delete the values manually and don't get an error.
Any ideas anyone?
Avagr8day, regards, Peter

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Enter Null into a date field

Post by JohnH »

What happens if you just use: Me.EventDate=Null ?

Works for me.
Regards

John

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Enter Null into a date field

Post by HansV »

In an update query, you should use Null in the Update to: row, and in VBA, you should be able to use code such as

Me.SpecialDate = Null

To make sure, I have tested both just now; they did what was expected without error.

This won't work if the date field is required of course.
Best wishes,
Hans

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: Enter Null into a date field

Post by Peter Kinross »

Can't get that far.
Dim TargetDateIn As Date
TargetDateIn = Null
gives error:
Invalid use of Null

I tried using DtIn dimmed as variant, then testing if IsNull(DtIn) and if so setting the date field to Null.
It works.
Last edited by Peter Kinross on 23 Aug 2017, 07:13, edited 1 time in total.
Avagr8day, regards, Peter

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Enter Null into a date field

Post by JohnH »

You are trying to set a variable to Null rather than a field.

Only variables of type Variant can be Null.
Regards

John

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: Enter Null into a date field

Post by Peter Kinross »

Thanks John. You were answering and I was editing at the same time.
Avagr8day, regards, Peter

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Enter Null into a date field

Post by HansV »

Keep in mind that TargetDateIn is a variable, not a field.
Best wishes,
Hans