Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA 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 March 8th, 2005, 12:57 PM
Authorized User
 
Join Date: Jun 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default elementary question on Public attribute

It seems that even if a Sub is declared public in the code for a form it is not possiblne to call it from outside the form-code screen.
Is this true?

As an alternative is there any kind of method to effect an event on a form like a method to emulate a button having been clicked via vba?

I can't easily moving the target sub out of the form code to a module (where public means something), but there is a 3rd party activex which must resides on a form so we are stuck with the form.

Any hints will be appreciated.
__________________
Tim
 
Old March 8th, 2005, 11:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi rahzan,

You can call the Sub (treating it like a method of the form class) if you first reference the form via an object variable. The following uses a form named Form1:

In the form class:

Public Sub MyMethod()
    MsgBox "Test"
End Sub

In a standard module:

Sub TestSub()

    ' Declare an object variable
    Dim frm As Form

    ' Instantiate the object variable as an instance
    ' of your form class
    Set frm = New Form_Form1

    ' Call the Sub as a method of the form object
    ' variable.
    frm.MyMethod

End Sub

HTH,

Bob



 
Old March 8th, 2005, 11:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Oh, and free the heap memory allocated to the form object reference when your done with it:

Set frm = Nothing

 
Old March 9th, 2005, 01:15 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Bob's solution, while correct, isn't always necessary. You can simply call the procedure in the form's class module.

E.g. Formname=frmTest, Buttonname=btnOk_Click

Your code would then be
  Call Form_frmTest.btnOk_Click

The form doesn't have to be open to use the code. However, if the code you're using needs the form to be open, you might want to be sure it is open before you call the procedure.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 11:04 AM
public article - public articledetails _keysersoze_ BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 8th, 2007 08:38 AM
public sub or function ? kscase Visual Basic 2005 Basics 3 May 20th, 2007 03:14 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
xsl:sort- select attribute, syntax question Flashlight XSLT 3 August 14th, 2003 12:27 AM





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