NZ() Function not working

TallMan

Registered User.
Local time
Today, 01:57
Joined
Dec 5, 2008
Messages
239
Hello,

I have a form with 7 unbound text boxes on it. The code below is on an On Click event of a button. The code is supposed to look at all of the unbound text boxes, if they are null make them a 0, and then conduct a simple subtraction calculation. For some reason the Nz(stringhere,"0") function is working on all but two of them. I have looked at some of the textbox properties to see if they are the same( for example all locked = no) and I cannotn find anything. Does anyone have any pointers or places for me to start?


PHP:
Dim str1 As Variant
Dim str2 As Variant
Dim str3 As Variant
Dim str4 As Variant
Dim str5 As Variant
Dim str6 as Variant
Dim strtotal As Variant
str1 = Nz(txtValue1, "0")
str2 = Nz(txtValue2, "0")
str3 = Nz(txtValue3, "0")
str4 = Nz(txtvalue4, "0")
str5 = Nz(txtvalue5, "0")
str6 = Nz(txtvalue6, "0")

strTotal = (str1 - str2 - str3 - str4 - str5 - str6)

:banghead:I am completely confused why this is working for some textboxes and not the others. :banghead:

Thank you,
Tallman
 
For starters, if you want 0, use 0:

str1 = Nz(txtValue1, 0)

The quotes make it a string. The textboxes may contain a zero length string rather than Null.
 
The quotes make it a string. The textboxes may contain a zero length string rather than Null.

Same goes for the variable declaration...
Dim str1 As Variant
should be
Dim str1 As String

Remember that a zero length string ("") is not the same as null, they look the same but are not the same

Try
If trim(txtValue1) = "" or txtValue1 is null then txtValue1 = "0"
 
In order to calculate the Total your variables named with str... should NOT be strings.
(I think that namliam don't saw the last line in your post).

Paul's solution should work. But the best approach is to dim variables as numeric:
Dim dbl1 As Double (is only an example)
Dim dbl2 As Double
.......
Dim dblTotal As Double


with this Paul's function become:
dbl1 = Nz(txtValue1, 0)
......

And your Total will be:
dblTotal = (dbl1 - dbl2 - dbl3 - dbl4 - dbl5 - dbl6)
 

Users who are viewing this thread

Back
Top Bottom