handling Null values...

  • Thread starter Thread starter Brad Pears
  • Start date Start date
B

Brad Pears

I have some code that is updating a DB table using SQL.

There are various field types, currency, boolean and string

I am using a function to set a value and in that function I want to check
for null values. However, the value passed to the function is of type
variant because it could be of various data types.

What syntax is there to check the "type" of variable passed to the function
so that I can code accordingly based on the variable type??

here is an example...

curBasePrice = SetValue(Price) ' Currency type
bolTaxExempt = SetValue(TaxExempt) ' boolean
strName = setValue(Name) ' Cust Name

Private Function SetValue(Value as variant)
if isnull(Value) then
' Check to see what type of data we have
' Pseudo code here - this is what I am unsure of...
if Value.type is Currency then
Value = 0
else if value.type is Boolean then
Value = False
else if value.type is string then
value = ""
endif

End Function

Thanks,

Brad
 
Hi,
Use the TypeName function:

Dim var As Variant

var = "hi there"

MsgBox TypeName(var)
 
I tried that but the "TypeName" function is returning a value of "textbox"
for variables I have declared as "Currency". Where would it be getting
"textbox" from???

Thanks,

Brad
 
Hi,
I don't see that behavior here, plus it you declare them as currency, you already know
the type!

Looking at your function again, make sure you're not mistakenly passing a textbox object when
you think you're passing the value.

Also, if you have code like this:
Dim cur as Variant

cur = 12.50

The type will be Double, not Currency
 
The function is operating on various types of data so I will not know what
the type is each time - that's why I'm using a generic function.

I just used currency as a test to see what it would produce and discovered
that it said textbox...

I will check it again...
 
Hi,
This statement:
curBasePrice = SetValue(Price)

Is Price the name of a text box on your form?
If it is, that's your problem.
If your text box is bound and you change your call to this:
curBasePrice = SetValue(Price.Value)

it might work for you.
 
Thanks for that... It seems to be working now...
Dan Artuso said:
Hi,
This statement:
curBasePrice = SetValue(Price)

Is Price the name of a text box on your form?
If it is, that's your problem.
If your text box is bound and you change your call to this:
curBasePrice = SetValue(Price.Value)

it might work for you.
 
Back
Top