|
-
May 31st, 2000, 05:40 AM
#1
Thread Starter
New Member
I'm having quite an issue with Visual Basic.
I have a custom library which has a class in it. This class has several properties which I am interested in looking at. There is no problem retrieving the values as follows:
....vbcode....
x = className.propertyName
..../vbcode...
but this forces me to call each property seperately. What I would rather do is have the user type in a text box the property name he chooses and have x loaded with that property value.
....vbcode...
txt1.text = "propertyName"
x= className. & txt1.text
..../vbcode...
This of course does not work. What I am wondering is if there is any way to make this work. To execute a string as a piece of vb code?
Any suggestions or comments ? I'm really up the wall on this one.
-
May 31st, 2000, 05:48 AM
#2
Member
AH HA!
This one goes back to when I learnt BASIC on an Amstrad CPC 6128 at the age of 12.
I wanted to write a program that did somthing like what you wanted to do. I wanted the user to input a string and the the program to be able to show what was in the variable with the same name as that string.
i.e
let a=12
<<USER INPUTS "A">
Program send back 12.
I just guess basic was never ment to analyse it's self, it wouldn't be basic.
-
May 31st, 2000, 06:02 AM
#3
Thread Starter
New Member
so does this mean that I'm stuck with getting the user's input and using a silly case statement to execute a "code" version of that string. It sort of makes me wonder how a language could be so versitile without being able to perform such a simple task....(I guess it's also sort of funny that I have just now learned this now having been programming in VB ever since V2.......)
-
May 31st, 2000, 06:05 AM
#4
Member
I'm not sure you can even do that - I never worked it out. Basic really can suck
-
May 31st, 2000, 07:37 AM
#5
Hyperactive Member
No can do in VB... you can in FoxPro though.
-
Aug 12th, 2001, 03:02 PM
#6
Member
Dynamic VB Code
I have tried to do the same thing: reading text from a TextBox and then entering it into a file and reading the file again. All using the Ms Script Control and help from this scriptlet.
But unfortunately I got an error telling me an object is required: it seems I have no use from the controls in my form using the script engine. Can anyone help me here? E.g. telling me how to call a certain object or ActiveX control (.OCX) from the script file.
Thanks!
-
Aug 12th, 2001, 03:06 PM
#7
Member
P.S. here's the link to the scriptlet:
http://members.aol.com/vbchenko/code...rce/script.txt
and here's the code I used:
Dim strcode As String
Open Application.Path & "\test.txt" For Input As #1
Do Until EOF(1)
Line Input #1, Data
strcode = strcode & Data & vbCrLf
Loop
Close #1
ScriptControl1.Language = "VBScript"
ScriptControl1.AddCode (strcode)
'Hello is the sub routine or function
ScriptControl1.Run ("hello") ' With "hello" being the script function's name
-
Aug 12th, 2001, 03:28 PM
#8
Actually, it can be done! It's a new feature in VB6.
Look into CallByName.
Here's a sample usage:
MsgBox CallByName(Form1, "Caption", VbGet)
-
Aug 12th, 2001, 04:02 PM
#9
Member
Thanks a lot!
I am actually running this code from the VBA editor in Word 2000. But I've found the CallByName function, and used it as described:
CallByName(frmUpload, txtCode.Value, VbGet)
With frmUpload being my form and txtCode.Value being the code from the textbox.
Unfortunately it returns an error informing me that the method is not supported by the object.
Any ideas?
Thanks!!
-
Aug 12th, 2001, 04:19 PM
#10
Member
Hold on! I have it:
CallByName(txtCode, "Value", VbGet)
But VB won't just execute this code: it expects an equal sign (=).
If I however try this:
a = CallByName(txtCode, "Value", VbGet) a
It expects a Sub or Function or something. What is the prob now? 
Bye!
-
Aug 12th, 2001, 04:31 PM
#11
Member
Of course, where's my brain? 
To use it stand-alone, you have to put it like this:
CallByName txtCode, "Value", VbGet
Although the code doesn't execute yet (like this sitting in a Sub by itself). What's wrong?
Sincerely,
-
Aug 12th, 2001, 04:37 PM
#12
Frenzied Member
VB Code:
a = CallByName(txtCode, "Value", VbGet) 'NO TRAILING A!!!
MicroBasic
Dragon Shadow Trainer
There is no good or evil in the world...only programmers and fools .
-
Aug 12th, 2001, 04:43 PM
#13
Member
Thanks a mil. but still it doesn't appear to execute the (VB)code from within the textbox
Any ideas?
-
Aug 12th, 2001, 04:50 PM
#14
Frenzied Member
MsgBox CallByName(classname, txtCode, VbGet)
Do you want to EXECUTE VB CODE, LIKE:
VB Code:
MsgBox "...GGGHHHJJJKKK"
Di m i As Long
i = 100
MicroBasic
Dragon Shadow Trainer
There is no good or evil in the world...only programmers and fools .
-
Aug 12th, 2001, 05:01 PM
#15
Member
I want to execute it like:
Sub hello()
CallByName txtCode, "Text", vbGet
End Sub
That OK? Because a messagebox displayed the contents correctly and txtCode is the name of my textbox control.
TNX!!
-
Aug 12th, 2001, 05:09 PM
#16
Frenzied Member
VB Code:
Sub hello()
CallByName txtCode, "Text", vbGet
End Sub
Yes, it's OK.
MicroBasic
Dragon Shadow Trainer
There is no good or evil in the world...only programmers and fools .
-
Aug 12th, 2001, 05:18 PM
#17
Member
-
Aug 12th, 2001, 05:41 PM
#18
-
Aug 12th, 2001, 05:42 PM
#19
Fanatic Member
there is a rotine in the vb dll that does this, it's like an api, but i don't remeber the name, it's what the immediate code window uses
look it up somewhere like on yahoo, or search here, i've posted it before
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Aug 13th, 2001, 03:49 AM
#20
Member
-
Aug 13th, 2001, 04:46 AM
#21
PowerPoster
Originally posted by AndrzejAntoszkiewicz
What I am wondering is if there is any way to make this work. To execute a string as a piece of vb code?
Any suggestions or comments ? I'm really up the wall on this one.
I wanted the same capability and after a lot of checking around, I found two options that are close:
1) Script using the script control
2) VBA , which requires that you license VBA
As far as I know, you cannot execute user input VB code from your application (it has to be VBA or Script). If anyone finds otherwise, please let me know.
Attached is a small example that executes user input code. Not sure if this will help you or not. Good Luck!
-
Aug 13th, 2001, 07:15 AM
#22
Fanatic Member
here it is:
VB Code:
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
' = = = = For VB5
'Declare Function EbExecuteLine Lib "vba5.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
' = = = = For Access 97/VBE.dll clients as Word 97 and Excel 97
'Declare Function EbExecuteLine Lib "vba332.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Form_Load()
Dim TheString As String
TheString = "MsgBox ""This is a user generated dialog"""
Debug.Print FExecuteCode(TheString, False)
End Sub
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Aug 13th, 2001, 07:31 AM
#23
-
Aug 13th, 2001, 08:26 AM
#24
PowerPoster
Originally posted by gwdash
here it is: ...
Thats awesome! I noticed that it doesnt recogize variables from the calling program, though.
Two questions reagrding this API:
1. Is there any way to execute a block of code?
2. Is there anyway to pass variables to/from the executed code?
Thanks for the info!
-
Aug 13th, 2001, 08:33 AM
#25
Fanatic Member
1. try multiple calls to the function, one for each line
2. yes:
VB Code:
TheString = "msgbox ""This is a " & MyVariable & " message"""
Debug.Print FExecuteCode(TheString, False)
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Aug 13th, 2001, 08:49 AM
#26
PowerPoster
Originally posted by gwdash
1. try multiple calls to the function, one for each line
2. yes:
VB Code:
TheString = "msgbox ""This is a " & MyVariable & " message"""
Debug.Print FExecuteCode(TheString, False)
Thanks gwdash, but thats not quite what I meant. In your example the calling prog is converting the the variable to its contents before sending it to the API call.
suppose I wanted to execute this code
x=y+z
and I wanted to supply y and z and return the x for use by the calling prog. Also Id like the code string to have access to all prog properties (text1.text, etc) . Can this be done?
Thanks again for the information. This will help me out a lot if I can figure out how to make it work.
-
Aug 13th, 2001, 10:19 AM
#27
Fanatic Member
think of it as typing into the immediate window, anything you can do there, you can do with this function, it's the function used by the immediate window
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Aug 13th, 2001, 10:43 AM
#28
Hyperactive Member
I think that is a very useful function.
Thank you for the information.
I am so skeptical, I can hardly believe it!
PS I am not a 'hyperactive member' I am a cool, calm, and collected member 
-
Aug 13th, 2001, 11:00 AM
#29
PowerPoster
I hate to be such a bother about this, but I can really use this if I can figure it out. In the following code, why does debug.print "hello" instead of the contents of text1.text?
Thanks for any help!
VB Code:
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Command1_Click()
Dim a As String
a = "hello"
Dim TheString As String
TheString = "a=text1.text"
Debug.Print a
End Sub
-
Aug 13th, 2001, 11:03 AM
#30
Fanatic Member
make a a public varaible, and put in the FExecuteCode line, and try again
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Aug 13th, 2001, 11:05 AM
#31
a = "hello"
Dim TheString As String
TheString = "a=text1.text"
Debug.Print a
Because you are Debug.Print ing a, which is = "hello"
If you debug print TheString, you will get "a=text1.text"
-Lou
-
Aug 13th, 2001, 11:28 AM
#32
PowerPoster
that was embarrassing!
Now why doesnt this work?
VB Code:
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Command1_Click()
Dim a As String
a = "hello"
Dim TheString As String
TheString = "a=text1.text"
Debug.Print FExecuteCode(TheString, False)
Debug.Print a
End Sub
-
Aug 13th, 2001, 11:37 AM
#33
Fanatic Member
make "a" a public variable
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Aug 13th, 2001, 11:39 AM
#34
Hyperactive Member
I am so skeptical, I can hardly believe it!
PS I am not a 'hyperactive member' I am a cool, calm, and collected member 
-
Aug 13th, 2001, 12:01 PM
#35
PowerPoster
ok, tu=ried this and i get an "object required" error. any ideas?
VB Code:
Public a As String
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Command1_Click()
Dim TheString As String
a = "hello"
TheString = "a = text1.text"
Debug.Print FExecuteCode(TheString, False)
Debug.Print a
End Sub
-
Aug 13th, 2001, 12:04 PM
#36
Fanatic Member
is this is a form? make it Private, not Public, i should have said object scope. also try making it like
form1.text1.text
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Aug 13th, 2001, 12:17 PM
#37
PowerPoster
Originally posted by gwdash
is this is a form? make it Private, not Public, i should have said object scope. also try making it like
form1.text1.text
OK the following gets rid of the error, but a debug.prints as "hello" . I've tried a both as a Public and Private string.
This is a form with 1 textbox and 1 command button.
Thanks for hanging in there with me so long.
VB Code:
'Public a As String
Private a As String
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Foo1 As Long, ByVal Foo2 As Long, ByVal fCheckOnly As Long) As Long
Function FExecuteCode(stCode As String, Optional fCheckOnly As Boolean) As Boolean
FExecuteCode = EbExecuteLine(StrPtr(stCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Command1_Click()
Dim TheString As String
a = "hello"
TheString = "a = form1.text1.text"
Debug.Print FExecuteCode(TheString, False)
Debug.Print a
End Sub
-
Aug 13th, 2001, 12:27 PM
#38
PowerPoster
Originally posted by badgers
sorry
no worries ... I deserved to be embarrassed on that one!
-
Aug 13th, 2001, 12:29 PM
#39
Fanatic Member
i tried adding,
FBExecuteCode("Debug.Print a", False)
and it worked as you wanted
weird
GWDASH
[b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]
-
Aug 13th, 2001, 12:36 PM
#40
Member
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
|