Search code examples
functionvbams-accessnullsubroutine

Null Error on Button Click


I have an access database that has a number of calculations in it. One the the issues I am having is I have multiple textboxes and access want me to enter data into all of them before it calculates the formula. I keep getting an error when I leave a textbox blank

Run-time error '94': Invalid use of Null

How do I set it to ignore all the nulls. Here is my code

Public Function calculate() as double 
calculate = cdbl(textbox1.value) * cdbl(textbox2.value) * cdbl(textbox3.value) * cdbl(textbox4.value) / 144
End Function

Private Sub btn1_click()
Dim x as double 
x = calculate
textbox5.value = x
End Sub 

Any help would be aprecciated. Thanks!


Solution

  • In your circumstance, I'd use the NZ method.

    calculate = cdbl(nz(textbox1.value,1)) * cdbl(nz(textbox2.value,1))  * cdbl(nz(textbox3.value,1))  * cdbl(nz(textbox4.value,1))  / 144