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

Basic VBA question

Status
Not open for further replies.

GaryRW

Programmer
Mar 8, 2002
67
GB
I think this is pretty basic stuff, but that's probably why I can't work it out...

Can you have a string and execute that as if it was a line of code, e.g.

CodeStr = "Forms![Form1]![Control1].setfocus"

and then a command or something along the lines of

RunCode(CodeStr)

I'm on blue sky thinking mode here, and I realise it's probably bad coding, but putting together a string and then executing it would help me a lot.

Anybody got any suggestions?
 
Hi,
What type of command are you planning to run? There is one function called Shell that does do something similar.

Eg.
Shell "C:\Notepad.exe " & yourTextFile


Shell "C:\NotePad.Exe MyFile.txt"

Send us more details if you want somehitng else. Hope it helps. Let me know what happens.
With regards,
PGK
 
Thanks PGK,

The code I'm trying to execute is just VBA.

The current situation is that a control has the focus, but which control will be different each time. I move the focus elsewhere, then I want to move it back to the original control. Hence the example string is

"Forms![Form1]![Control1].setfocus"

Before moving the focus, I could assign it's name to a string, then afterwards construct the full string above using the control name, and then (hopefully) just execute the above string as if it was a line of VBA code.

I realise the correct method in this example would probably something along the lines of:

Dim ctlCurrentControl As Control
Set ctlCurrentControl = Screen.ActiveControl
(move the focus elsewhere, then )
ctlCurrentControl.setfocus

but the above does not work (my understanding of objects is v. limited), and I think the whole executing a string approach would get me out of quite a few other tight spots.

Is it possible? Would the Shell "C:\NotePad.Exe MyFile.txt"
approach execute the textfile as VBA code or as something else (e.g. DOS commands?)?

Cheers for all the help
 
What you need to do is change your code to:

Forms![Form1].controls(VariableWithControlName).setfocus

HTH

Ben

----------------------------------------
Ben O'Hara
Home: bpo@SickOfSpam.RobotParade.co.uk
Work: bo104@SickOfSpam.westyorkshire.pnn.police.uk
(in case you've not worked it out get rid of Sick Of Spam to mail me!)
Web: ----------------------------------------
 
Hi,
The Shell command in the previous example would open the specified text file in Notepad. Thats all. Hope it helps. Let me know what happens.
With regards,
PGK
 
Thanks Ben, that solved the instant problem.

I'd still like a general way of getting round this though. I think the simplest way to explain it, is that I'm after the equivalent of:

Docmd.RunSQL(SQLString)

But instead of running SQL code contained in a string, I'm looking for a way of:

RunVBA(VBAString)

Anybody know of a way?

 
The eval() function might go someway to what you are trying to acheive.

Eval "msgbox(""hello"")"

gave me a message box with hello in it. May be worth some extra investigation if you can be bothered.

B ----------------------------------------
Ben O'Hara
Home: bpo@SickOfSpam.RobotParade.co.uk
Work: bo104@SickOfSpam.westyorkshire.pnn.police.uk
(in case you've not worked it out get rid of Sick Of Spam to mail me!)
Web: ----------------------------------------
 
Cheers Ben - seems to work a treat.

Figured it was something simple (unfortunately what I do know is self taught, and so fundamental stuff is not my strong point...)
 
Don't worry about it. I am self taught too!

:)

B ----------------------------------------
Ben O'Hara
Home: bpo@SickOfSpam.RobotParade.co.uk
Work: bo104@SickOfSpam.westyorkshire.pnn.police.uk
(in case you've not worked it out get rid of Sick Of Spam to mail me!)
Web: ----------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top