Home All Groups Group Topic Archive Search About

Changing a property on a control

Author
22 May 2009 8:32 PM
Jonathan
If I have a string variable p where p = "Text1.Height=1000" and Text1 is a
control on my form, is there a way to execute the contents of p as if it were
just a line
of code?

Thanks

Author
22 May 2009 10:04 PM
MikeD
"Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
news:15AA2FBF-9C36-4783-9504-2FEC41AEA4D7@microsoft.com...
> If I have a string variable p where p = "Text1.Height=1000" and Text1 is a
> control on my form, is there a way to execute the contents of p as if it
> were
> just a line
> of code?


With a little effort, yes. First thing you'll need to do is parse the text
assigned to p into 3 "parts"...the object name, the property name, and the
value. Using your example, Text1 would be the object name, Height would be
the property name, and 1000 would be the value.  Then, you can use the
CallByName function to assign the value to the property. Here's some sample
code for you.  Place a textbox named Text1 on the form.  Copy and paste this
code into General Declarations:

-----BEGIN CODE
Private Sub Form_Load()

    Dim p           As String
    Dim sObject     As String
    Dim sProperty   As String
    Dim sValue      As String
    Dim lPos1       As Long
    Dim lPos2       As Long

    Show

    p = "Text1.Height=1000"

    lPos1 = InStr(1, p, ".")
    lPos2 = InStr(1, p, "=")

    sObject = Left$(p, lPos1 - 1)
    sProperty = Mid$(p, lPos1 + 1, lPos2 - lPos1 - 1)
    sValue = Mid$(p, lPos2 + 1)
    CallByName Controls(sObject), sProperty, VbLet, CLng(sValue)

End Sub
-----END CODE

