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

invalid use of null when reach to textbox with no value

Status
Not open for further replies.

onusonu

MIS
Jun 21, 2005
18
0
0
US
How do I get it to stop when the record is null instead of getting the error "invalid use of null"
Function TotalW() As Double

Dim TotalWeight As Double
Dim SWeight As Double
Dim count As Integer
Dim TWeight As Variant
Dim i As Integer


For i = 1 To 13
If Not IsEmpty(TWeight) Or IsNumeric(TWeight) Or TWeight <> Null Then
TWeight = Me("Wt" & CStr(i))
TotalWeight = TotalWeight + TWeight
MsgBox i
End If



TotalW = TotalWeight

Next i

End Function
 

Hi,

Check out HELP in VB in Null.

It has a VERY SPECIAL meaning

Rather use TWeight <> ""

Skip,
[sub]
[glasses] [red]Be advised:[/red]To be safe on the FOURTH, don't take a FIFTH on the THIRD, or...
You might not come FORTH on the FIFTH! [bomb][tongue][/sub]
 
Can you explain this construct: Me("Wt" &CStr(i))?

For the sake of argument, let's replace that with an array which I'll name arrValues [ Dim'd arrValues(1 to 13) ]. You could re-write your function as:
Code:
Function TotalW() As Double

Dim TotalWeight As Double
Dim SWeight As Double
Dim count As Integer
Dim i As Integer


    For i = 1 To 13
      TotalWeight = TotalWeight + arrValues(i)
    Next i
    TotalW = TotalWeight

End Function

Note, I'm only using an array for example. Also, did you intend to put TotalW = TotalWeight inside the loop? Why?

Regards,
Mike
 
For i = 1 To 13
TWeight = Me("Wt" & i)
If IsNumeric(TWeight) Then
TotalWeight = TotalWeight + TWeight
End If
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you, but now i have another problem.
when I have 3 textbox with numbers,
it loop through the 3 textbox and repeat 2 more times. And when I have 7 textboxes with numbers, it loops through the 7 textbox and another 6 set, each 7 times.
I can't see why it's doing that.

Function TotalW() As Double

Dim TotalWeight As Double
Dim SWeight As Double
Dim count As Integer
Dim TWeight As Variant
Dim i As Integer


For i = 1 To 13
TWeight = Me("Wt" & CStr(i))
If TWeight<>"" Or IsNumeric(TWeight) Or Then

TotalWeight = TotalWeight + TWeight
MsgBox i
End If
next i


TotalW = TotalWeight


End Function
 
Hi PHV,
with the code you showed me, it gave me "invalid use of null" message
 
Me("Wt" &CStr(i)) refers to each textbox. Wt1, Wt2, Wt3...Wt13
 

How is TotalW being called?

Skip,
[sub]
[glasses] [red]Be advised:[/red]To be safe on the FOURTH, don't take a FIFTH on the THIRD, or...
You might not come FORTH on the FIFTH! [bomb][tongue][/sub]
 
it gave me "invalid use of null" message
At which line of code ?
For i = 1 To 13
TWeight = Me("Wt" & i) & ""
If IsNumeric(TWeight) Then
TotalWeight = TotalWeight + TWeight
End If
Next i

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
onusonu,

I just happened to come accross this thread and noticed that no one has suggested the use of the: IsNull() function. You could use:

Code:
    For i = 1 To 13
        TWeight = Me("Wt" & i) & ""
        If Not IsNull(TWeight) Then
            TotalWeight = TotalWeight + TWeight
        End If
    Next i

Also, nesting your if clauses like you did in:
Code:
    For i = 1 To 13
    If Not IsEmpty(TWeight) Or IsNumeric(TWeight) Or TWeight <> Null Then
        TWeight = Me("Wt" & CStr(i))
        TotalWeight = TotalWeight + TWeight
        MsgBox i
    End If
will cause all of your if's to be evaluated, when you probably only need the one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top