RUN VBA Code stored in Table

memaxt

Registered User.
Local time
Today, 12:35
Joined
Mar 12, 2013
Messages
62
Hi there,

Is it possible for me to store VBA Code in a table and execute on a form?

For example:

My table contains a field called VBA, that field contains the following code: Docmd.Quit

is it possible to reference that VBA Code on a form? e.g. a button that will reference that VBA code from the table and execute it?

Many thanks
Max
 
God forgive me for posting this: Yes, yes you can do that. First, my plea to you to not do it:

This is a bad idea. Generally you can code around this and put your code in a module where it belongs, this is the way it should be done. When you do what you are asking, you are allowing people with access to the table to screw things up, more than they usually screw things up when they have access to tables.

With that said, you want the Eval function (https://msdn.microsoft.com/en-us/li...v=office.11).aspx?f=255&MSPPError=-2147217396). You pass it a string and it treats it like code and runs whatever is in that string. So, you could use a Dlookup to the table, get the value of your field and pass it to Eval.

Again though, this is usually a poor programming practice. A hack way around a problem that would be better solved another way. Highly recommend against this.
 
Many thanks for your reply!

I take your serious warning onboard..I completely agree with too.

What I'm trying to do is a simple quick fix which your solution seems to do!

but i'm struggling with the eval function itself after reading the microsoft article.

Eval = (DLookup("vbaCode", "vbatable"))

Am I close?

still struggling to execute it.

KindesT regards
Max
 
You don't set Eval equal to anything, you pass it a string and it does whatever that string tells it to do.

For example:

Dim x=0

Eval("x = x +1")

MsgBox(x)

Would show a message box that has "1" appearing as the message.
 
Thank again Plog,

I do / dont understand I tried:

dim x=dlookup("vbacode","vbatable")

Eval("x")

or

Eval("x = x")


...a little confused on how to refernce "docmd.quit" from the table using the Eval solution.
 
Your inability to construct a string really has me advising you to not implement this method. No offense, but I'm not going to walk you down this path any further.

If you would like to explain what you are trying to accomplish (the big picture, not this step of it), I would be happy to help you construct a good method in achieving it. So start from the beginning, what are you ultimately hoping to achieve?
 
Woops

I meant

Dim VbaString as string

Vbastring = dlookup("vbacode","vbatable")

Eval (vbastring)


Apologies I can build strings I'm having one of those days

What I'm trying to achieve is a quick fix table that contains Vba code so users can execute the code as and when it comes available to fix existing problems. The table is shared on a network to multiple databases

All the user has to do is click the button and the code runs. And then moves on to the next row to execute the next code and so on
 

Users who are viewing this thread

Back
Top Bottom