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

How to pass a control to general sub?

Status
Not open for further replies.

FirasSHhady

Programmer
Dec 5, 2004
14
SA
I have design a form as an Invoice with 15 row, each consist of tow comboboxes and 3 TextBoxes, txtVal, txtNum, txtPrice.

When anyone of these 3 textboxes change I wanna change the value of txtVal TextBox.

And to do that I named each controls line with line number (i.e txtVal1, txtNum1, txtPrice1, ....) and so on.

I try to make a general sub to calc the value each time one of these 3 textboxes change and passing thoes textboxes all
not just in chengend line writting this:

Sub CalcVal(Num As TextBox, Price As TextBox, Val As TextBox)
Dim txtVals(1 To 15) As Integer
Dim sum As Long
Dim tempVal As String
Dim i As Integer
'
For i = 1 To 15
Me("Val" & i) = Me("Num" & i) * Me("Price" & i)
Next
'
For i = 1 To 15
tempVal = Me("Val" & i).Value
txtVals(i) = CInt(tempVal)
Next
'
sum = 0
For i = 1 To 15
sum = sum + txtVals(i)
Next
frmInvoice.txtInvoVal = 0
frmInvoice.txtInvoVal = sum

End Sub


and in the change Event of textboxes I write:
Private Sub txtPrice2_Change()
Call CalcVal(txtNum2, txtPrice2, txtVal2)
End Sub

but I always have error meassage.
Any ideas.

 


Hi,

What application?

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
And which message on which line of code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I'm using Excel 2007
the meassage appear in the change event code told me that type miss match.
 



Please post the change event code and IDENTIRY the statement that errors.

Use your DEBUG button!

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Anyway, CalcVal don't use its parameters, so why not simply get rid of them ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

I just put 2 rows of text boxes, and one called txtInvoVal
Code:
Option Explicit

Private Sub CalcVal()
Dim sum As Long
Dim i As Integer

For i = 1 To 2
    Me.Controls("Val" & i) = Me.Controls("Num" & i) * Me.Controls("Price" & i)
Next

sum = 0
For i = 1 To 2
    sum = sum + Me.Controls("Val" & i).Text
Next

frmInvoice.txtInvoVal = sum

End Sub

Private Sub Price2_Change()
Call CalcVal
End Sub
Works for me, I hope it will work for you, too.

Have fun.

---- Andy
 
Dear SkipVought
I attach the invoice form and code window snapshot.

Dear Andrzejek
I htink I've to pass the textboxes name bcz i wanna the General sub to use different set of textboxes each time.

But what I'm failing at is that I cann't told the general sun what is that textboxes.

I think if u see the attachment it'll give u the idea of what i mean.

see the pics here:





thank u both gentleman
 



your calling variables must be declared the same data type as the arguments in the called procedure.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


...and you might chage the Textbox declaration to Control

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top