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

Run VBScript or JavaScript in VFP

Activex

Run VBScript or JavaScript in VFP

by  craigsboyd  Posted    (Edited  )
Slighthaze = [color blue]NULL[/color]

[img http://www.sweetpotatosoftware.com/ttimages/vfpscript.gif]

Using the Microsoft Script Control we can facilitate running VBScript or JavaScript from VFP. This allow us to extend VFP or even provide a script language to our applications' users. Cut-n-paste the code below into a prg file and run it from within VFP.

Code:
PUBLIC oForm
oForm = CREATEOBJECT("clsscriptform")
oForm.show()

DEFINE CLASS clsscriptform AS form
    
    Autocenter = .T.
    Top = 0
    Left = 0
    Height = 512
    Width = 443
    DoCreate = .T.
    Caption = "RUNNING VBSCRIPT OR JAVASCRIPT FROM VFP"
    Name = "Form1"

    ADD OBJECT shape1 AS shape WITH ;
        Top = 376, ;
        Left = 65, ;
        Height = 113, ;
        Width = 313, ;
        BackStyle = 0, ;
        SpecialEffect = 0, ;
        Name = "Shape1"

    ADD OBJECT text1 AS textbox WITH ;
        Height = 23, ;
        Left = 76, ;
        Top = 393, ;
        Width = 288, ;
        Name = "Text1"

    ADD OBJECT command1 AS commandbutton WITH ;
        Top = 292, ;
        Left = 30, ;
        Height = 27, ;
        Width = 84, ;
        Caption = "Execute", ;
        Name = "Command1"

    ADD OBJECT edit1 AS editbox WITH ;
        Height = 199, ;
        Left = 29, ;
        Top = 81, ;
        Width = 385, ;
        Name = "Edit1"

    ADD OBJECT optiongroup1 AS optiongroup WITH ;
        ButtonCount = 2, ;
        Value = 1, ;
        Height = 26, ;
        Left = 29, ;
        Top = 25, ;
        Width = 200, ;
        Name = "Optiongroup1", ;
        Option1.Caption = "VBScript", ;
        Option1.Value = 1, ;
        Option1.Height = 17, ;
        Option1.Left = 5, ;
        Option1.Top = 5, ;
        Option1.Width = 100, ;
        Option1.Name = "Option1", ;
        Option2.Caption = "JavaScript", ;
        Option2.Height = 17, ;
        Option2.Left = 105, ;
        Option2.Top = 5, ;
        Option2.Width = 100, ;
        Option2.Name = "Option2"

    ADD OBJECT optiongroup2 AS optiongroup WITH ;
        ButtonCount = 2, ;
        Value = 1, ;
        Height = 46, ;
        Left = 76, ;
        Top = 429, ;
        Width = 144, ;
        Name = "Optiongroup2", ;
        Option1.Caption = "Option1", ;
        Option1.Value = 1, ;
        Option1.Height = 17, ;
        Option1.Left = 5, ;
        Option1.Top = 5, ;
        Option1.Width = 61, ;
        Option1.Name = "Option1", ;
        Option2.Caption = "Option2", ;
        Option2.Height = 17, ;
        Option2.Left = 5, ;
        Option2.Top = 24, ;
        Option2.Width = 61, ;
        Option2.Name = "Option2"

    ADD OBJECT command2 AS commandbutton WITH ;
        Top = 429, ;
        Left = 256, ;
        Height = 27, ;
        Width = 84, ;
        Caption = "Command2", ;
        Name = "Command2"

    ADD OBJECT label1 AS label WITH ;
        AutoSize = .T., ;
        BackStyle = 0, ;
        Caption = ['"MyForm" references Thisform'], ;
        Height = 17, ;
        Left = 255, ;
        Top = 297, ;
        Width = 167, ;
        Name = "Label1"

    ADD OBJECT label3 AS label WITH ;
        AutoSize = .T., ;
        BackStyle = 0, ;
        Caption = "Some controls to mess with through VBScript", ;
        Height = 17, ;
        Left = 64, ;
        Top = 357, ;
        Width = 249, ;
        Name = "Label3"

    ADD OBJECT label2 AS label WITH ;
        AutoSize = .T., ;
        BackStyle = 0, ;
        Caption = "Write your VBScript below and click Execute", ;
        Height = 17, ;
        Left = 29, ;
        Top = 57, ;
        Width = 238, ;
        Name = "Label2"

    PROCEDURE Init
		this.optiongroup1.Interactivechange()
		MESSAGEBOX("Some sample script is provided for you.  Just click execute to see how it works." + CHR(13) +;
				"Then write your own script.  You can change to JavaScript as well.",64,"HOW TO GET STARTED")      
    ENDPROC

    PROCEDURE command1.Click
        Local loScript
        loScript = Createobject([MSScriptcontrol.scriptcontrol.1])
        IF thisform.optiongroup1.value = 1
        	loScript.Language = [VBScript]
        ELSE
        	loScript.Language = [JavaScript]
        ENDIF
        loscript.addobject("MyForm", thisform)
        loScript.executestatement(thisform.edit1.value)
    ENDPROC
    
    PROCEDURE optiongroup1.Interactivechange
    	IF this.value = 1
    		thisform.label2.caption = "Write your VBScript below and click Execute"
    		thisform.label3.caption = "Some controls to mess with through VBScript"
	        thisform.edit1.Value = [MsgBox ("I am about to change the controls below through VBScript")] + CHR(13) + CHR(10) + ;
                                [MyForm.Text1.value = "Hello VBScript World!"]+ CHR(13) + CHR(10) + ;
                                [MyForm.Optiongroup2.value = 2]+ CHR(13) + CHR(10) + ;
                                [MyForm.Optiongroup2.option1.Caption = "Male"]+ CHR(13) + CHR(10) + ;
                                [MyForm.Optiongroup2.option2.Caption = "Female"]+ CHR(13) + CHR(10) + ;
                                [MyForm.Command2.caption = "Testing"]+ CHR(13) + CHR(10) + ;
                                [MsgBox ("Now write your own VBScript in the Editbox provided")]
    	ELSE
    		thisform.label2.caption = "Write your JavaScript below and click Execute"
    		thisform.label3.caption = "Some controls to mess with through JavaScript"
	        thisform.edit1.Value = [var name = "Hello JavaScript World!";] + CHR(13) + CHR(10) + ;
                                [var x = 6;] + CHR(13) + CHR(10) + ;
                                [var y = 12;] + CHR(13) + CHR(10) + ;
                                [var z = y / x;] + CHR(13) + CHR(10) + ;
                                [MyForm.text1.value = name;] + CHR(13) + CHR(10) + ;
                                [MyForm.optiongroup2.value = z;]
    	ENDIF
    ENDPROC

ENDDEFINE
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top