Execute dynamic VBA code just like docmd.runsql(SQLtext)

Posted on 2006-03-27
Last Modified: 2012-08-13
I am trying to make my app flexible and accept code on the fly.

I am using msaccess 2003 for the frontend and MS SQL2000 for the backend.  I compile all projects and the users run an *.ade file.

currently I create the dynamic code in T-SQL and execute it on the SQL server.  This as it is not secure and could crash with the wrong code.  I want to execute the code locally and/or use VBA code.

So basically I would like to create a string like:

str2Exec = "=iif(Value = 1, "S", "C")"

Then Replace the "Value" with a real number:

str2Exec = Replace(str2Exec, "Value", 1 )

Then finally exec the string and save the result

strResult = RunVBA(str2Exec)

Any ideas on how to execute this code on the fly?
Question by:ScottGutman
    LVL 2

    Accepted Solution

    Have you tried the Eval() function?
    LVL 58

    Expert Comment


    If your strings contain only standard VB operators and built-in functions, you can use "Eval":

        ? Eval(Replace(" IIf(Value = 1, ""A"", ""B"") ", "Value", "13"))


    Author Comment

    exactly what i was looking for!!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    The first two articles in this short series — Using a Criteria Form to Filter Records ( and Building a Custom Filter ( — discuss in some detail how a form can be…
    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now