473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

I'm getting a compiler error even though the sub is defined

Seth Schrock
2,965 Expert 2GB
I have created a subroutine that is declared as public in one of my form modules. In another form module I am trying to call it. However, when I try to compile my code, I get an error that says Sub or Function not defined.

I'm calling it like this:
Expand|Select|Wrap|Line Numbers
  1. Call InOutStatus
And it is declared like this:
Expand|Select|Wrap|Line Numbers
  1. Public Sub InOutStatus()
  2.  
  3. End Sub
When I actually run the code, it works fine. Why am I getting a compiler error?

Note: By form module, I mean the object that gets created in the Microsoft Access Class Objects group in the VBA editor whenever you create an event in the form. I'm not exactly sure what to call it, so I chose form module.
Jan 16 '13 #1

✓ answered by NeoPa

Form Modules, while visible to all code, are not General Modules, so need qualification when referred to. The code should read :
Expand|Select|Wrap|Line Numbers
  1. Call Form_frmYourForm.InOutStatus
or (only if the form is open) :
Expand|Select|Wrap|Line Numbers
  1. Call Forms("frmYourForm").InOutStatus
Alternatively, it is very unusual for code from a specific form to be called from outside of that form. It normally (Though not necessarily) indicates the procedure is in the wrong place.

10 1399
NeoPa
32,556 Expert Mod 16PB
Form Modules, while visible to all code, are not General Modules, so need qualification when referred to. The code should read :
Expand|Select|Wrap|Line Numbers
  1. Call Form_frmYourForm.InOutStatus
or (only if the form is open) :
Expand|Select|Wrap|Line Numbers
  1. Call Forms("frmYourForm").InOutStatus
Alternatively, it is very unusual for code from a specific form to be called from outside of that form. It normally (Though not necessarily) indicates the procedure is in the wrong place.
Jan 16 '13 #2
Seth Schrock
2,965 Expert 2GB
That did it. Even though during normal use the called form will be open, I went ahead and used your first option so that if I ever need to do some troubleshooting on the calling form, I don't have to worry about making sure another form is open.

Thanks NeoPa!
Jan 16 '13 #3
ADezii
8,834 Expert 8TB
Just some useless information. A Sub-Routine defined within a Form's Class Module is essentially a Method of that Form, ergo the
Expand|Select|Wrap|Line Numbers
  1. Form.Sub-Routine 
Calling Syntax.
Jan 16 '13 #4
Seth Schrock
2,965 Expert 2GB
I wouldn't call that useless information. It helps it make sense to me of why. Thanks ADezii.
Jan 16 '13 #5
NeoPa
32,556 Expert Mod 16PB
I agree Seth. It's not remotely useless :-)

Oh, and BTW you're welcome to the assistance.
Jan 16 '13 #6
NeoPa
32,556 Expert Mod 16PB
I forgot to mention earlier that there is another strange issue that comes up every now and again where the compiler seems to get its knickers in a twist and complains about something not being there, when clearly it is. I've found that by cutting the declaration code out, then pasting it back in again, the compiler seems to come to its senses and again recognise that it actually exists. Don't worry about this if your issue is already fixed, but for any others reading this thread where nothing else has worked, this simple fix is always worth a try.
Jan 17 '13 #7
TheSmileyCoder
2,322 Expert Mod 2GB
If your code is acting "weird" (yes that really is the best word I could come up with) then I have found its often a matter of in-consistent compilation, the cause of which is usually editing "live/running" code. Don't ever do that!

I have found it often helps to decompile the application:
http://bytes.com/topic/access/insigh...-fresh-compile
Jan 17 '13 #8
TheSmileyCoder
2,322 Expert Mod 2GB
Note that forms are a class, and that YOUR form is a sub-class of forms. Thats why you can use the following:
Expand|Select|Wrap|Line Numbers
  1. Dim frm as Form_YOURFORMNAME
  2. Set frm=Forms("Form_YOURFORMNAME") 'Only works if open
