|
-
Sep 1st, 2011, 11:22 PM
#1
Thread Starter
Member
[RESOLVED] Programmatically countering
Good day again everyone,
i am using vb6, of which i am now learning to do scripting using scriptcontrol.
i can now write numeric calculations on text box and make an output from it.
like this
HTML Code:
Dim script1 As ScriptControl
Private Sub Command1_Click()
amf = Text1.Text
EXCECUTE = "NDP" & " = " & "Round(" & amf & ",2)"
formula = EXCECUTE
MsgBox (formula)
script1.ExecuteStatement formula
End Sub
Private Sub Form_Load()
Set script1 = ScriptControl1
script1.Language = "VBScript"
script1.AddObject "NDP", Text2
End Sub
so if i type 5+1 on text1 it will outpult 6 on text2
problem now is...
i got 3 textboxes A,B, and C each holding a number input.
i got another textbox which is COMP holding the "computation"
and last textbox for the output which is RSLT
now to be complex,
i am tring to write A+B+C on COMP
(e.g
A = 1
B = 2
C = 3
>>> on textbox COMP i write A+B+C
)
on command click i would like have the result on RSLT. which is like the above working program.
>the error i am getting with this is type mismatch and some ")" missing "string value:[]"
what i have done now is passing A,B,C to an integer variable, to string but with no luck still not working.
Code:
Dim script1 As ScriptControl
Private Sub Command1_Click()
Dim psrp, gdp, cd As Integer
psrp = A
gdp = B
cd = C
amf = Text4.Text
EXCECUTE = "NDP" & " = " & "Round(" & amf & ",4)"
formula = EXCECUTE
MsgBox (formula)
script1.ExecuteStatement formula
End Sub
Private Sub Form_Load()
Set script1 = ScriptControl1
script1.Language = "VBScript"
script1.AddObject "NDP", Text5
End Sub
help please.
~liz
-
Sep 2nd, 2011, 02:00 AM
#2
Thread Starter
Member
Re: Programmatically countering
Code:
Option Explicit
Dim script As ScriptControl
Private Sub Command1_Click()
Dim strFormula As String
strFormula = Text4.Text
strFormula = Replace(strFormula, "A", Text1.Text)
strFormula = Replace(strFormula, "B", Text2.Text)
strFormula = Replace(strFormula, "C", Text3.Text)
Text5.Text = script.Eval(strFormula)
End Sub
Private Sub Form_Load()
Set script = ScriptControl1
script.Language = "VBScript"
End Sub
ok with the code above i seemingly got what i want heres the reference link http://www.vbforums.com/showthread.p...=scriptcontrol
thanks for the people who contributed here on vbforums.
ill mark resolve this thread after i get a second opinion from anyone who knows a better way to do this.
thanks again
~liz
-
Sep 2nd, 2011, 02:02 AM
#3
Re: Programmatically countering
I would have done it by adding a Module and code within it to perform the calculation. Something like this
Code:
Option Explicit
Private script1 As ScriptControl
Private Sub Command1_Click()
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim strCode As String
Dim mMod As Module
A = CInt(Text1.Text)
B = CInt(Text2.Text)
C = CInt(Text3.Text)
strCode = "Function Calc(A, B, C)" & vbCrLf
strCode = strCode & "Calc = " & Text4.Text & vbCrLf
strCode = strCode & "End Function" & vbCrLf
script1.Modules.Add ("MyMod")
script1.Modules("MyMod").AddCode strCode
Set mMod = script1.Modules("MyMod")
text5.Text = mMod.Run("Calc", A, B, C)
End Sub
Private Sub Form_Load()
Set script1 = New ScriptControl
script1.Language = "VBScript"
End Sub
Text4.text = "a+b+c"
-
Sep 2nd, 2011, 02:03 AM
#4
Re: Programmatically countering
what abt this
Code:
Private Sub Command1_Click()
Text4 = ScriptControl1.Eval(Text1 & "+" & Text2 & "+" & Text3)
End Sub
Private Sub Form_Load()
Text1 = "(12+3)*(13+2)"
Text2 = "1+6"
Text3 = "2+3"
End Sub
-
Sep 2nd, 2011, 03:22 AM
#5
Thread Starter
Member
Re: Programmatically countering
@Doogle
Sir your code is better than mine and its working well, i can customize the text4 for any formula/calculations that regards a+b+c however i have added
script1.reset after the declarations so you/i can continuously reprogram textbox4 and rerun the button thanks man...
but i really dont get .module function on scriptcontrol and is it usefull on scripting like this. can you enlighten me sir?
@seenu
Sir your code is helpful too for computation done exclusively from the textboxes also. Still the point of this thread was putting up computations relying on the inputs of text1-text3 which are assigned to a value to which that value will be the one written on text4 for the creation of the formula/calculations.
still thats a great code sir thanks also.
@all
still wanting more explanation on this so if anyone still got ideas please post.
thanks
~liz
-
Sep 2nd, 2011, 04:01 AM
#6
Re: [RESOLVED] Programmatically countering
Liz:
You can think of a Module within the Script Object just the same as as Module in your Code. So I created a Module named mMod and within that module wrote a Function named Calc to performthe calculation defined by the contents of Text4. This is one way of passing variabloe values from your code to the Scripting object. The code executed by the scripting control is not in the same scope as the variables in your application - ie the Variable A in your code is not the same as Variable A in the Function, so in order to use the value in the Function it is passed as a parameter. Likewise the result from the Function is passed back to a variable in your code. The use of a Module for what you are doing may be considered overkill, but it does allow for future expansion especially if you may need to perform more complex calculations.
Hope that helps.
Good luck with the rest of the Project.
-
Sep 2nd, 2011, 07:43 PM
#7
Thread Starter
Member
Re: [RESOLVED] Programmatically countering
sir Doogle,
yeah its somehow overkill haha ~_~... more complex but yeah it should hold future modification of this project, and i see it, been trying your code for complex calculation formula and still works fine.
i understand it now sir. so i guess modulating some code within a code is much better way to pass/counter variables.
thanks again sir.
~liz
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|