 |
| Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 18th, 2006, 01:02 PM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Easy Question: Setting a variable to null
Ok, so I've searched the internet and help files and I still haven't found an answer. Is there any way in VBA to set a variable back to null once it has been assigned a value?
Thanks for your help
-Enoch
Enoch
__________________
Enoch
|
|

May 18th, 2006, 01:04 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
What kind of variable is it?
Mike
EchoVue.com
|
|

May 18th, 2006, 02:00 PM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
An integer, the truth is I avoided the problem by just initializing it to a negative number and then treating -1 as null, because I really didn't need any of the differences null offers. But I was wondering for future reference.
Thanks
-Enoch
Enoch
|
|

May 18th, 2006, 02:07 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
That Works! Actually I don't think that you can set an integer to null. When it is initialized, I think it is assigned a default value of 0, but that is as close as it ever gets to null.
Mike
EchoVue.com
|
|

May 19th, 2006, 08:52 AM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks once again for your help
Enoch
|
|

May 19th, 2006, 09:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
As long as the table does not require a value to be input for the record, you should be able to set the integer to null (even if the default is zero) simply by doing something like this for the textbox that is bound to the field.
Me.txtInteger = Null
I assume you can do this because what if there truly is no value for that record? A zero is misleading because you think that the actual value is zero when the value is really "nothing was ever entered."
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

May 19th, 2006, 12:37 PM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
That last one would do it for the field value.
As far as setting an integer variable to Null, it can't be done. Integers are initialized as 0. Null means no value, so that Null can never equal Null, but 0 = 0. So Null has a different meaning.
Once you declare the variable as an integer, Null is not available to you.
mmcdonal
|
|

May 22nd, 2006, 09:35 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
Actually, if you do not specify a default field value when you create a table and you do not specify a default field value in the form based on the table, then an integer field is NOT initialized to zero. It's null.
An integer VARIABLE created in VBA may be initialized to zero automatically upon runtime of the VBA code, but not a field in the table.
I just tested it. I created a table, added an integer field, took away the default value setting, and left it blank. When I started entering data, the default integer value was null.
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

May 22nd, 2006, 10:30 AM
|
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
Thanks Greg,
I stand corrected, but more informed - I think my dabblings in java a few years back are coming back to haunt me :)
Mike
Mike
EchoVue.com
|
|
 |