I have a simple Function Test() in Module1 and a Sub SubName() in Module2. I'm trying to call SubName() from Function Test(). When I type:
Call Module2.SubName
and exit the raw, I'm getting an error:
Compile Error: Expected: identifier
When I just type Module2 and put Stop, it offers me the name of the SubName in Module2. So it sees it, but looks like is missing something in syntax. I can't figure out what.
Some related links :
MSDN-Calling Sub and Function Procedures
SO-Calling a Sub in VBA
SO-What does the Call keyword do in VB6?
Solution to your question :
Either use Call SubName or just SubName.
Testing:
After some testing, I've been able to break the code. The error is NOT the same you get, but maybe this will bring us closer to solution.
Picture from VBA:
The solution is simple: just use the statement
SubName
and it'll be called.
Few clarifying remarks:
No need to use the Call statement. If you do however, you need to use Call SubName()
If you call any sub with parameters, use SubName Param1 - or call Call SubName(Param1). Don't use Subname(Param1)- if param1 is an object, this will pass it's default property to the sub, not the object (e.g. Subname (Range("A1")) will pass the value of the cell to the sub - but not the Range object - Subname Range("A1") will pass the Range object.
You can also use Module2.SubName - however this is only required, if you have multiple SubName sub in different modules
Related
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)
I am new to vbscript and trying to use the below code with a Function and then a Sub Procedure. I am confused as to why I have to "call" the function as it should itself return the value without calling the function. Please see the below pieces of code - one with Function and the other one with Sub Procedure.
Function -
Systemutil.Run"C:\Program Files\Internet Explorer\iexplore.exe","www.gmail.com"
Function tester()
Set tester=Description.Create
tester=Browser("title:=Gmail").Page("title:=Gmail").WebButton("html id:=next").GetROProperty("Name")
print tester
End Function
Call tester
If I don't call the function, it doesn't return anything.
I have used function name as a variable to output the value.
Sub Procedure-
Systemutil.Run"C:\Program Files\Internet Explorer\iexplore.exe","www.gmail.com"
Sub tester()
Set X=Description.Create
X=Browser("title:=Gmail").Page("title:=Gmail").WebButton("html id:=next").GetROProperty("Name")
print X
End Sub
Call tester
My question is why doesn't the first script work without calling the Function.
The first part of your code only defines your function. It doesn't execute the code in it yet.
Whether it returns a value or not is irrelevant. If you want the code in your function or sub to execute, you must call it.
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).
I have a form (named DateForm) that contains a textbox (named txt_AsOfDate), which displays a date value.
I have a separate SUB which has the following code:
Sub Test()
Dim AsOfDate As String
AsOfDate = txt_AsOfDate.Text
MsgBox (AsOfDate)
End Sub
When running it, I get the following error:
Run-time error '424': Object required
What is going on? I tried loading the DateForm at the beginning of the SUB, and also tried assigning the value by further defining the object schema like below, but no luck. What am I doing wrong?
AsOfDate = DateForm.txt_AsOfDate.Value
Try replacing txt_AsOfDate.Text with Form_DateForm.txt_AsOfDate.Text. Referring to a control directly in code by its name only works in the form, whereas I'm guessing this code is in a separate module.
In order to catch things like this, add Option Explicit to the top of your code modules. This forces the compiler to notify you if there is a variable being used which wasn't first declared with Dim.
I have a click event on a form in Access 2010 that looks like so:
Private Sub SaveRecord_Click()
checkDataIntegrity(Me)
End Sub
Where checkDataIntegrity is defined as follows:
Function checkDataIntegrity(ByVal fForm As form) As Boolean
This works fine and dandy. However, I have another click event:
Private Sub LFS_Flashed_Successfully_Fail_Click()
preventSimultaneousPassAndFail (Me)
End Sub
Where preventSimultaneousPassAndFail is defined as follows:
Function preventSimultaneousPassAndFail(ByVal fForm As form) As Boolean
When I invoke this click event I get the following error:
Run-time error '13':
Type mismatch
What am I doing wrong here? Both event calls are invoked in the same form and call functions in the same module.
You don't seem to be doing anything with the functions' return values, so don't enclose the arguments in parentheses.
'preventSimultaneousPassAndFail (Me)
preventSimultaneousPassAndFail Me
That is standard VBA practice. Unfortunately, I can't explain why it triggered an error in one case but not the other. So this issue may not contribute to the problem, but I still suggest you make that change to rule it out.
Although we don't know any details about those functions, I suggest you consider passing the form objects ByRef instead of ByVal.
The reason why you get the error is that in the case of
preventSimultaneousPassAndFail (Me)
the parenthesis expression tries to invoke the default member of the object. In case of an Access.Form this is the Name property being of type String. That's the reason for the type mismatch error.
In case of
preventSimultaneousPassAndFail Me
the reference to the form itself is passed to the method.