Justin
Justin

Reputation: 4539

MS Access 2003 - text box calculation on a form

Lets say I have two text boxes on a form. the first returns a count value from a SQL statement, or a domain aggregate expression, etc. the second does the same with additional parameters.

Now i want to have another text box (#3) that divides one by the other for a very simple percentage. like this for a controlsource:

=[textbox2]/[textbox1]

this works great unless the original counted value returned is a zero. If the first returned value is a zero, then the second is going to be a zero too, and ideally 0 / 0 should come out to zero, but I get a #Num! error string in the text box.

I realize this is yet another weird request but this is for a dashboard form that has about 50 of these, and they work great, unless I hit a zero.

So is there any way I can set text box properties that i may be unaware of, for this to work without having to write numerous If statements in the code?

Thanks!

Upvotes: 0

Views: 2343

Answers (2)

Clon
Clon

Reputation: 1055

Mathematically, the division by 0 is undetermined, but for your purposes, you can calculate it with:

=IIf([textbox1]<>0;[textbox2]/[textbox1];IIf([textbox2]=0;0;"N/A"))

This is, when textbox1 equals 0, you check whether or not textbox2 equals 0. If this is the case, then return 0, which is what you want.

Upvotes: 0

Fionnuala
Fionnuala

Reputation: 91316

I cannot see how you can avoid an if statement when divide by zero is a possibility

=IIf(TextBox1<>0, TextBox2/TextBox1,"N/A")

Upvotes: 1

Related Questions