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

Set Number Field = Null or Zero Length 1

Status
Not open for further replies.

PatrioticTX

Programmer
Dec 5, 2001
18
US
In the process of saving data to a recordset from VB6, I need to store a blank in a Number (Single) field of an Access table. I do not want to store a '0' value, it must be completely blank. When I try to do this, I get a Data Type Conversion Error. I have tried setting the recordset field to both vbNullString and Null using VB, but neither work.

If I am using Access directly, all I have to do is clear the field and it works. It also works when I run an Access Update Query and set the field value to null.

Executing an update SQL from VB is too inefficient. How can I do this in VB? Thank you for your help!
 
Hmmm... Have you tried the double quote?

recordset field = ""
 
Yes. That didn't work either. Sometimes VB can really be a pain.
 
If I used the Empty variable, I would have to change all my data types to
variants. Unfortunately, that is not an option. The variable must remain a
Code:
single
data type.

Further elaboration:
A field on form frmXYZ called [FIELD_ABC] has nothing in it (it is blank).
I am using a recordset to update all fields on the form, one being
[FIELD_ABC]. A generic example of the code is:

Code:
Rs.Edit
Rs!MySingle = Me!FIELD_ABC
Rs.Update

When I run the code to update the recordset, Error 3421 (Data Type
Conversion Error) occurs. As expected, it will save any
Code:
single
number I enter, but it will not store an empty space. Access easily does
it... I haven't been able to get VB6 to do it. Please help!
 
Try inserting a statement like this:

Rs!MySingle = IIf(Me!FIELD_ABC.Text = "", Null, Me!FIELD_ABC.Text)

I've had no problem assigning VB's Null keyword to Access fields that are of type Number->Single.

Hope this helps!
Josh
 
As I mentioned in my first post, I tried to set the field equal to Null. I was unsuccessful a few days ago. I tried what you said and it worked great. I believe my error was occuring because I used a function call to assign Null to the variable if it saw that the textbox was equal to vbNullString. I had set the function output data type to Single instead of variant. I have since decided put the code inline (without IIf) to improve efficiency. Thank you for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top