Access VB Control via Variables? 
Author Message
 Access VB Control via Variables?

Hi,

VB.net doesn't support Control Arrays.
Does anyone have a more efficient method for accessing
controls, than the (New VB Method shown below) which uses Select
Statements.

Something like creating the name of the control in a variable
and then using the variable to access the field.
example:
        dim controlname as string, fa(10) as string

        for i = 1 to 10
                controlname = "screencontrol_" & cstr(i)
                fa(i) = Me.controlname
        next i

Thanks,
Ed

************************************************
Old VB Method:

        dim fa(10) as string

        for i = 1 to 10
                fa(i) = screencontrol(i)
        next i

************************************************
New VB.Net Method:

        dim fa(10) as string

        for i = 1 to 10
                select case i
                        case 1
                                fa(i) = screencontrol_1
                        case 2
                                fa(i) = screencontrol_2
                        case 3
                                fa(i) = screencontrol_3
                        ...
                end select
        next i
************************************************



Sun, 30 Jan 2005 10:07:47 GMT  
 Access VB Control via Variables?
Ok, I figured it out.  Here's a working version.
Now you only have to call SCV or GCV.  
(short for SetControlValue and GetControlValue).

Old Method:       label(i) = farray(i)
New Method:       SCV "label" & i, farray(i)

I CAN LIVE WITH IT.

OK, I'm Back to working with VB.Net
Ed

=================================================================
Private Sub Form_Load()

        Rem SIMPLE TEST.  CREATE ONE FORM WITH THESE 3 LABELS,
        Rem PASTE ALL THIS CODE IN IT,  AND RUN IT !
        Rem LABEL1, LABEL2, LABEL3
        Rem NOTE: DON'T MAKE THEM CONTROL ARRAYS.  USE THE DEFAULT NAMES.

        Dim fieldname As String
        Dim farray(3) As String

        farray(1) = "Ed"
        farray(2) = "is"
        farray(3) = "here"

        For i = 1 To 3
                SCV "label" & CStr ( i ), farray( i )
        Next i
End Sub

========== YOU NEVER HAVE TO LOOK AT THESE FUNCTIONS AGAIN =========
                                   AFTER YOU ADD THEM TO YOUR CODE
==================================================================
Public Sub SCV(fieldname As String, svalue As String)
    Dim myControl As Control, ctrlname As String
    Set myControl = GetControlFromName(fieldname)
    myControl = svalue
End Sub

Public Sub GCV(fieldname As String, svalue As String)
    Dim myControl As Control, ctrlname As String
    Set myControl = GetControlFromName(fieldname)
    svalue = myControl
End Sub

Public Function GetControlFromName(ByVal ControlName As String) As Control
    Dim tempControl As Control
    For Each tempControl In Me.Controls
        If UCase(tempControl.Name) = UCase(ControlName) Then
            Set GetControlFromName = tempControl
        End If
    Next
End Function

=================================================================



Quote:

>Hi,

>VB.net doesn't support Control Arrays.
>Does anyone have a more efficient method for accessing
>controls, than the (New VB Method shown below) which uses Select
>Statements.

>Something like creating the name of the control in a variable
>and then using the variable to access the field.
>example:
>        dim controlname as string, fa(10) as string

>        for i = 1 to 10
>                controlname = "screencontrol_" & cstr(i)
>                fa(i) = Me.controlname
>        next i

>Thanks,
>Ed

>************************************************
>Old VB Method:

>        dim fa(10) as string

>        for i = 1 to 10
>                fa(i) = screencontrol(i)
>        next i

>************************************************
>New VB.Net Method:

>        dim fa(10) as string

>        for i = 1 to 10
>                select case i
>                        case 1
>                                fa(i) = screencontrol_1
>                        case 2
>                                fa(i) = screencontrol_2
>                        case 3
>                                fa(i) = screencontrol_3
>                        ...
>                end select
>        next i
>************************************************



Sun, 30 Jan 2005 11:45:22 GMT  
 Access VB Control via Variables?

Quote:

>Something like creating the name of the control in a variable
>and then using the variable to access the field.
>example:
>    dim controlname as string, fa(10) as string

>    for i = 1 to 10
>            controlname = "screencontrol_" & cstr(i)
>            fa(i) = Me.controlname
>    next i

        fa(i) = Me.Controls(controlname)
or, since Controls is a form's default collection,
        fa(i) = Me(controlname)

--
Marsh
MVP [MS Access]



Sun, 30 Jan 2005 12:21:53 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. access to variable via namestring

2. Two Questions - Making a userform active / Accessing a userform via a variable

3. accessing sql variables via vbs

4. Sending Variables Via the Winsock Control

5. Accessing encrypted data in Access 2000 DB via VB

6. Accessing Access report via VB

7. Showing VB Forms via string variable

8. Using Oracle bind variables from VB via ADO

9. Setting environment Variables via VB Script

10. Print via VB-Script ActiveX-Control in VB-Application

11. Connecting to Access via Visual Basic via Internet

12. Control Properties on Access forms via DAO

 

 
Powered by phpBB® Forum Software