Using variables as function names and parameters in VBA - ms-access

I have the following variables in my VBA logic sFunctionName, sParam1, sParam2, sParam3 all string variables populated from a table. I would like to use those variables to call a function. I have tried using Application.Run(sFunctionName, sParam1) and the statement fails, However when i use Application.Run(sFunctionName) it works just fine. I have also tried Eval on someones suggestion with no luck. Can someone let me know what I am missing, or if i can even do what I am attempting to do? I appreciate any and all help.
Thanks,
J

#1. Regarding Eval(): Per Access's help files,
You can use the Eval function to
evaluate an expression that results in
a text string or a numeric value.
So, if your function resolves to text or numeric, then you're good to go.
i.e. Debug.Print Eval("Date()")
#2. I don't think your problem with Run() is with the actual function itself, but rather how you are applying it. I threw together some quick code. Does this help?
Function AddOne(What As Integer) As Integer
AddOne = What + 1
End Function
Function x()
Dim WhichFunc As String
WhichFunc = "AddOne"
Dim What As Integer
What = 1
x = Run(WhichFunc, What)
End Function
(Calling this with debug.print x in the Immediate Window will give you a 2)

Try running a method without braces, like:
Application.Run sFunctionName, sParam1

Related

Access VBA: Passing Form/Subform name to a Function?

I am attempting to pass a form/subform name to a function without success. The function steps thru each .Control on the subform and performs a simple set of look-ups & actions. My code works as expected with the Form/Subform name hard coded; I am looking for a more generic approach.
Getting a type mismatch error on the function call, with and without quotes.
Example:
'Function Call
call AuditChanges("forms![someForm]![someSubForm]")
'Audit Function
Sub AuditChanges(thisForm as form)
Dim ctl As Control
Dim strTest as string
For each ctl in thisForm.controls
strTest = ctl.Value
'do some stuff
Next ctl
end sub
Any suggestions for proper syntax to pass the form/subform info?
Thanks!
Your sub call is all kinds of weird:
The likely cause of your error is the quotes. You're currently passing a string to your function containing "forms![someForm]![someSubForm]"
Furthermore, you shouldn't use parentheses when you're not receiving a return value (so never when calling a sub).
Also, the Call keyword has been deprecated a long time ago.
And you likely want to pass the form object, and not the subform control object
Try calling your sub like this:
AuditChanges forms![someForm]![someSubForm].Form
(Never had this many comments on one line of code before)

VBA - Why do I get a compile error expected function or variable

I am curious to understand why the following throws and compile error stating it is expecting a function or variable
Dim frm as Form
set frm = DoCmd.OpenForm(FormName:=mstr_FORM_NAME, View:=acDesign, WindowMode:=acHidden)
but I can do this
DoCmd.OpenForm FormName:=mstr_FORM_NAME, View:=acDesign, WindowMode:=acHidden
set frm=Forms(mstr_FORM_NAME)
I have no issue with doing what works, I just want to understand what is going on with the former statement.
Thank you,
Fred
DoCmd.OpenForm is a method which doesn't return a value. In the second code snippet you are accessing the Forms Collection, which contains the form, after it has been opened by DoCmd.OpenForm. When you call a method, you must not specify braces.
The OpenForm method (doc) is not returning anything, just opening the form (not returning it).
So, you're trying to cast Nothing into a variable defined As Form.
Instead, frm (having the Set frm = statement) is expecting to "become something", it is expecting a function or variable - a value, to be clear).

What's wrong with my VBA code (I'm getting runtime error 9)

I've looked around and found that the "runtime error 9 subscript out of range" error usually has to do with arrays. However, I'm not sure how to interpret the answers I've found to best suite my code.
I have a simple function that I wrote to pull the first and last names out of a field, I then found that the field had a few Nulls in it and tried to compensate by changing from a String to Variant decleration.
I've gotten this to work without an error with similar code... but not with this code.
My code is as follows:
Function FLastName(ByVal lnIn As Variant) As Variant
Dim astrFullName() As String
If IsNull(lnIn) Then
FLastName = Null
Else
ReDim astrFullName(2) 'This is a new concept to me... not sure if I really need this
astrFullName = Split(lnIn, ",")
FLastName = astrFullName(0)
End If
End Function
Thanks in advance for your insights and assistance.
EDIT:
I found that the issue is that the array doesn't like the Null value in some of the name fields... I thought that the following line would fix that:
If IsNull(lnIn) Then
FLastName = Null
But apparently I was wrong.
Anyone have some advice on what I've found thus far?
You dont really need to ReDim ... and you dont even have to Dim it as an Array.
Function FLastName(ByVal lnIn As Variant) As Variant
Dim astrFullName
If IsNull(lnIn) Then
FLastName = Null
Else
astrFullName = Split(lnIn, ",")
FLastName = astrFullName(0)
End If
End Function
PS: Assuming that lnIn would contain a comma separated string ... else you may have to put a check.
I found the answer!
Allen Browne (who is totally awesome) has a "ParseWord" function available on his site that takes care of what I need to accomplish.
It can be found here: http://allenbrowne.com/func-10.html

Windows Media Player in MS Access

I would like Access to "automatically" play a song whenever a combo box is updated. I want that song to change depending on the contents of the combo box. Unfortunately the below code returns:
Constant expression required
Here's the code I'm working with:
Private Sub cboCustomerID_AfterUpdate()
txtSongFile = Me.cboCustomerID.Column(2)
Me.Refresh
Const conMEDIA_FILE_TO_OPEN As String = Me.txtSongFile
Me![WindowsMediaPlayer1].openPlayer (conMEDIA_FILE_TO_OPEN)
End Sub
Any help would be greatly appreciated!! Thank you.
The error you mentioned is a compile error, not a run time error. It is caused by this line:
Const conMEDIA_FILE_TO_OPEN As String = Me.txtSongFile
The reason is that the VBA rule for what you can use on the right side of the = sign is quite restrictive. This is what Access' online help says:
"Literal, other constant, or any combination that includes all arithmetic or logical operators except Is."
But you should be able to use a variable instead:
Dim strMEDIA_FILE_TO_OPEN As String
strMEDIA_FILE_TO_OPEN = Me.txtSongFile

If statement within VBscript function not working

I am hoping someone can help me possibly debug the below code, as I am confused as to why it is happening.
I have a fairly simple VBScript that runs when a user logs onto a server/PC, that will create some signatures based on their active directory details. I have decided to move each section of the signature creation into a function, in order to make things a bit easier when creating new signatures.
Here is the function I am having issues with:
'Function to add job title and company
Function AddTitle
objSelection.Font.Name = "Calibri"
objSelection.Font.Bold = False
objSelection.Font.Italic = False
objSelection.Font.Size = "11"
objSelection.Font.Color = RGB(0,0,0)
If(strTitle) Then
objSelection.TypeText strTitle & Chr(11)
End If
objSelection.TypeText strCompany & Chr (11)
End Function
Now, when calling the function later on using:
'Add job title and company
AddTitle
It ignores the section within the If statement. I know that some variables need to be defined globally for them to work in a function, and strTitle is definitely defined at the beginning of my script.
Am I missing something totally obvious, as the section inside the if statement functions correctly if taken out of the if statement. Likewise, If I were to add the if statement to my script inline, and take it out of the function, it works correctly.
It is only when running from the Function.
Apparently you have a global On Error Resume Next somewhere in your script, which suppresses the "Type Mismatch" error that the line If(strTitle) Then normally would raise.
strTitle probably contains a string value, so you can't use it like a boolean value in a conditional. Change the line to something like this:
If Trim(strTitle) <> "" Then
and your code should work as expected.