Why is my vba microsoft access textbox not calculating?
VBA Textbox not calculating
Private Sub Textbox3()
Me.Textbox3 = (IIf(Me.Textbox1 = "", 0, Me.Textbox1) + 0) + (IIf(Me.Textbox2 = "", 0, Me.Textbox1) + 0)
End Sub
Microsoft AccessVBA
Last Comment
Martin Liss
8/22/2022 - Mon
Martin Liss
I would not name your sub Textbox3 since that is apparently also the name of a textbox.
Joseph S
ASKER
What should my sub be?
Dale Fye
Actually, you don't really need any code at all.
In design view, select your textbox3 (I always rename all of my controls to something meaningful).
Display the properties dialog and on the 2nd tab, in the ControlSource box enter something like:
This should check for either NULL or an empty string in textbox1 or textbox2. I assume that you wanted to add the value from Textbox2 in the 2nd half of your equation, whereas your code above references textbox1 as the value to sum if textbox2 is empty.
I want to be able to do like in excel, where cell1=1, cell2=2 , and cell3 =3. Then when you are in cell4=sum(cell1:cell3). With no buttons. I have everything written in vba.
I watched tutorials online, and I have it written same way. I don't understand why it worked from them, but doesn't work for me.
Mark Edwards
Hey Joe: Sounds like a case of monkey see, monkey do. That's a hard one to troubleshoot since it means even the slightest difference anywhere could cause an issue (i.e. your use of the textbox1 value instead of the textbox2 value in your second addition factor, and the "+0" in each factor - not needed.)
Dale's formula should work as long as all values in the textbox are either Null, an empty string, or a number. If anything in textbox1 or 2 is anything else, it will throw an error. i.e. a 2 plus a space " " will cause an error.
Also, Access is NOT a spreadsheet and doesn't act/behave like one. SUM(Field1:Fieldn) is NOT something you can do in Access (although you probably already know that).
By the way, are your textboxes bound to an underlying table/query or unbound. If they are unbound, then it will add strings (i.e "1" + "2" = "12"). You'll have to change the formula to include a force change of the value to a number.
Martin Liss
Please describe in words what you are attempting to do in your Me.Textbox3 = (IIf(Me.Textbox1 = "", 0, Me.Textbox1) + 0) + (IIf(Me.Textbox2 = "", 0, Me.Textbox1) + 0) line.