Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Calling function to fill textbox

Hi,

I am trying to fill my textboxes using a function:

the actual textbox is in an array so it's named  txtM1(index)

I am trying to call the function :
....
Box_Down (txtM1)
.........


Private Function Box_Down(ByVal name As TextBox)
Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is TextBox Then
            ctl.Text = ""
        End If
    Next
'For z = 0 To 11
z = 0
While Not (z + arrCounterCode) = UBound(arrVerify) + 1
    Set name(z).Text = arrVerify(z + arrCounterCode, 0)
       Set name(z).Text = arrVerify(z + arrCounterCode, 1)
          Set name(z).Text = arrVerify(z + arrCounterCode, 2)
               Set name(z).Text = arrVerify(z + arrCounterCode, 3)
    z = z + 1
Wend
'Next
End Function

but I keep getting errors??
0
JamesBrian
Asked:
JamesBrian
  • 3
  • 2
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Several problems in there...

(1) To pass a Control Array, declare the parameter as Object.
(2) You are not returning anything so use a Sub not a Function.
(3) "Name" is not a good choice as this is already being used.
(4) Don't use "Set" with the "Text" property.
(5) Why are you setting the "Text" property of the SAME TextBox multiple times in the same iteration?
(6) The loop uses UBound(arrVerify) but we are accessing a Control Array.  Are you sure these two things are the same size?

Private Sub Command1_Click()
    Box_Down txtM1
End Sub

Private Sub Box_Down(ByRef ctlArray As Object)
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is TextBox Then
            ctl.Text = ""
        End If
    Next
       
    Dim z As Integer
    For z = 0 To UBound(arrVerify)
        ctlArray(z).Text = arrVerify(z + arrCounterCode, 0)
        ctlArray(z).Text = arrVerify(z + arrCounterCode, 1)
        ctlArray(z).Text = arrVerify(z + arrCounterCode, 2)
        ctlArray(z).Text = arrVerify(z + arrCounterCode, 3)
    Next
End Sub
0
 
JamesBrianAuthor Commented:
Idlemind said :
5) Why are you setting the "Text" property of the SAME TextBox multiple times in the same iteration?

oops, my mistake

originally, I called different textboxes in this loop

For z = 0 To 11
    textbox1(z).Text = arrVerify(z + arrCounterCode, 0)
       textbox2(z).Text = arrVerify(z + arrCounterCode, 1)
          textbox3(z).Text = arrVerify(z + arrCounterCode, 2)
               textbox4(z).Text = arrVerify(z + arrCounterCode, 3)
Next

How can I incorporate these four different textboxes in the same call?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Next stupid question:

    Why are we passing in a Control Array to Box_Down() if you want to update SPECIFIC control arrays in your code?

Take a step back...

What is it you want to achieve with this procedure?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JamesBrianAuthor Commented:
Well,

I have an array that holds 4 values(columns) per row
My screen shows 12 rows at a time.
When I come to the bottom of the screen, I want to show the next 12 rows and so on.

Thats why, when I get to the "new screen", I am looping through the array (arrverify), and I am
updating 4 textboxes (the 4 values)

I was thinking that something like this might do the trick:

Box_Down(txtM1,txtM2,txtEasy,txtQty) (these are the actual textbox names)

This explains also why the textboxes are in an array :

txtM1(0) txtM2(0)  txtEasy(0)  txtQty(0)
txtM1(1) and so on, all the way down to 11

Hope this sheds some light...

0
 
Dany BalianCTOCommented:
i dont know if i got u straight or not...

here's a function that will reset the values of all the objects on ur form that have the same name and that have indexes in a specific range

Private Sub Box_Down(ByVal T As String, FromIndex As Integer, ToIndex As Integer)
    Dim i As Integer, ctl As Control
    For Each ctl In Me.Controls
        If ctl.name = T Then
            If ctl.Index >= FromIndex And ctl.Index <= ToIndex Then
                ctl.Text = "" ' read actual data from ur array
            End If
        End If
    Next
End Sub

you call the function by saying:

Box_Down  "txtM1", 0,3
this will change the values of the textboxes in the controlarray txtm1 that have indexes 0,1,2,3
please note that "txtM1" is a string value which corresponds to the name of the textbox...

hope this helps...
cheers,

Dan
0
 
JamesBrianAuthor Commented:
This is how I've done it, and got it to work:  
              Box_Up txtM1, 0
               Box_Up txtM2, 1
                Box_Up txtEasy, 2
                 Box_Up txtQty, 3

Private sub Box_Up (ByRef ctlArray As Object, position As Integer)
Dim ctl As Control
On Error GoTo errhandler
    If position = 0 Then
        For Each ctl In Me.Controls
            If TypeOf ctl Is TextBox Then
                ctl.Text = ""
            End If
        Next
    End If
    Dim z As Integer
    Y = 0
    For z = 0 To UBound(arrVerify)
        If Not Y + arrCounterCode = UBound(arrVerify, 2) + 1 Then
            ctlArray(z).Text = arrVerify(position, z + arrCounterCode)
        Else
    txtM1(0).SetFocus
            Exit Sub
        End If
        Y = Y + 1
    Next



0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now