Results 1 to 16 of 16

Thread: [RESOLVED] Execute command from textbox

  1. #1

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    258

    Resolved [RESOLVED] Execute command from textbox

    Hi,

    Related with this message, I trying to built a small console so app user could help me to debug.

    Code:
    Option Explicit
    Dim script As ScriptControl
    
    Private Sub Form_Load()
        Set script = New ScriptControl
        Text1.Text = "MsgBox ""Hello"""    'Load a default command into Text1.
        script.Language = "VBScript"
        script.ExecuteStatement Text1.Text
    End Sub
    On startup, it opens a message box saying Hello.

    But if I want to display a variable value :
    Code:
    Option Explicit
    Dim script As ScriptControl
    
    Dim pocpoc As String
    
    Private Sub Form_Load()
        Set script = New ScriptControl
        pocpoc = "plouf"
        Text1.Text = "MsgBox pocpoc"    'Load a default command into Text1.
        script.Language = "VBScript"
        script.ExecuteStatement Text1.Text
    End Sub
    It display a blanc message box

    Is there something I messed ?

    Thanks
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  2. #2
    Addicted Member
    Join Date
    Jul 2021
    Posts
    171

    Re: Execute command from textbox

    pocpoc is a variable in Form_Load, but not in the script scope.
    You can do something like this:
    Code:
    Text1.Text = "MsgBox """ & pocpoc & """

  3. #3

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    258

    Re: Execute command from textbox

    Hi,

    Thanks for answer. Now I see now my request is not very clear lol

    In fact, I would have a text box (it will be empty in the final project) and a button.

    The goal is for example, saying to the user (who encounters bug with the app): Write MsgBox pocpoc and click button.
    pocpoc is just an example, I can ask him to MsgBox another variable of the app.
    I could also ask him to change the variable value, like: pocpoc = "Scrounch" and click button.

    Code:
    Option Explicit
    
    Dim script As ScriptControl
    
    Public pocpoc As String
    
    Private Sub Command1_Click()
       script.ExecuteStatement Text1.Text
    End Sub
    
    Private Sub Form_Load()
        Set script = New ScriptControl
        pocpoc = "plouf"
        script.Language = "VBScript"
    End Sub
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  4. #4
    Addicted Member
    Join Date
    Jul 2021
    Posts
    171

    Re: Execute command from textbox

    So basically you want to allow the user to have access to your application?
    You need to add the objects to the script like this:
    Code:
    Private Sub Form_Load()
        Set script = New ScriptControl
        pocpoc = "plouf"
        script.Language = "VBScript"
        script.AddObject "Form1", Form1, False
        script.AddObject "Form2", Form2, False
        ' etc...
    End Sub
    Now the user can type: MsgBox Form1.pocpoc
    Or even: MsgBox Form1.Command1.Caption

  5. #5
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,029

    Re: Execute command from textbox

    And make sure to declare the form variables as public so the script control can access them with like

    Public poopoo as string

    Form1.poopoo
    Last edited by dz32; Nov 27th, 2023 at 11:52 AM.

  6. #6
    Addicted Member
    Join Date
    Jul 2021
    Posts
    171

    Re: Execute command from textbox

    Why do people abandon their own thread?
    So disrespectful!

  7. #7

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    258

    Re: Execute command from textbox

    Quote Originally Posted by Dry Bone View Post
    So basically you want to allow the user to have access to your application?
    [...]
    Now the user can type: MsgBox Form1.pocpoc
    Or even: MsgBox Form1.Command1.Caption
    I just want to make a console where the user can, on my demand, display or change the value of a public variable.
    So If I ask him to type MsgBox pocpoc and hit "Execute" (comamndbutton), it will display the pocpoc value. If I ask him to type pocpoc = 1 and hit execute, the pocpoc value changes to 1 (instead of 0, for example).

    Quote Originally Posted by dz32 View Post
    And make sure to declare the form variables as public so the script control can access them with like

    Public poopoo as string

    Form1.poopoo
    In the app, near from all variables I need to display or change, are already Public

    Quote Originally Posted by Dry Bone View Post
    Why do people abandon their own thread?
    So disrespectful!
    Sorry but sleep + work, I can read messages only now ...
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  8. #8
    Addicted Member
    Join Date
    Jul 2021
    Posts
    171

    Re: Execute command from textbox

    If you only need to change public variables of Form1, you can do:
    Code:
    script.AddObject "Form1", Form1, True
    The parameter "True" makes Form1 the default object, so you can access its properties directly, like you want:
    MsgBox pocpoc
    or
    pocpoc=somevalue
    should work.

  9. #9

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    258

    Re: Execute command from textbox

    Hi

    thanks I obtains some results like this.

    Also, I have some Public variables in a module, I tried to replace Form1 for Module1 (in the test example) but I get a mismatch error.

    Edit :
    Code:
    script.AddObject "Module1", Form1, True
    No longer get mismatch error, but if I type MsgBox pocpoc, I get a blank result.
    If I type pocpoc = 1 and after, MsgBox pocpoc again, the result says 1 .

    I don't exactly understant the role of script.AddObject, as well as if I put
    Code:
    script.AddObject "Module2", Form1, True
    I get the same results, even if Module2 does not exists.

    Im' about searching about script.AddObject
    Last edited by Couin; Nov 28th, 2023 at 05:44 PM.
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  10. #10
    Addicted Member
    Join Date
    Jul 2021
    Posts
    171

    Re: Execute command from textbox

    AFAIK, you can't add a module to the script. Just objects.
    You must move the variables from the module to Form1 or to a designated Class Module, and then create an object of that class module and add that object to the script.

  11. #11
    Addicted Member
    Join Date
    Jul 2021
    Posts
    171

    Re: Execute command from textbox

    You need to understand AddObject
    The first parameter (text) is the name you assign to the object. It can be any name, like "foo", so the user will address the object with foo.[somemethod/property].
    The second parameter is the object itself. It must be an object, not a module.
    So, in your example, script.AddObject "Module2", Form1, True you just add again the same object (Form1) with a different alias ("Module2").

  12. #12

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    258

    Re: Execute command from textbox

    Thanks for explanation
    Also, I think I can't move variables of modules to classes (and I tried on a test example , even with Public pocpoc, the variable is not defined when I start the app), there are a loooot a variables, and the app Startup object in the Project properties is the main module.

    Perhaps is there existing another method to make the "console", that handles public variables of modules ?
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  13. #13
    Addicted Member
    Join Date
    Jul 2021
    Posts
    171

    Re: Execute command from textbox

    You need to create the object of the class in the main module:
    Code:
    Public Vars As New Class1
    Then you need to access the variables in your app with Vars.[variable], which is somewhat inconvenient.
    But the user doesn't need to type Vars.[variable], just [variable], if you add Vars to the script as a default object (True).
    That's the only way I know.

  14. #14

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    258

    Re: Execute command from textbox

    Hmmm, even if it is totally unimagineable to modify thousands lines of code to add Vars. before each variables, I tested with the test example, but I get not more result than before...
    It's very too much asbtract for me

    Form1
    Code:
    Option Explicit
    
    Dim script As ScriptControl
    
    Private Sub Command1_Click()
       script.ExecuteStatement Text1.Text
    End Sub
    
    Private Sub Form_Load()
        Set script = New ScriptControl
        script.Language = "VBScript"
        script.AddObject "Form1", Form1, True
    End Sub
    Module1
    Code:
    Option Explicit
    
    Public Vars As New Class1
    
    Public pocpoc As String
    And must add a Class1 class module to ride off User-defined type not defined error.

    When I start the example and type MsgBox pocpoc, the result still empty.

    It looks very complicated for a thing that should be simple
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  15. #15
    Addicted Member
    Join Date
    Jul 2021
    Posts
    171

    Re: Execute command from textbox

    You are doing it all wrong!
    But I have cooked another way for you.

    Create a new class, say, Class1
    In Class1:
    Code:
    Option Explicit
    
    Public pocpoc As String
    Public foo As Long
    'etc
    In Module1:
    Code:
    Option Explicit
    
    Public Vars As New Class1
    Public script As ScriptControl
    
    Sub Main
        Set script = New ScriptControl
        script.Language = "VBScript"
        script.AddObject "Form1", Form1, True
        script.AddObject "Vars", Vars, True
    End Sub
    
    Property Let pocpoc(v As String)
        Vars.pocpoc = v
    End Property
    
    Property Get pocpoc() As String
        pocpoc = Vars.pocpoc
    End Property
    
    Property Let foo(v As Long)
        Vars.foo = v
    End Property
    
    Property Get foo() As Long
        foo = Vars.foo
    End Property
    Form1:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
       script.ExecuteStatement Text1.Text
    End Sub
    You need to do the same for EVERY variable you want the user get access.
    That includes: Put the variable in Class1 as public, and add propery Get and property Let for that variable.

    Now you don't have to change ANYTHING in the rest of the code.
    You can do pocpoc="Pouf": Debug.Print foo (and so on).

  16. #16

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    258

    Re: Execute command from textbox

    Hi

    Thanks a lot, it looks running as attempted

    Rest to adapt to the project

    See ya
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width