Invalid Outside Procedure Error -- Module Referencing a Form

blongmire

New Member
Joined
Jul 3, 2014
Messages
35
Hi All,

I'm trying to create a module to avoid repeating the same code each time I'd like to us it.

I've created the below module called "Update" and can successfully call this module from my forms. But, when the module runs I receive an error for "Invalid Outside Procedure" with the section of my code highlighted that is referencing a form in the same database as the module.

The code errors on the first time I use "Forms![Ez Comp Form]" in the below code block:

HTML:
Dim CurrentID As Long        CurrentID = Forms![Ez Comp Form].CurrentRecord            Forms![Ez Comp Form].Requery            DoCmd.GoToRecord acDataForm, "Ez Comp Form", acGoTo, CurrentID       Forms![Ez Comp Form].[Payroll subform].Requery        Forms![Ez Comp Form].[Jobs Already Matched subform].Requery        Forms![Ez Comp Form].[Final Competitor Job Selection subform].Requery

Can you help me properly reference my forms in this module? I'm unable to see where I've gone wrong as this code works perfectly as a sub.

Thanks!

Bob
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
"Invalid Outside procedure" suggests a syntax error of some kind (rather than a problem with using this or that form in the code). Check the code for errors. Or post all of the code here so we can see it. Also can you tell what line it is actually complaining about? Do you get an indication of the line with the problem?
 
Upvote 0
The module is very short and here's all the code contained in it:

Code:
Dim CurrentID As Long
    
    CurrentID = Forms![Ez Comp Form].CurrentRecord
        
    Forms![Ez Comp Form].Requery
        
    DoCmd.GoToRecord acDataForm, "Ez Comp Form", acGoTo, CurrentID
   
    Forms![Ez Comp Form].[Payroll subform].Requery
    
    Forms![Ez Comp Form].[Jobs Already Matched subform].Requery
    
    Forms![Ez Comp Form].[Final Competitor Job Selection subform].Requery

The error doesn't like the first time it encounters the word "Forms" in the second line that's part of:

Code:
CurrentID = Forms![Ez Comp Form].CurrentRecord

I've check that the code works as a sub, but when I insert the same code into a module, it gives me that error.
 
Upvote 0
Hmm. Well, if that's all the code then yes, it is invalid outside a procedure. It needs to be *inside* a procedure.
Code:
[COLOR="#B22222"]Sub My_Procedure()
[/COLOR]Dim CurrentID As Long
 
    CurrentID = Forms![Ez Comp Form].CurrentRecord
    Forms![Ez Comp Form].Requery
    DoCmd.GoToRecord acDataForm, "Ez Comp Form", acGoTo, CurrentID
    Forms![Ez Comp Form].[Payroll subform].Requery
    Forms![Ez Comp Form].[Jobs Already Matched subform].Requery
    Forms![Ez Comp Form].[Final Competitor Job Selection subform].Requery

[COLOR="#B22222"]End Sub[/COLOR]

The first line is okay outside of a procedure since it follows the syntax for declaring a private module level variable.

I'm guessing now you are confused. But it would have to be in a procedure of some kind, so that you can call it when you need to run it. Otherwise it's just random code you dropped into your project and the compiler will not understand what it is supposed to be doing with it.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top