Optional Date field in access vba Insert

Hi,
i am trying to insert an optional Date variable in a table but i don't want it should have any default date just empty, i tried a few ways but still no success
here is my code

thanks forever

Sub AddUpdateReminder(Optional tbl As String, Optional fld As String, Optional PrimKey As Long, Optional Description As String, _
                       Optional Reminding As String, Optional mValue As String, Optional remdate As String, Optional remtime As String, Optional assigned As String, _
                       Optional remID As Long)

If Not remID > 0 Then       'Insert
remdate = IIf(remdate > "", CDate(remdate), "")
remtime = IIf(Not remtime > "", CDate(remtime), "")

CurrentDb.Execute "Insert Into Reminders(Tbl,Fld,PrimKey,Description,Reminding,mValue, " & _
                    "RemDate,RemTime,Assigned,Created,User) " & _
                    "Values ('" & tbl & "','" & fld & "'," & PrimKey & ",'" & Description & "','" & Reminding & "','" & mValue & "', " & _
                    " " & Nz(remdate, 0) & "," & Nz(remtime, 0) & " ,'" & assigned & "',#" & Now & "#,'" & Cuser & "')"

Else                        'Update

End If

Open in new window

LVL 1
Joseph KrauszCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You could try inserting a NULL instead of a 0 - i.e. Nz(remdate, NULL) - but when I do these sorts of things I "build up" the INSERT and VALUES strings depending on the value passed in. For example:

dim sInsert As String
Dim sValues As String

If Nz(tbl, "") <> "" Then
  sInsert = "Tbl"
  sValues = "'" & tbl & "'"
End If

If Nz(fld, "") <> "" Then
  If Len(sInsert) > 0 Then
    sInsert = sINsert & ","
    sVAlues = sVAlue & ","
  End If
  sInsert = "Fld"
  sValues = "'" & fld & "'"
End If

If Nz(PrimKey,0) <> 0 Then
  If Len(sInsert) > 0 Then
    sInsert = sINsert & ",Fld"
    sVAlues = sVAlue & ","
  End If
  sInsert = sInsert & ",PrimKey"
  sValues = sValues & PrimKey
End If

'/ and so on for all the fields

sInsert = "INSERT INTO MyTable (" & sInsert & ") VALUES(" & sValues & ")"

Open in new window

Using this, I can insure that I only capture and save exactly what I want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOCommented:
Try this:
Sub AddUpdateReminder(Optional tbl As String, Optional fld As String, Optional PrimKey As Long, Optional Description As String, _
                       Optional Reminding As String, Optional mValue As String, Optional remdate As String, Optional remtime As String, Optional assigned As String, _
                       Optional remID As Long)

If Not remID > 0 Then       'Insert
remdate = IIf(IsDate(remdate), Format(DateValue(remdate), "\#yyyy\/mm\/dd\#"), "Null")
remtime = IIf(IsDate(remtime), Format(TimeValue(remtime), "\#hh\:nn\:ss\#"), "Null")

CurrentDb.Execute "Insert Into Reminders(Tbl,Fld,PrimKey,Description,Reminding,mValue, " & _
                    "RemDate,RemTime,Assigned,Created,User) " & _
                    "Values ('" & tbl & "','" & fld & "'," & PrimKey & ",'" & Description & "','" & Reminding & "','" & mValue & "', " & _
                    remdate & "," & remtime ",'" & assigned & "', Now(),'" & Cuser & "')"

Else                        'Update

End If

Open in new window

Also, use Now() inline.

/gustav
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.