Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting Nulls into Date Fields MS Access 2010/2013

Status
Not open for further replies.

67Y10_Doc

IS-IT--Management
Sep 11, 2016
1
0
0
US
In reference to an old post in thread 705-300424, I am writing to give my solution for anyone who needs a way to insert Nulls into a Date field in a table.

Here is what I did. I used a VBA Private Sub to accomplish this goal.

Code:
Private Sub cmdAdd_Click()
    DoCmd.SetWarnings False

DoCmd.RunSQL "INSERT INTO tbl_Test_Date (TestDate, TestText) VALUES (" _
    & IIf(IsNull(dtDate), "'" & Null & "'", "#" & dtDate & "#") & ",'" & Me.Text2 & "');"

tbl_Test_Datesubform.Form.Requery

End Sub

In the above code I'm executing this against a form with 2 unbound text fields.
I have a subform within the main form to see the output of my code (hence the requery of the subform).
In my Test Form, I have one text field set with a format of "Short Date", and the other as a plain text with no format.
The table I am inserting the values into has 3 fields. The first field is an ID AutoNumber. The second field is my Date/Time with a format of "Short Date" (for consistency). The third and final field is a Short Text field with a maximum of 25 characters. I have had no issues inserting normal dates of 9/30/2016, or 10/31/2016. There is no reason to reference the AutoNumber field when performing an insert.

I hope this helps anyone out there who is having issues inserting nulls into a date field.

 
I may be wrong, but the way I see it: you have in your statement: [tt]"'" & Null & "'" [/tt] so you do not insert a NULL value (unknown value) into your [tt]TestDate[/tt] field in your [tt]tbl_Test_Date [/tt]table, you are inserting a word [tt]Null[/tt] because you have single quotes around it.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Try:

Code:
Private Sub cmdAdd_Click()
    DoCmd.SetWarnings False
    Dim strSQL as String
    strSQL = "INSERT INTO tbl_Test_Date (TestDate, TestText) VALUES (" _
    & IIf(IsNull(dtDate), "Null", "#" & dtDate & "#") & ",'" & Me.Text2 & "');"
    debug.print strSQL
    DoCmd.RunSQL strSQL

   tbl_Test_Datesubform.Form.Requery

   DoCmd.SetWarnings True

End Sub

Duane
Hook'D on Access
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top