public variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have defined a public variable
GDate as date

when I display this variable, it shows 12:00 am.

I tried moving GDATE = NULL, I get error
"Invalid use of Null"

My question is how do I initialize the date variable.
Even for other variables if I give gnumber = null,
I get error. Is this wrong? Then how do I initialize variable
with null values?

Thank you,
-Me
 
My question is how do I initialize the date variable.
Even for other variables if I give gnumber = null,
I get error. Is this wrong? Then how do I initialize variable
with null values?

The only variable data type that can contain a NULL value is a Variant.
All other types have default values (all numbers = 0, text = "",
dates=CDate(0) and so on) but none of them can be null.

If you really want a null, you have to use a variant. This is
particularly relevant when taking values from database fields, which can
(all) contain nulls and frequently do.

Hope that helps


Tim F
 
Tim,

Thanks for your reply!
How do I use variants in my case, like for date field? Can you send me some
examples as I never used it?

Thank you very much!
-Me
 
Tim

To declare a variant use:

dim dateField as variant

I should point out that using varinats slows your code as access has to worl
out the data type. Better to declare the field type and initialise with a
value.

Regards
Paul
 
How do I use variants in my case, like for date field? Can you send me
some examples as I never used it?

.... like Paul says

Dim varGDate as Variant

'...

varGDate = Null

If IsNull(varGDate) Then
' ... etc

ElseIf IsDate(varGDate) Then
' ... etc

Else
' just in case you managed to get some other
' rubbish data in there:

The biggest disadvantage of using Variants is that you lose what little
strong typing is available in VBA, so you need to check what data type is
actually inside it every time, particularly if there is any chance of
user-entered data getting in (try varGDate = "13/14/2005").

Hope that helps. The Help files are pretty explicit on data types and
swapping between them.

Tim F
 
Back
Top