Calling function to fill textbox

Posted on 2006-10-19
Last Modified: 2010-04-30

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
'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
End Function

but I keep getting errors??
Question by:JamesBrian
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 85

Accepted Solution

Mike Tomlinson earned 125 total points
ID: 17766727
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
    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)
End Sub

Author Comment

ID: 17766981
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)

How can I incorporate these four different textboxes in the same call?
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17767008
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?
Independent Software Vendors: 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!


Author Comment

ID: 17767540

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...

LVL 11

Assisted Solution

by:Dany Balian
Dany Balian earned 125 total points
ID: 17771718
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 = T Then
            If ctl.Index >= FromIndex And ctl.Index <= ToIndex Then
                ctl.Text = "" ' read actual data from ur array
            End If
        End If
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...


Author Comment

ID: 17773031
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
    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)
            Exit Sub
        End If
        Y = Y + 1


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Validating VB6 Function 19 68
Paint/Redraw window while dragging 16 86
Automatic Email Reminder 4 71
Formula problem with Excel attachment 6 43
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question