views:

420

answers:

3

I have recently edited a fellow student's data collection software to my specifications. The program works fine when I run within Visual Basic 6, however ideally I would like to compile the program into an .exe file so I can run it from any PC. However, when I select the option to turn it into an exe, I get the following error while its compiling:

Compile Error: Sub or Function not defined.

I was curious why my program will run from within Visual Basic, but can't compile into an exe. Any fixes/suggestions would be greatly appreciated!

+5  A: 

You must just be running the app in the IDE by using Start (F5) - this does not do a full compile so won't catch all compile errors. I suggest you always use Start With Full Compile (Ctrl+F5) - then it will show you where the errors are.

You can create a custom control bar button to do the Start With Full Compile - saves typing CTRL+F5 all the time.

DJ
+2  A: 

at the begining of every file, write "option explicit". This changes vb behavior from weak typed to strong typed.

What this means is that when you put a string into an integer, then the vb will tell you right away, or at least when you press "run". In a weak typed mode, the program will run until it encounters a fault, like "Sub or Function not defined" then crash.

When you compile into an "exe", vb needs to do extra sanity checks much like "option explicit" would do. This is why you don't see the problem while normal execution. It is still there, lurking, but you probably don't execute the problematic line.

It is very important that you use this keyword in every vb source file you have, otherwise you'll see problems like this all the time.

Eric
This and the previous answeres helped me figure out how to compile my program. Apparently the guy who coded this software before me left out a lot of variable declarations. So I had to go through and actually declare all non-declared variables. After that was done, the program compiled just fine.
Sam
+4  A: 

It allows you to run the program even though it won't compile due to the Compile On Demand feature the IDE supports. As noted in other posts you can CTRL-F5 to do a full compile, or you can go into Options --> General and turn off Compile On Demand...(saves a lot of CTRL-F5s)

cmsjr
Options --> Editor, and turn on 'Require Variable Declaration' is another must-set option
Patrick McDonald
+1 I was obsessive enough to write an answer with my list of must-set options :) http://stackoverflow.com/questions/664370/your-favorite-visual-basic-6-tools-and-tips/667225#667225
MarkJ