PDA

View Full Version : ActiveX objectTextboxes names in an array



PKisielewski
07-28-2009, 02:52 PM
I tried to figure this one out and I have reviewed other posts but I have not come up with the answer to my Question. My Question is “How to incorporate arrays in the ActiveX object's names to reduce redundant code”.

I have a Word document (Word 2002)which consists of several ActiveX text boxes that begin with the same name with a number on the end (ex: txt_sev_des1, txt_sev_des2, txt_sev_des3......) I am wanting to use an array to eliminate redundant coding since I am using the same code for each text box. The following is the code for one of the text boxes. Since I have 4 text boxes identical how can I consolidate the code so that I can use just one Sub Routine instead of 4.


Sub txt_sev_des1_Change()
If txt_fin_prod1.Value = "" Then ‘ Checks to make sure user typed in amount if not - exits routine
Exit Sub

Else ‘ Totals are calculated into the final risk field
txt_fin_risk_des1.Value = txt_fin_prob_des1.Value * txt_sev_des1.Value
End If
End Sub


Thanks in advance for your help.

PKisielewski
08-26-2009, 09:45 AM
I still have not heard back from anyone in regards to my Question. Hopefully, someone can help me.

I would however like to refine my Question.
I have multiple text boxes which are created at RUN time (not design). If the user needs additional rows then a row is added which includes the ActiveX text boxes.
These text boxes are numbered txt_prob_des1_1 to ...des1_2, txt_prob_mfg1_1 to mfg1_2 and so on.

The following code works but I have to create each separate change event for each text box.

Sub txt_prob_des1_1_Change()

If txt_prob_des1_1.Value <> "" Then
txt_in_risk_des1_1.Value = txt_prob_des1_1.Value * txt_sev_des1_1.Value

End If

End Sub

But what I would like to happen is that I put a call function in each text box change event. The call statement would like this:

Sub txt_prob_des1_1_Change()
Call run_calc(des1_1)
End Sub

Function run_calc_Change(text_name)

If "txt_prob_" & text_name.Value <> "" Then
"txt_in_risk_" & text_name.Value = "txt_prob_" & text_name.Value * "txt_sev_" & text_name.Value

End If

End Sub

The code for the function does not work. I have also tried every way that I can think of and it still gives me an error. See one example that I tried:

Function run_calc_Change(text_name)

If Me.txt_prob_ & text_name.Value <> "" Then
Me.txt_in_risk_ & text_name.Value = Me.txt_prob_ & text_name.Value * Me.txt_sev_ & text_name.Value

End If


End Sub

Hopefully, someone can help me with this. I would greatly appreciate it.
Thanks in advance.
:banghead:

Tommy
08-26-2009, 12:36 PM
Not knowing exactly what you are doing and having nothing to test it on the only thing I can think of to gentley shove you in a generally correct direction - look into withevents. Make a class and using withevents attach it to the textboxes, check the name to make sure it is one of the textboxes you want to change and .....

If you need more help please attach a working copy with instructions on how to use. Make sure to remove all private/personal information before posting.