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 86

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 86

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?
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Put text in a picture ASP.NET C# 2 73
MS Access 03, TransferText, decimal places 8 92
Dinamic report to Crosstab query 9 60
VBA Replace Function - What am I missing? 3 52
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 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