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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Property Value of a Control from String 1

Status
Not open for further replies.

swreng

MIS
Jun 22, 2006
90
GR
Dear all,

if i scan all form's controls with for next
is it possible to get a specific property value of the control by passing a string with property's name.

For example:

Dim ctl as Control

for each ctl in me
' Call a function which i will supply it
' like this:
' ReturnProperty("Width") and return
' then Width value of the current control in for

next

Many thanks
 
Sure. Here's an example with two possible solutions:
Code:
[blue]Option Explicit

Private Sub Command1_Click()
    Dim ctl As Control
    For Each ctl In Me.Controls
        Debug.Print ReturnProperty1(ctl, "name")
        Debug.Print ReturnProperty2(ctl, "width")
    Next
End Sub

Private Function ReturnProperty1(ctlSource As Object, strProperty As String) As Variant
    ReturnProperty1 = CallByName(ctlSource, strProperty, VbGet)
End Function

Private Function ReturnProperty2(ctlSource As Object, strProperty As String) As Variant
    With CreateObject("MSSCriptControl.ScriptControl")
        .Language = "vbscript"
        .AddObject "AskMe", ctlSource
        ReturnProperty2 = .Eval("AskMe." & strProperty)
    End With
End Function[/blue]
 
Hi strongm

Thank you for your post is very useful for me.
I would like to ask and something else

is it possible with a similar way to set the property by passing the string of the property and the value which i want.
I try this:

Private Sub WriteProperty(ctlSource As Object, strProperty As String, val As Variant)
Call CallByName(ctlSource, strProperty, VbSet, val)
End Sub

and i call it for a CommandButton like this:

Call WriteProperty(commandbutton1,"Caption","Changed Caption")

but doesn't work during the execution of CallByname

Many Many thanks
 
Solved
I understand my mistake the procedure callbyname it works in this case with VbLet



Many Many thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top