Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 15th, 2004, 06:45 PM
Authorized User
Join Date: Jun 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Raif
Default calling subroutine from different module?

I have a sub in a module. I'm calling that sub from form"A". Form "A" has a subform"B" . Subform"B" has some code in it as well. From form "A" I want to call sub from module, then set forcus or whatever I need to do to start calling functions from subform"B" which are specific to subform "B". In other words I can't put these in module because they need to run in form "B"
Any Ideas?

Old June 15th, 2004, 11:33 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

Sometimes it helps to think of Access forms as the class modules they are, and to treat them in a more "object-oriented" manner (i.e., by working with user-defined properties and methods).

================================================== ========
Code behind Sub Form - FormB:
Defines properties and methods that can be called from the Main Form - FormA

Option Compare Database

' Module level data member variable.
Dim m_Test As String

' Public methods.
Public Sub TestSubProcedure(strInitialize As String)
   MsgBox "This is the initial string"
End Sub

Public Sub TestSubProcedure2()
   MsgBox m_Test
End Sub

' Public property.
Public Property Get TestPropertyProcudure() As String
    TestProperty = m_Test
End Property

Public Property Let TestPropertyProcudure(TestValue As String)
    m_Test = TestValue
End Property
================================================== ========

================================================== ========
Code behind Main Form - FormA:
Calls properties and methods in the Sub Form - FormB

Private Sub cmdExecuteSubFormProc_Click()

    ' Call your public routine in the standard module here...

    ' Call public method in sub form passing in argument. Displays
    ' MessageBox - "This is the initial string"
    Me("FormB").Form.TestSubProcedure ("This is the initial string.")

    ' Initialize a module level data member variable in the sub form
    ' using a property procedure.
    Me("FormB").Form.TestPropertyProcudure = "This is initialized string."

    ' Call public method in sub form that processes the initialized data
    ' member variable. Displays MessageBox - "This is initialized string."

End Sub
================================================== ========

Be sure your sub form procedures are defined as PUBLIC.

Just as an aside, a new instance of an Access form can always be instantialted and used with the following syntax:

Dim frm As Form
Set frm = New Form_FormB

' initialize user-defined property
frm.AProperty = aValue

' call user-defined method

Should look real familiar to the Visual Studio crowd.

Old June 15th, 2004, 11:56 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

I notice I forgot one piece: retrieving the data member value of the form instance via the GET accessor method. First fix the GET accessor's definition in FormB. It should look like this:

Public Property Get TestPropertyProcudure() As String
    TestPropertyProcudure = m_Test
End Property

I didn't assign the return value to the correct procedure name.

Then add the following to the end of the FormA module:

' Retrieve the stored data member value from the form instance.
Dim propertyValue As String
propertyValue = Me("frmFormB").Form.TestPropertyProcudure
MsgBox propertyValue

Hope some of this helps.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call to subroutine from seperate workbook Coby Excel VBA 2 February 1st, 2008 10:15 PM
how to make subroutine code automatically execute dgr7 Beginning VB 6 2 February 24th, 2007 11:20 AM
passing parameter to a perl subroutine crmpicco Perl 2 February 16th, 2007 08:07 AM
vb6 subroutine to ftp a file? dgr7 VB How-To 2 January 16th, 2007 12:15 AM
Function/Subroutine Pointer Parameter pjm Access VBA 3 October 3rd, 2006 09:07 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.