Solved

Calling function to fill textbox

Posted on 2006-10-19
6
187 Views
Last Modified: 2010-04-30
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
Comment
Question by:JamesBrian
  • 3
  • 2
6 Comments
 
LVL 85

Accepted Solution

by:
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
    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
 

Author Comment

by:JamesBrian
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)
Next

How can I incorporate these four different textboxes in the same call?
0
 
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?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:JamesBrian
ID: 17767540
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
 
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 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
 

Author Comment

by:JamesBrian
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
        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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now