Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Access Developer 18
Vicent Mnaya 
   
4 years ago
How can I access a public sub present in one form from another form
I have this code in PBarF
Private Sub AbortBtn_Click()
    Abort = True
End Sub

Public Sub MyPBar(PercentComplete As Long)
    PBFront.Visible = True
    PBFront.Width = PBBack.Width * (PercentComplete / 100)
    PBFront = PercentComplete & "%"
End Sub

I tied to run MyPBar public sub from MonthlyBatchF with the following code
Private Sub Command0_Click()
    Dim X As Long
    DoCmd.OpenForm "PBarF"
    With Forms!PBarF
        !Abort = False
        DoCmd.Hourglass True
        !AbortBtn.Visible = True
        X = 1
        While X <= 100 And Not !Abort
            .MyPBar X
            X = X + 10
            DoEvents
        Wend
        DoCmd.Hourglass False
        If Not !Abort Then
            .MyPBar 100
          
        Else
            !PBFront = "Abort!"
        End If
    End With
End Sub

The form (PBarF) opens but MyPBar sub does not run, what could be the problem?
Kevin Yip  @Reply  
     
4 years ago
To call a function or sub inside a form from somewhere else, you need to use:

Form_PBarF.MyPBar()

Since MyPBar belongs to a form's module, you need to reference the form's module when you call it.  The form's module's name is Form, underscore, form name.  In the VBA editor, the left side shows all the form modules' names, which all begin with "Form_".

To simply call MyPBar on its own, put it in a public module.  In the VBA editor, the left side under Modules is where you create public modules.

Vicent Mnaya OP  @Reply  
   
4 years ago
Thank you Kevin, I have tried still it doesn't work
Alex Hedley  @Reply  
           
4 years ago
Wouldn't they also need to be Public?
Kevin Yip  @Reply  
     
4 years ago
Hi Vicent, the sub MyPBar refers to "PBFront" (an item on the form).  But PBFront is known only to the form PBarF.  Other forms don't know it.  To have other forms call this sub, you need to refer to PBFront in a fully qualified manner: Forms!PBarF!PBFront.  And the PBarF form needs to be opened for that to work.  The code will be more cumbersome, of course.  And this is not really an ideal way to "share" a sub among forms.  What is usually done is write a public sub in a stand-alone module (in the VBA editor, under Modules on the left), and have all forms call it.
Kevin Yip  @Reply  
     
4 years ago
Hi Alex, the sub doesn't need to be public, since a form's module is technically a class module.  And private subs inside a class module are still accessible everywhere because the class itself is public.  A form doesn't even have to be opened for you to able to call its private subs by using Form_FormName.PrivateSubName.  That's because the "class" of the form is always present and public even if the form itself isn't opened.

But as I said in the other post, this isn't a great way to call a sub for practical purposes such as in this case.  (But it's an inherent feature for class modules and works great for them.)
Kevin Robertson  @Reply  
          
4 years ago
PBarF is referenced in the code With Forms!PBarF

Vincent, What actually happens when you run the code? Do you get an Error Message?
I ran an example with your code and the 'Progress Bar' filled as expected.
Scott Axton  @Reply  
        
4 years ago
Kevin -
If you are going to run it from different places wouldn't it be better to move the whole thing out to a GlobalMod?
Kevin Robertson  @Reply  
          
4 years ago
You could put MyPBar in a Global Module and send the name of the form as a parameter.

Public Sub MyPBar(FormName As String, PercentComplete As Integer)

    With Forms(FormName)
        !PBFront.Visible = True
        !PBFront.Width = !PBBack.Width * (PercentComplete / 100)
        !PBFront = PercentComplete & "%"
    End With
    
End Sub


Then add the name of the form in the call.

MyPBar "PBarF", X
MyPBar "PBarF", 100
Kevin Yip  @Reply  
     
4 years ago
Vicent, I concur with Kevin and Scott's idea of using a global module, even though your method should work too.  Maybe in your Command0_Click sub, don't open the form PBarF.  Have it already opened earlier but make it invisible.  Then set it to visible in Command0_Click.  That way you are sure the form is there.  Sometimes when a form is just opened, Access may not know it's there.  That may be the reason your sub isn't run.
Kevin Robertson  @Reply  
          
4 years ago
Surprised no-one has noticed my deliberate mistake in my post above.
Vicent Mnaya OP  @Reply  
   
4 years ago
Thank you all, through your contributions i finally made it. Thank you very much.
Richard Rost  @Reply  
          
4 years ago
Kevin Yip: I'm curious why you say "the sub doesn't need to be public." In order for one form to run code on another form, it must be public. It doesn't have to be open, but it cannot be a private sub. Private, by definition, means that only other procedures in that form's module can access it.

Kevin Robertson: You mean declaring PercentComplete as an Integer?

I think we need more Kevins. :)
Kevin Robertson  @Reply  
          
4 years ago
Nope. No need to send the form name as it's always going to be 'PBarF'. 😁
Kevin Yip  @Reply  
     
4 years ago
Hi Richard, if you call a form's procedure as below, the procedure doesn't need to be public:

Form_MyFormName.MyProcName

That's because "Form_MyFormName" is the class module of the form MyFormName. The "class" itself is public, so its procedures can be private and still can be called anywhere.  

If you call the procedure by referencing the form itself, as below, then yes, the procedure needs to be public:

Forms!MyFormName.MyProcName

P.S. The OP seems to have solved his problem, so the info above may be moot.

P.P.S. I suggested the OP to keep the form opened and set its visibility instead. If that was the solution that worked for him, this was the third time this solution has worked for someone in your forum. I tend to avoid having to open and close forms (it's my own evil Access thing) because you often don't know if Access "knows" a form is opened or not, right after you opened it. Having a form opened permanently (if you can help it) solves that.
Vicent Mnaya OP  @Reply  
   
4 years ago
Kevin, I tried all all suggestions but didn't solve problem, the problem was solved by setting focus on the abort button, the code itself is correct.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 8:31:26 AM. PLT: 1s