Lawrence Salvucci
asked on
Get Form Name in vba module
I have a function in a module that I want to be able to pull in a specific form name. How do I write that in vba code to declare a form and then pull in the form name? I also need to pull in specific controls on that form as well and not really sure how to do that either.
You can start with this:
Dim frm As Form
Dim sName as String
Set frm = Form_YourFormNameHere
sName = frm.Name
Dim frm As Form
Dim sName as String
Set frm = Form_YourFormNameHere
sName = frm.Name
You can also do this. Note that the first example above actually opens a hidden instance of the form, which may not be what you want. This example does not do that.
Dim frm As Form
Dim sName as String
Set frm = Forms!YourFormNameHere
sName = frm.Name
Dim frm As Form
Dim sName as String
Set frm = Forms!YourFormNameHere
sName = frm.Name
I would pass the from object to your function
Example:
In the form call it like this:
Example:
Public Function MyFunction(pfrm as Form)
' use the pfrm. like you would use Me.
' to get the form name use
MsgBox pfrm.Name
' set the Text in a unbound control on the form named txtMessage
pfrm.txtMessage = "My Message Text"
End Function
In the form call it like this:
MyFunction Me
ASKER
@DatabaseMX
I like you approach where the form isn't opened in a hidden mode. But now how would I go about pulling a control from a specific record as well?
I like you approach where the form isn't opened in a hidden mode. But now how would I go about pulling a control from a specific record as well?
define 'Pulling' ...
Also ... you might look at mzipup's approach, wherein you 'pass' the info to a Function ...
Also ... you might look at mzipup's approach, wherein you 'pass' the info to a Function ...
ASKER
Wrong choice of words. I mean pass the values to the function for the current record that is open on the form. I need to pass the recordID and a couple of other controls into my function.
(mzipup's - unintentional typo)
then mbizup's approach is what you need ...
then mbizup's approach is what you need ...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm confused about mzipup's function and the msgbox. How does that pass the value from the control and the form's name to my function? That's where I'm getting lost.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The message box is just an example of a command that uses the form name as passed to the function.
Trying to clarify, and adding a control as a parameter to pass to the sub:
You would call the sub from any form like this:
Trying to clarify, and adding a control as a parameter to pass to the sub:
'the sub declaration uses the form name as a pass parameter. The actual form name is specified when calling the sub from wherever needed. Adding a control here too
Sub MySub (strForm as string, strControl as string)
' The following shows how to use a string variable to refer to a form (just placing a value on the passed form name and control name)
Forms(strForm).Controls(strControl) = "This is a test"
End Sub
You would call the sub from any form like this:
MySub Me.Name, "txtMyTextboxName"
ASKER
I think I understand now. One final step...how do I pass the value of the control and not just the control name of the current record on my form?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Declare a function or sub like this:
Open in new window
Call it like this from a command button (for example) on some form:
Open in new window
Controls can be passed and referred to as well... but might need more detail for more targeted help.