Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting a string to a control a control name

Status
Not open for further replies.

nickjar2

Programmer
Jun 20, 2001
778
0
0
US
Wonder if anyone can help. I am looping through the controls on a form. If a check box is found i need to check if the coresponding text box is enabled. I do a bit of string manipulation to convert the check box name into the text box name. Below is the code I have at the mo:

For Each cControls In Me.Controls

If cControls.ControlType = acCheckBox Then
s = cControls.NAME
' now get the coresponding texzt box name
sName = "txt" & Right$(s, Len(s) - 3)

'If sName.Enabled Then
If IsNull(sName) Then
txtUserApproved = Null
Exit Sub
End If
'End If
End If

Next cControls

eg. if check box is called chlMech, then i can get the text box name (txtMech), but how can i refer to this as a control. Obviously sName.enabled won't work because sName is a string, not a control.

Hope this makes sense,

Cheers,

Nick
 
you have to DIM a variable as a control
this is a Help example which should get you started.

ActiveControl Property Example

The following example assigns the active control to the ctlCurrentControl variable and then takes different actions depending on the value of the control's Name property.

Dim ctlCurrentControl As Control

Set ctlCurrentControl = Screen.ActiveControl
If ctlCurrentControl.Name = "txtCustomerID" Then
. ' Do this.
.
.
ElseIf ctlCurrentControl.Name = "btnCustomerDetails" Then
. ' Do this.
.
.
End If

DougP, MCP

Visit my WEB site to see how Bar-codes can help you be more productive
 
I have got some variables declared, but i didn't paste them into the message:

Dim cControls As Control
Dim sName As String
Dim s As String

' if i have

s = "txtBox" and i then want to reference this string, but reference it as a text box how would i do it?

Cheers,

Nick
 
Hello,
Try this:

1. Create a new form.
2. Create 5 textboxes, and name them "Text1", "Text2", ... "Text5"
3. Create a button and put the following code in the OnClick Event of the button:

'**********************
Dim i As Integer
Dim strText As String
Dim cControl As Control

For i = 1 To 5
For Each cControl In Me.Controls
strText = "Text" & i
If cControl.ControlName = strText Then
cControl = "Test"
End If
Next
Next
'**********************

It works as a quasi control array, similar to Visual Basic. I have read others saying we can create an array of controls (like on Visual Basic), but I can never do it. If anyone knows how to do it, please let me know.
 
You can also use the Controls(controlName) syntax.

e.g. If some control object is declared (Dim MyCtrl as Control), you may reference a control via the controlname syntax:

Set MyCtrl = "Text1"

MyVal = CDbl(MyCtrl)

Should return a number if Text1 has a numeric string, or an error.

Perhaps the following will shead some light:


Please not that this is only a reasonable generic process. You can use it to see the named reference to the control. but it is NOT suitable for general use. It needs some error chacking and to be customized to reference controls types of YOUR project.


Code:
Public Function Chk2Txt()

    'Michael Red 2/6/2002 To "reference" related Controls

    Dim MyCtrl As Control

    For Each Ctrl In Me.Controls
    
        If Ctrl.ControlType = acOptionButton Then
            s = Ctrl.Name
            ' now get the coresponding texzt box name
            SName = "txt" & Right$(s, Len(s) - 3)
            Set MyCtrl = Me.Controls(SName)
           
            If MyCtrl.Enabled Then
                If (Ctrl <> 0) Then
                    If IsNull(MyCtrl) Then
                        Me.txtUserApproved = Null
                        Exit Function
                     Else
                        Me.txtUserApproved = MyCtrl
                    End If
                End If
            End If
        End If
    
    Next Ctrl

End Function
'____________________________________________________

The Following needs to be in each Option Button (or -in your case CheckBox).  It calls the above
Private Sub optPhone_Click()
    Chk2Txt
End Sub
[code] MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top