Solved

Get Form Name in vba module

Posted on 2013-11-01
14
3,136 Views
Last Modified: 2013-11-05
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.
0
Comment
Question by:Lawrence Salvucci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39617671
This is one way to get a form name to a generic function...

Declare a function or sub like this:

Sub MyFunction(strFormName as string)
     ' use the form
      msgbox Forms(strFormName).Name
End SUb

Open in new window


Call  it like this from a command button (for example) on some form:

MyFunction Me.Name

Open in new window



Controls can be passed and referred to as well... but might  need more detail for more targeted help.
0
 
LVL 75
ID: 39617680
You can start with this:

Dim frm As Form
Dim sName as String
Set frm = Form_YourFormNameHere

sName = frm.Name
0
 
LVL 75
ID: 39617685
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
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 21
ID: 39617694
I would pass the from object to your function

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 

Open in new window


In the form call it like this:

MyFunction Me

Open in new window

0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39617701
@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?
0
 
LVL 75
ID: 39617720
define 'Pulling' ...
Also ... you might look at mzipup's approach, wherein you 'pass' the info to a Function ...
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39617731
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.
0
 
LVL 75
ID: 39617746
(mzipup's  - unintentional typo)

then mbizup's approach is what you need ...
0
 
LVL 21

Assisted Solution

by:Boyd (HiTechCoach) Trimmell, Microsoft Access MVP
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 166 total points
ID: 39617752
I would pass the form object to your function in a standard code module. Then you can reference controls on the form like when the VBA code in behind the form.

Example:

Public Function MyFunction(pfrm as Form)
     ' use the pfrm.  like you would use Me.
    
      ' to get the form name use
       MsgBox pfrm.Name

        ' get the recordID from the control txtrecordID.
        MsgBox pfrm.txtrecordID 


End Function 

Open in new window

                                         

In the form call your function in a standard code module it like this:

MyFunction Me

Open in new window

0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39617773
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.
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 167 total points
ID: 39617789
The MsgBox was just an example to display the name that was passed.

To actually pass - you would use what she posted  next:

MyFunction Me.Name

or

Call MyFunction (Me.Name)
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39617801
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:


'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   

Open in new window


You would call the sub from any form like this:

MySub Me.Name, "txtMyTextboxName"

Open in new window

0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 39618204
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?
0
 
LVL 61

Accepted Solution

by:
mbizup earned 167 total points
ID: 39618647
You don't need to pass anything else.  If you have the control name, you can get to any of its other properties like this:

'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)   
           dim s as string
           s = "the value contained in " & strControl & " On " & strForm & " is: " 
 
          '  The following uses the value property to display the control's value
           msgbox s & Forms(strForm).Controls(strControl).Value

End Sub   

Open in new window


You would call the sub the same way as before:

MySub Me.Name, "txtMyTextboxName"

Open in new window

0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

622 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