I think that's probably about as simple as it's going to get for you.  This
code is make the assumption that what's assigned to p is pretty much exactly
what you said. If there could be spaces involved (IOW, you have "Text1.Text
= 1000"), then you'll need to modify this code to account for that. Easiest
way would probably be to use the Replace function to remove all space
characters first. But on the other hand, if the property is a string (say
the Text property), then you might WANT spaces that are part of the string
value you're assigning to the Text property.

When you're doing something like this, there's a lot that could potentially
go wrong.  So be sure you've got an error handler enabled so that when
something does go wrong (and most likely eventually it will), it fails
gracefully. With some additional code, you could validate the object and the
property by "testing" for those before continuing on. Here's something a
little more robust:

-----BEGIN CODE
Private Sub Form_Load()

    Dim p           As String
    Dim sObject     As String
    Dim sProperty   As String
    Dim sValue      As String
    Dim lPos1       As Long
    Dim lPos2       As Long
    Dim oObject     As Object

    On Error GoTo EH

    Show

    p = "Text.Height=1000"

    lPos1 = InStr(1, p, ".")
    lPos2 = InStr(1, p, "=")

    sObject = Left$(p, lPos1 - 1)

    On Error Resume Next
    Set oObject = Controls(sObject)
    Do Until Not oObject Is Nothing
        sObject = InputBox$("Invalid object name.  Type in the correct
object name")
        If Len(sObject) = 0 Then
            Exit Sub
        End If
        Set oObject = Controls(sObject)
    Loop

    sProperty = Mid$(p, lPos1 + 1, lPos2 - lPos1 - 1)
    sValue = Mid$(p, lPos2 + 1)
    CallByName oObject, sProperty, VbLet, CLng(sValue)

    Exit Sub

EH:

     MsgBox Err.Description

End Sub
-----END CODE

I used InputBox$ for example purposes only. I'd recommend something MUCH
better than that for your actual program, such as your own dialog box form
that you create. Note that I also changed CallByName to use oObject. Might
as well since it's already set.  But it'd have still worked fine if you
continued to use Controls(sObject) in CallByName.

That should give you enough to get started at least.

--
Mike
Author
22 May 2009 10:14 PM
MikeD
"MikeD" <nob***@nowhere.edu> wrote in message
news:uCsXNly2JHA.5816@TK2MSFTNGP02.phx.gbl...
>
> Then, you can use the CallByName function to assign the value to the
> property.

I should also mention this will only work in VB6 because VB5 and under do
not have the CallByName function.

--
Mike
Author
26 May 2009 1:50 PM
Jonathan
Just what I'm looking for. Thanks
--
Jonathan


Show quoteHide quote
"MikeD" wrote:

>
> "Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
> news:15AA2FBF-9C36-4783-9504-2FEC41AEA4D7@microsoft.com...
> > If I have a string variable p where p = "Text1.Height=1000" and Text1 is a
> > control on my form, is there a way to execute the contents of p as if it
> > were
> > just a line
> > of code?
>
>
> With a little effort, yes. First thing you'll need to do is parse the text
> assigned to p into 3 "parts"...the object name, the property name, and the
> value. Using your example, Text1 would be the object name, Height would be
> the property name, and 1000 would be the value.  Then, you can use the
> CallByName function to assign the value to the property. Here's some sample
> code for you.  Place a textbox named Text1 on the form.  Copy and paste this
> code into General Declarations:
>
> -----BEGIN CODE
> Private Sub Form_Load()
>
>     Dim p           As String
>     Dim sObject     As String
>     Dim sProperty   As String
>     Dim sValue      As String
>     Dim lPos1       As Long
>     Dim lPos2       As Long
>
>     Show
>
>     p = "Text1.Height=1000"
>
>     lPos1 = InStr(1, p, ".")
>     lPos2 = InStr(1, p, "=")
>
>     sObject = Left$(p, lPos1 - 1)
>     sProperty = Mid$(p, lPos1 + 1, lPos2 - lPos1 - 1)
>     sValue = Mid$(p, lPos2 + 1)
>     CallByName Controls(sObject), sProperty, VbLet, CLng(sValue)
>
> End Sub
> -----END CODE
>
> I think that's probably about as simple as it's going to get for you.  This
> code is make the assumption that what's assigned to p is pretty much exactly
> what you said. If there could be spaces involved (IOW, you have "Text1.Text
> = 1000"), then you'll need to modify this code to account for that. Easiest
> way would probably be to use the Replace function to remove all space
> characters first. But on the other hand, if the property is a string (say
> the Text property), then you might WANT spaces that are part of the string
> value you're assigning to the Text property.
>
> When you're doing something like this, there's a lot that could potentially
> go wrong.  So be sure you've got an error handler enabled so that when
> something does go wrong (and most likely eventually it will), it fails
> gracefully. With some additional code, you could validate the object and the
> property by "testing" for those before continuing on. Here's something a
> little more robust:
>
> -----BEGIN CODE
> Private Sub Form_Load()
>
>     Dim p           As String
>     Dim sObject     As String
>     Dim sProperty   As String
>     Dim sValue      As String
>     Dim lPos1       As Long
>     Dim lPos2       As Long
>     Dim oObject     As Object
>
>     On Error GoTo EH
>
>     Show
>
>     p = "Text.Height=1000"
>
>     lPos1 = InStr(1, p, ".")
>     lPos2 = InStr(1, p, "=")
>
>     sObject = Left$(p, lPos1 - 1)
>
>     On Error Resume Next
>     Set oObject = Controls(sObject)
>     Do Until Not oObject Is Nothing
>         sObject = InputBox$("Invalid object name.  Type in the correct
> object name")
>         If Len(sObject) = 0 Then
>             Exit Sub
>         End If
>         Set oObject = Controls(sObject)
>     Loop
>
>     sProperty = Mid$(p, lPos1 + 1, lPos2 - lPos1 - 1)
>     sValue = Mid$(p, lPos2 + 1)
>     CallByName oObject, sProperty, VbLet, CLng(sValue)
>
>     Exit Sub
>
> EH:
>
>      MsgBox Err.Description
>
> End Sub
> -----END CODE
>
> I used InputBox$ for example purposes only. I'd recommend something MUCH
> better than that for your actual program, such as your own dialog box form
> that you create. Note that I also changed CallByName to use oObject. Might
> as well since it's already set.  But it'd have still worked fine if you
> continued to use Controls(sObject) in CallByName.
>
> That should give you enough to get started at least.
>
> --
> Mike
>
>
>
Author
22 May 2009 10:36 PM
Nobody
"Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
news:15AA2FBF-9C36-4783-9504-2FEC41AEA4D7@microsoft.com...
> If I have a string variable p where p = "Text1.Height=1000" and Text1 is a
> control on my form, is there a way to execute the contents of p as if it
> were
> just a line
> of code?

Besides what MikeD suggested, one method to add scripting support to your
application, if that's what you were looking for, is to use VBScript. You
can's use something like the line that you provided exactly, but VBScript
supports CreateObject() to run your ActiveX project and use the properties
and methods it offers. For something like your statement, you need to offer
a method, or property, such as "someclass.Height = 1000". Here are some
examples:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=55570&lngWId=1
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=37693&lngWId=1
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2938&lngWId=1
Author
22 May 2009 11:43 PM
MikeD
Show quote Hide quote
"Nobody" <nob***@nobody.com> wrote in message
news:etbGU3y2JHA.1416@TK2MSFTNGP04.phx.gbl...
> "Jonathan" <Jonat***@discussions.microsoft.com> wrote in message
> news:15AA2FBF-9C36-4783-9504-2FEC41AEA4D7@microsoft.com...
>> If I have a string variable p where p = "Text1.Height=1000" and Text1 is
>> a
>> control on my form, is there a way to execute the contents of p as if it
>> were
>> just a line
>> of code?
>
> Besides what MikeD suggested, one method to add scripting support to your
> application, if that's what you were looking for, is to use VBScript. You
> can's use something like the line that you provided exactly, but VBScript
> supports CreateObject() to run your ActiveX project and use the properties
> and methods it offers. For something like your statement, you need to
> offer a method, or property, such as "someclass.Height = 1000". Here are
> some examples:


On locked down systems, scripting may not be available.

--
Mike
Author
25 May 2009 6:20 PM
mark.tunnard.jackson
An alternative way to use VBScript without making your program an
ActiveX EXE. You can use the Script control. You can load and run
VBScript from external text files, passing in your own objects so that
the script can manipulate them. I think you'd still need to wrap the
text box in an object, I don't think passing in a textbox control
would work.

My tests indicate it still works if scripting is disabled

Here is an article
http://articles.techrepublic.com.com/5100-10878_11-1045808.html

And the script control free download (Microsoft)
http://www.microsoft.com/downloads/details.aspx?FamilyId=D7E31492-2595-49E6-8C02-1426FEC693AC

Mark
Author
28 May 2009 2:34 AM
MikeD
<mark.tunnard.jack***@googlemail.com> wrote in message
news:311514bb-5f03-411e-96a8-6d3e014205e2@j18g2000yql.googlegroups.com...
> An alternative way to use VBScript without making your program an
> ActiveX EXE. You can use the Script control. You can load and run
> VBScript from external text files, passing in your own objects so that
> the script can manipulate them. I think you'd still need to wrap the
> text box in an object, I don't think passing in a textbox control
> would work.
>
> My tests indicate it still works if scripting is disabled


I didn't say disabled (and who said anything about an ActiveX EXE? I've no
idea where you got that from nor what it has to do with scripting).  I said
scripting may not be available. There's a difference between " not
available" and "disabled". I know many sysadmins who one way or another
remove the scripting library, which means ANYTHING that wants to use it
won't work.

The point is why use the scripting library for something that can be
accomplished without it? Now, if Jonathan had said he was using VB5 (well, I
guess he didn't, but he did reply that the suggestion I provided was "just
what he was looking for", so presumably he is using VB6), I might have
conceded that he had to use the scripting library and concessions by a
sysadmin would just have to be made (or not, sysadmins can be pretty
stubborn).

--
Mike