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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to assign NULL or EMPTY to Numeric Field

Status
Not open for further replies.

Punchinello

Programmer
Apr 25, 2003
116
US
I've read bunches of threads that address NULL and EMPTY issues but none that demonstrates how to assign a NULL or EMPTY value to a numeric or date field. For example, I'm using standard edit controls and assigning their .text properties from an Access table in code like so:
Code:
Ctrl.Text = IIf(IsNull(rs!ADate), "", Format$(rs!ADate, "MM/DD/YYYY"))
But then a user will do this:
1. Enter a date and save the record to the database.
2. Return to the record, blank out the date and save it.

So I want a statement like one of these (neither of which actually work):
Code:
rs!ADate = IIf(Ctrl.Text = vbNullString, Null, CDate(Ctrl.Text))
or
Code:
rs!ADate = IIf(Ctrl.Text = vbNullString, Empty, CDate(Ctrl.Text))
And a similar solution for assigning to number fields. Any ideas?
 
I use the following

If txtDate.Text & "" = "" Then
rs.Fields("FieldName") = Null
Else
rs.Fields("FieldName") = txtDate.Text
End If

 
I think the problem lies in the use of vbNullString in your comparison.

I think that edit control returns a zero-length string (which is not the same as a vbNullString).


Try this:
rs!ADate = IIf(Len(trim(Ctrl.Text)) = 0, Null, CDate(Ctrl.Text))


Greetings,
Rick
 
Thanks for the fast help! Using replies from dvannoy and LazyMe (and my own little pea-sized brain) I've determined that the problem is related to the use of the IIf function in that the following statement:
Code:
rs!ADate = IIf(CtrlIsBlank, Null, CDate(Ctrl.Text))
evaluates the expression CDate(Ctrl.Text) whether or not this value will be used as the result of the function. Therefore, unless Ctrl.Text reflects a valid date, this line will crash. Like in dvannoy's example, my solution is to replace this line with:
Code:
If CtrlIsBlank Then
  rs!ADate = Null
Else
  rs!ADate = CDate(Ctrl.Text)
End If
Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top