VBA - run VBA code, which is stored as a string code - vba

i want to execute VBA code, which is stored as a string, i would like to execute this, for example :
dim code as string
code = "Msgbox ""test"""
ExecuteCommand(code)
Please help me to execute this . Thanks :)

I believe Eval will work in Access with functions as long as the function call includes parenthesis
code = "Msgbox(""test"")"
Eval(code)
ref: Eval function

Related

Run-time error '3265': Item not found in this collection. VBA module in Access

I am trying to create a macro that will copy a table and its associated queries for each year's equipment audit. I copied some code that I found on another forum, but I receive the error in the title on one line. I'm not familiar enough with VBA to understand what the problem is, so I'm hoping I can get some help.
This code is supposed to replace the source table on a copied query so that the queries don't have to be remade each year. Here is the code:
Sub UpdateQuery(QueryName, CurrentSourceTable, NewSourceTable)
Dim strQryName, strCTbl, strNTbl, strCsql, strNsql As String
Dim defqry As DAO.QueryDef
strQryName = QueryName
strCTbl = CurrentSourceTable
strNTbl = NewSourceTable
Set defqry = CurrentDb.QueryDefs(strQryName)
strCsql = defqry.SQL
strNsql = Replace(strCsql, strCTbl, strNTbl)
defqry.SQL = strNsql
defqry.Close
End Sub
The error occurs on the "Set defqry" line. Can anyone tell me what is causing the error?
ETA:
When I try to run the code above, I'm also using the following code to fill in QueryName/CurrentSourceTable/NewSourceTable:
Sub Proc1()
Call UpdateQuery(YYYY_Count_of_items_by_floor, Building_Audit_2021, Building_Audit_YYYY)
End Sub
Again, I did not write this code, I copied code that someone else had written and am attempting to use it for what I need.
Thanks, HansUp! YYYY_Count_of_items_by_floor is indeed the name of the query, so enclosing it in quotes was all that I needed to do.

Execute .exe in SSRS Report

i want to execute a .exe in a SSRS Report from Microsoft Dynamics AX.
I tried so far to realize that over Custom Code and a Textfield with following Expression: =Code.StartProcess("test")
Public Function StartProcess(ByVal s As String) As String
Dim pHelp As New ProcessStartInfo
pHelp.FileName = "test.bat"
pHelp.Arguments = s
pHelp.UseShellExecute = True
pHelp.WindowStyle = ProcessWindowStyle.Normal
Dim proc As Process = Process.Start(pHelp)
Return "it works"
End Function
I get the error ":StartProcess is invalid. InvalidIdentifier"
As second try i use this:
="javascript:void(window.open('file://AX2012R2A/Share/batch.exe'))"
in an action expression.
This trial opened in the report the following message by clicking on the image:
My problem is now that I have to pass a parameter to the batch and then it doesn't work.
Have you any idea to help me?
I have solved my Problem.
As first step I mentioned, that also simple methods like:
Public Function StartProcess(ByVal s As String) As String
Return "it works"
End Function
brought the error: ":StartProcess is invalid. InvalidIdentifier"
As second step i found this link:
Link to Google Group discussion
(perhabs you have to expand all messages).
As last step i have to create a Datamethod and then call it in an expression and it works.
The code in the datamethod isn't in VB.Net but in C#.
How this works, can you see here (Youtube)
Thank you all for your help.

VBA does not recognize my company's user defined function

I'm conducting a VBA code where I use a function defined by the company. This function is contained in a Project which is Password protected, which I will not get access to, and I'm rather sure it should not be needed.
I can call the function through the worksheet "directly", i.e. in a cell I can write =TONNES(B2,B3,B4) and the cell output is the correct value. However in VBA when I use:
Private Sub CommandButton1_Click()
Dim param1 as String, param2 as String, param3 as String
param1 = Cells(2,2)
param2 = Cells(2,3)
param3 = Cells(2,4)
Cells(1,3) = TONNES(param1, param2, param3)
End sub
I get the error that the function TONNES is not a sub or function.
I have saved my Macro in a Module, and I don't have access to the actual code of the User defined function. Is there a way to be able to use this through VBA? As it can be used directly in the worksheet, but not through VBA, can that conclude that the function is disabled through VBA for some reason?
EDIT: The function is an add-in (more specifically I have a Company add-in which contains the data from production in a summarization page, which has created these functions in order to present these in the summarization page).
So I've finally found a solution. Basically going into Tools -> References in the VBA Environment (Alt + F11) and adding in the specific references caused a solution to this issue. Hopefully this may be of help for someone else with this problem.

Access vba pass variable into code

I'm in Access 07 trying to pass a variable to a line of code. The purpose is to make an array of field names and then loop over the array performing the same action on each field. I've simplified it down to avoid any potential problems with the array or the loop.
In any other language I am familiar with it would be pretty simple, but I can't seem to format it in VB.
Me.FeildName.Locked = True
would be the static code, and I think the variable code would look something like this:
Dim Temp as String
Temp="FieldName"
Me.[Temp].Locked = True
but it keeps giving me an error that says "can't find the field '|' referred to in your expression", so it's not reading the value of the variable.
How do I get it to read the variable in the command?
Alternatively, I've tried to concatenate strings into a line of code:
Dim CodeLine As String
Dim TestName As String
TestName = "FieldName"
CodeLine = "Me.[" & TestName & "].Locked = True"
This creates a string that looks like functional code but how would I run it?
Thanks
If Me is a form, you need
Temp = "FieldName"
Me.Controls(Temp).Locked = True
Dick Kusleika's answer is the way to go, but for completeness' sake, you can do also something like this:
For i = LBound(strControlNames) To UBound(strControlNames)
CallByName Forms![MyFormName].Controls(strControlNames(i)), "Locked", VbLet, "True"
Next
strControlNames would be the array with your control names.

How can I return the results of a function to a cell in Excel?

Suppose I have a function attached to one of my Excel sheets:
Public Function foo(bar As Integer) as integer
foo = 42
End Function
How can I get the results of foo returned to a cell on my sheet? I've tried "=foo(10)", but all it gives me is "#NAME?"
I've also tried =[filename]!foo(10) and [sheetname]!foo(10) with no change.
Try following the directions here to make sure you're doing everything correctly, specifically about where to put it. ( Insert->Module )
I can confirm that opening up the VBA editor, using Insert->Module, and the following code:
Function TimesTwo(Value As Integer)
TimesTwo = Value * 2
End Function
and on a sheet putting "=TimesTwo(100)" into a cell gives me 200.
Put the function in a new, separate module (Insert->Module), then use =foo(10) within a cell formula to invoke it.
Where did you put the "foo" function? I don't know why, but whenever I've seen this, the solution is to record a dimple macro, and let Excel create a new module for that macro's code. Then, put your "foo" function in that module. Your code works when I follow this procedure, but if I put it in the code module attached to "ThisWorkbook," I get the #NAME result you report.
include the file name like this
=PERSONAL.XLS!foo(10)