VB .EXE Problem.

I get Error 5 (Invalid Proc Call or Argument) when running an exe file, but not when am running the debug version. Happens on a MSGBOX statement(no problem when that lines commented out). Occurs in a TextBox_LostFocus of an MDI child. The same line works in other parts of the same program. Would be grateful for any help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Could you post the offending code segment?  It may be what you're trying to pass to the function rather than the function itself.
jkeoghAuthor Commented:
Private Sub txtNACE_LostFocus()
    MsgBox "Entry information complete", vbOKOnly, "Information"
End Sub

Thats all thats in the procedure.
By the way I should have mentioned that I'm running VB5.
Try to use something like:

MessageString = MsgBox ("Entry information complete", vbOKOnly, "Information")

Message box is a function, not a subroutine. One of the best ways to call a function is to assign it to a variable.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jkeoghAuthor Commented:
I tried this and it has made no difference to my problem. I accept yfang's point that I was calling MSGBOX in a 'lazy' way but I have done the same in other programs, and in other parts of the current program without any problem. It is only this
particular MSGBOX call which causes me the problem. It is definitely this line as when I comment it out I have no crash.
Thanks for your interest, yfang, and please re-reply if you
have any other suggestions.

just out of curiosity...

what happens if you change the line of code to its non-"lazy" form:

    call msgbox ...


Are you using doevents somewhere in this code?
jkeoghAuthor Commented:
I narrowed this problem down a bit further. In FormLoad I make a
call to a sub called AbleControls which sets the enable property of all controls on my form to True or False (in this case False) Those controls which have no enable property are excluded by use
of ifs. When I comment out the call to this module I have no problems with my MSGBOX call later on. This suggests to me that
that when when running the .EXE as opposed to the debug version,
there is some extra control that I am disabling.
Here is the Subroutine.

Sub AbleControls(F As Form, TrueFalse As Boolean)
    Dim lclCtl As Control
    Dim lclObj As Object
    Dim lclStrCtlType As String
    'Disable all controls on the form
    For Each lclCtl In F.Controls
        lclStrCtlType = TypeName(lclCtl)
        If lclStrCtlType = "Menu" Then
            'Do nothing
        ElseIf lclStrCtlType = "TabStrip" Or _
               lclStrCtlType = "SSTab" Then
            'Do nothing
        ElseIf lclStrCtlType = "Toolbar" Then
            'Do nothing
        ElseIf lclStrCtlType = "ListView" Then
            'Do nothing
        ElseIf lclStrCtlType = "CommonDialog" Then
            'Do nothing
        ElseIf lclStrCtlType = "Line" Then
            'Do nothing
        ElseIf lclStrCtlType = "CrystalReport" Then
            'Do nothing
            'We are OK to set the enabled property
            lclCtl.Enabled = TrueFalse
        End If
End Sub

P.S In reply to Mirkwood and StapleHead, I am not using DoEvents
and the problem was still occuring no matter what way I was calling MSGBOX.
Why not put all controls on a frame and disable the frame. This will have the same result.
jkeoghAuthor Commented:
If possible I'd prefer not to do as you're suggesting as I have about 200 forms on this system, and I'd have to put a frame on
each of them, new source code etc. Ideally I'd like to put a fix
into the AbleControls subroutine above to fix any problem.
Its unfortunate that I waited till I basically had my system
finished before trying to make an .EXE. This is the first VB
system I've done and I'll take on board the frames thing next time.
On error resume next will most likely get rid of your problem
jkeoghAuthor Commented:
I placed 'On Error Resume Next' before the MSGBOX call but I still have the same problem. I tried checking for err.number = 5 and using err.clear but I still always get the crash.

ok... what you're trying to do in ableControls is set the Enabled property for all controls EXCEPT Menus, TabStrips, etc...

If you're thinking that you're setting the Enabled property of something that you shouldn't be, then let's look at your approach from another perspective.  Exactly which types of controls DO you want to go after?  If you enumerate them in an if/elseif or case, then you might not hit a control which you don't want, and not get the error...

There are probaply some parts in your program that can't be complied by the VB complier.

MsgBox "Entry information complete", vbOKOnly, "Information"

There is someting in this sentence that VB did not complie proberly.
Try first what is efect of compile to P-code/ Native code.

If this don't give proper combilation then try this:

Replace vbOKOnly whit 6&

This will cause Vb not to use constant value in here,
if the problem is in the VB compilation part, it will not
need to get this constant from the library file.

jkeoghAuthor Commented:
Firstly Matti. I tried the two things suggested and neither made
any difference.
Secondly Staplehead. What you said made sense. I changed AbleControls to only operate on the types of controls that I wanted Enabled/Disabled. These being TextBoxes,Commandbuttons,
ComboBoxes and Labels. I still had the problem. I then commented out each of them in turn and found that the problem was only occuring on ComboBoxes. I moved my disabling of ComboBoxes into
my main program and found that my problem is now this....
The MSGBOX line crashes only when I have disabled the Combo Box on my form. If I comment out that line, everythings fine.
Again just to emphasize that this occurs only when running the
EXE. Works perfectly OK doing a normal debug.
some questions for you:

how are txtNACE and your combo box related? does data flow between them, based on user interaction?

are you certain that it's at the *msgbox* line that you're crashing? is it possible that it's crashing before that line is executed?  (try some sort of debugging, like changing the color of a control or something *immediately* before the msgbox line.)

are you attempting any manipulation of the combo box in the "vicinity" of your execution of the msgbox command? take a look at statements which "touch" the combo box variable.  are any properties accessed or altered when the combo box is disabled, sometime just before the txtNACE_LostFocus event handler is called?

are you doing any OOP? are there any objects/classes at work here? (i'm trying to determine whether any other code is executing that might be easy to overlook..)

finally, are you willing to post or email your code?

jkeoghAuthor Commented:
Larry S.
I think I have this solved. Its ties in with your third point above though I dont understand in totally yet (I've only been
doing VB about 4 weeks)
I have some command buttons which get enabled, disabled or setfocussed depending on whether TxtNace is empty,non-empty or
full, with a check after every keystroke (txtNace_Change).
It seems that when I tabbed off the Nace, code in one of the CommandButtons_GotFocus was going off before txtNace_LostFocus.
I dont really understand it as it doesnt happen when I'm debugging.
Anyway I made some changes to the order of source code and things
seem OK. As you put me on the right track, if you send a solution
thingummy to me, I'll accept it and give you the points.
Thanks to yourself and everyone for their help and I'm sure I'll
talk to you again,
John Keogh.

actually, getting the order of event firing down is, in my opinion, one of the most frustrating parts of learning VB.  just when you think you've got it down cold, something like that sneaks up and bites you...


Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.