Now you might wonder what the benefit of this would be. The clear benefit is that you actually get intellisense of the controls in the form. Just like you can do Me. to show (among others) controls in your form, you can also now do frm..
Jan 17 '13 #9
NeoPa
32,556 Expert Mod 16PB
Smiley:
Don't ever do that!
Indeed. I appreciate the potential problems involved. I actually do do it regularly myself, but I have procedures I follow which ensure this problem doesn't occur with me any more (At least I can't remember it happening for ages and ages so I assume they're reliable). Nevertheless, I have kept up with the linked thread and, in general, would advise members similarly (to your quoted comment that is. Avoid it like the plague).

Nevertheless, I still remember it happening and I can't assume that everyone has avoided the problem just because we have Smiley, hence the idea to post my quick fix (which essentially flags the code as not compiled, just as a specific decompilation would).

Smiley:
Expand|Select|Wrap|Line Numbers
  1. Dim frm as Form_YOURFORMNAME
  2. Set frm=Forms("Form_YOURFORMNAME") 'Only works if open
I found, in A2003, that it could be run, and does work, whether the form is open or otherwise. However, it causes the form to stay loaded (if not necessarily open) if an attempt is made to close it while your code still has any active references to it. Similarly, it will cause the object to be loaded if your code runs at a time when it is not already loaded.

In most cases this will not be an issue, but I have fallen over it myself when dealing with all the forms in a database with (literally) hundreds of them to process through. My process crashed complaining of lack of resources, as I recall. Tidier code was required once I appreciated what was happening.

Anyway, I digress. I just wanted to add the Government Health Warning (that I should have added earlier when I first introduced the idea into the thread.), in case anyone saw it and decided to use it more extensively.

BTW Smiley's point about all forms and reports being classes in their own right, which are simply derived classes from the main classes Form and Report that all Access databases have, is absolutely right and can be an important point to be aware of.
Jan 17 '13 #10
TheSmileyCoder
2,322 Expert Mod 2GB
I will admit to doing it myself once in a while (Editing live code) but I do try to steer clear of it, since I know and realise the potential issues involved. It sometimes feels like that "game" boys played as kids, who could hold their hand over the flame the longest without getting burnt. Its fun and games, but well, every once in a while somebody got burnt!

The decompilation however will affect all modules, where your fix only affects a single module at a time.

Also remember that you can safely modify values during breakmode, by using the immediate pane. That can be quite usefull, to more quickly try out different scenarios.
Jan 17 '13 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: paul calvert | last post by:
I hope somewhere here has encountered and solved a similar problem in the past. 1) on a new Win2000 PC: installed Visual C++ 6.0 download & install single file Service Pack 5.0 2) try to...
2
by: EmmanuelE | last post by:
Just installed VS2003 and tried a hello world app. Getting 404 error even when I explictly specify the start page in the url. In IIS Mgr (v 5.1), App has .aspx mapped to...
4
by: jt | last post by:
I'm getting a compiler error: warning C4172: returning address of local variable or temporary Here is the function that I have giving this error: I'm returning a temporary char string and its...
11
by: ravi.sathyam | last post by:
Hi, so basically I define a struct as follows: struct speechSegment { float coeff_Array; float period; float energy; }; struct speechSegment segment = { /*I fill this struct with info */};
4
by: Richard | last post by:
Hi All, I am using Visual C++ .Net to create a windows forms application and I am getting the following errors when I do a MessageBox::Show in my Form1.cpp file: Form1.cpp(19): error C2653:...
1
by: Jay | last post by:
Hi, I have been working on ASP.NET 2.0. When I was trying to work with one of the master pages, I used some classes (.vb files) in App_code directory. When the application is run, it gives me the...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
0
by: gmsieling | last post by:
We're getting compiler error CS0006, with increasing frequency. It says that a randomly named DLL was not found. This happens at random intervals when you try to run the application. We're using the...
3
by: Yahooooooooo | last post by:
Hi, whats the issue in below code ... #include <iostream> #include <fstream> #include <sstream> #include <string> #include <iterator> #include <boost/regex.hpp>
4
by: ayan4u | last post by:
HBITMAP patternBMP =NULL; patternBMP = (HBITMAP)LoadImge(hInstance, "bitmap.bmp", IMAGE_BITMAP,0,0,LR_LOADFROFILE); getting a compiler error error C2664: 'LoadImageW' : cannot convert...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.