Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Time Error 5 in Compiled Version VB 6.0

Status
Not open for further replies.

Weltman

Programmer
Apr 27, 2001
17
US
I get a run time error 5 (Invalid Procedure or Argument) in the compiled version of my program only, when trying to use the MsgBox Function to display a message in a Click Event Procedure.
Here's the Procedure:
Private Sub Check5_Click(Index As Integer)
On Error Resume Next
'Countem to make sure there's no overvote.
CountChecks(5) = 0
MaxVotes = 2
Call CountemUp5(Index, MaxVotes)
If TooMany Then OverVoteMsg
End Sub
Here's the message:
Public Sub OverVoteMsg()
Response = MsgBox("You cannot exceed the maximum number of votes allowed in this category.")
If Response = 1 Then TooMany = False
End Sub
I have determined that the error is generated only if I use the MSgBox Function, and only in the compiled program.
Can anybody help?
 
First off . . . Never, Never, NEVER start a routine with On Error Resume Next unless you have a really, really, REALLY good reason to. Doing this is just asking for a logic bug of some type. On Error Resume Next should be used as a Try-Catch device. It can be turned on for 1 or 2 lines and then turned off right away.
There, having said that, lets look at your code . . .Right off hand, i don't see any thing that would cause an Error #5, but I have no idea what you are doing in the method call "CountemUp" Please post that code and I'll check it for you. - Jeff Marler B-)
 
Thanks for your advice so far. I'm not even sure I'm doing this posting correctly, but here's the code:
Private Sub CountemUp5(Index As Integer, MaxVotes As Integer)
For IndexNumber = 0 To 3
If Check5(IndexNumber).Value Then
CountChecks(5) = CountChecks(5) + 1
End If
If CountChecks(5) > MaxVotes Then
TooMany = True
Exit For
End If
Next IndexNumber
If TooMany Then
'Remove the last vote. Don't allow the voter to overvote.
Select Case Index
Case 0 To 2
Check5(Index).Value = 0
Check5(Index).SetFocus
Case 3
Check5(Index).Value = 0
NameBox5(Index).Text = ""
NameBox5(Index).SetFocus
End Select
End If
End Sub
Weltman
 
Weltman -

You might want to make sure that Check5(Index) and Namebox5(Index) are visible. If you try and setfocus to a non-visible control you'll get this error.

Chip H.
 
Yes they are visible, and as I said, This all works fine until after I compile it.
Weltman
 
Does this problem happen all of the time? Or just once in a while?
Aside from what Chip mentioned, the ony real problem (potential problem) is if all of the elements of your control array were not there or if the index values were not exactly 0,1,2, or 3.
I know that compiled code can at times run differently than code running in the IDE due to the way threading is handled in each environment, and that may (I stress the word may) be what is causing your problem.
Also, it would be very helpful to know what line the problem is happening on. There is a very simple technique that will allow you to figure out the line that is throwing the error in your compiled application using the largely undocumented Line Number attribute of the VB Error object. Please do the following . . .


1) Add an "On Error Goto ErrorHandler" in your code. Note that this is not an "On Error Resume Next"

2) Number all of the lines in the routine that is throwing the error.

3) Add an error handler that will display the error number and the line number that is throwing the error.



Code:
Private Sub CountemUp5(Index As Integer, MaxVotes As Integer)
Code:
On Error Goto ErrorHandler
Code:
10    For IndexNumber = 0 To 3
20      If Check5(IndexNumber).Value Then
30         CountChecks(5) = CountChecks(5) + 1
40      End If
50      If CountChecks(5) > MaxVotes Then
60         TooMany = True
70         Exit For
80      End If
90    Next IndexNumber

100    If TooMany Then
       'Remove the last vote. Don't allow the voter to
       'overvote.
110       Select Case Index
120            Case 0 To 2
130                Check5(Index).Value = 0
140                Check5(Index).SetFocus
150            Case 3
160                Check5(Index).Value = 0
170                NameBox5(Index).Text = ""
180                NameBox5(Index).SetFocus
190        End Select
    End If
Code:
Exit Sub
ErrorHandler:

     Msgbox "Error #" & err.number & " on line #" & err.erl
Code:
End Sub


Try to run with this code and let us know what line number is causing the error.
[/code][/color]


- Jeff Marler B-)
 
Just adding this string so that the column display is wider in order to make the code above easier to read.
========================================================================================================================= - Jeff Marler B-)
 
Jeff,
I tried running the program with your numbered lines of code and the error handler. I do not get the message from the error handler at all. I only get the run time error message, and when I click OK I get the message that I was trying to display in the event of an overvote.
But, here's what I think is an important clue to the problem. I just haven't figured out what to do with the clue. What I have is an array of Check Boxes that I have named Check5. There are four Check Boxes in the array (indexes 0 through 3). I'm supposed to get my overvote message when more than two of the boxes are clicked. The peculiar thing is that I only get the run time error message when index 3 is the 3rd one clicked. For example, If I click on Check5(3) first and then any two of the other three boxes, everything works fine. I get the overvote message on the 3rd click. If I click on any two of the top three boxes and then the bottom one [Check5(3], that's when I get the run time error 5.
Another thing is that I only have the problem using the mouse. when I use the keyboard (keydown event) to set the value of the check boxes to true, I don't have the problem.
Following is code that I have in the Mousedown event procedure (similar to that in Keydown for Keycode 13 (Enter Key).
Private Sub Check5_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case Index
Case 3
Select Case Check5(Index).Value
Case 0 'Not Checked
Check5(Index).Value = 1 'Check it.
NameBox5(Index).SetFocus
Case 1 To 2 'Checked
Check5(Index).Value = 0 'Uncheck it.
NameBox5(Index).Text = "" 'Remove name.
NameBox5(Index).SetFocus 'Enter a new name?
End Select
End Select
End Sub
Weltman
 
The fact that you are still getting the error message and not the message from the error handler tells me that some other routine in you code is throwing the error. In fact, your original code contrained an "On Error Resume Next", so you should've never seen any error at all thrown by that routine or any other routine that it called.
I think that you may need to add an error handler to all of your routines so that you can figure out what routine i actually throwing the error. If the error was being thrown by the CountemUp5 subroutine, the error handler would've caught it. - Jeff Marler B-)
 
I found the problem, thanks to you guys!
It was in the GotFocus Event Procedure, and as Chip suspected, was caused by "SetFocus."
At the bottom of the procedure (NameBox5(3).SetFocus:
Private Sub Check5_GotFocus(Index As Integer)
Frame1(5).BackColor = &HE0E0E0 'Highlight Frame
For IndexNumber = 6 To 11 '
Frame1(IndexNumber).BackColor = &HC0C0C0 'Remove Category highlight.
Next IndexNumber
IndexNumber = 0
For IndexNumber = 1 To 4
Frame1(IndexNumber).BackColor = &HC0C0C0 'Remove Category highlight.
Next IndexNumber
Select Case Index
Case 0 To 2
Screen.ActiveControl.BackColor = &HFFFFFF 'White
Timer1.Enabled = True
If Check5(3).Value = False Then NameBox5(3) = "" 'Clear text.
Case 3
NameBox5(3).SetFocus
End Select
End Sub
By the way when I put the error handler in with your line numbers, I got a run time error 438 (Object doesn't support this property or method).
So, I just REMed out one line at a time, and compiled it again, each time until I found the problem.
Another by the way: The On Error Resume Next at the top of my procedure was part of the trial and error process that I was going through to find the problem. I should have just done that for all of the procedures.
Thanks Again. I'll be hanging around here more often in the future.
Weltman
 
Weltman,
hmmmm . . . Erl is throwing an error? You're right . . . so is mine! Grrrrr! MS must've changed this property! It was undocumented which you can basically read as unsupported so there is not guarantee that it will work as service packs and new versions come out! Personally, I have not actually had to use Erl for awhile, so it may have changed. Sorry if I lead you astray.

Chip,
Are you able to get Erl to work? I am getting the same error that Weltman is getting (Error 438). The IDE recognizes it and will compile it, but when it tries to access it at runtime, it crashes? - Jeff Marler B-)
 
Jeff -

Erl is not a property of the error object. It's a standalone Function/Variable/Thingie. If you were to change your sample code to:
[tt]
ErrorHandler:
Msgbox "Error #" & err.number & " on line #" & Erl
End Sub
[/tt]

it would work.

Chip H.
 
IF EVERYTHING HERE FAILS, TRY SETTING THE MSGBOX TO MODAL, I think you set the last argument to vbModal Ive ran into this if you have a label, textbox or something being changed while the msgbox is up on the screen. I've even went so far to circumvent this that I rarely use a msgbox unless I'm really sure nothing on any other form in my project is likely to change while the box is up. I use VB5 and 6 and notice this worse in VB6, however the ServicePack3 common controls update allows for modal msgboxes
 
AudioPlayer65,
My understanding is that the MsgBox is automatically modal, since the button value for modal is 0.
Incidentally, I am still having problems with this whole thing, but it's a long story, and I'll post it as soon as I have time to summarize it. Thanks for your response.
Weltman
 
Weltman is correct . . . by default MsgBox is always Modal. - Jeff Marler B-)
 
Update:
Moving the line of code (See Line 50 below)from the GotFocus Event Procedure to the Click Event Procedure solved the problem.

Private Sub Check5_Click(Index As Integer)
On Error GoTo ErrorHandler
'Countem to make sure there's no overvote.
10 CountChecks(5) = 0
20 MaxVotes = 2
30 Call CountemUp5(Index, MaxVotes)
40 If TooMany Then OverVoteMsg
50 If Index = 3 Then NameBox5(3).SetFocus
Exit Sub
ErrorHandler:
MsgBox "Error #" & Err.Number & " on line #" & Erl
Resume Next
End Sub

But when I got to the next form, I had a similar problem. This time, the same solution didn't work. However, this time instead of just having an array of Check Boxes and one Text box, there is an array of Checkboxes and an array of Text Boxes.

Private Sub Check6_Click(Index As Integer)
On Error GoTo ErrorHandler
'Countem to make sure there's no overvote.
10 CountChecks(6) = 0
20 MaxVotes = 3
30 Call CountemUp6(Index, MaxVotes)
40 If TooMany Then OverVoteMsg
Select Case Index
Case 3 To 5
50 NameBox6(Index).SetFocus
End Select
Exit Sub
ErrorHandler:
MsgBox "Error #" & Err.Number & " on line #" & Erl
Resume Next
End Sub

If I click on more than 3 CheckBoxes, I am supposed to get the Overvote Message (MsgBox) but, Line 50 is throwing run time error 5, in certain situations.
For example, if I click on Check6(2), Check6(3), Check6(4), and Check6(5), in that order, I get the error. I presume that is because, when I click on Check6(4), Line 50 sets the focus to the text box NameBox(4).
Then, when I click on Check6(5), at the time that Line 50 tries to execute, the TextBox [NameBox6(4)] has the focus, rather than the CheckBox. The problem is that I haven't found a solution. Does anybody have any suggestions? Or, even some words of encouragement would help, at this point.
Weltman
 
Another Update.
For anybody who might be interested, this time I solved the problem by putting the instruction to set focus to the textbox back into the GotFocus Event Procedure, but this time I made it conditional as follows:

If TooMany = False Then NameBox6(Index).SetFocus

This eliminated the conflict with the lines containing SetFocus in the following segment:
40 If TooMany Then
50 OverVoteMsg
'Remove the last vote. Don't allow the voter to overvote.
Select Case Index
Case 0 To 2
60 Check6(Index).Value = 0
70 Check6(Index).SetFocus
Case 3 To 5
80 Check6(Index).Value = 0
90 NameBox6(Index).Text = ""
100 NameBox6(Index).SetFocus
End Select
End If
Thanks to those of you who posted messages above.
Weltman
 
HELP!
I get "Compile Error" "Only comments may appear after end sub, end function or end property" OK HELP. A visual basic window appears and after "end sub" a highlighted phrase appears "on error resume next".
This happens everytime I open any Word document. When I close, I have performed an illegal something and word shuts down.

HELP! HELP! HELP! HELP! HELP! HELP! HELP! HELP!
Mark
 
Mark,
If You're talking about a sub in Visual Basic then the sub has to end with "End Sub". If you have the line of code "On Error Resume Next" after "End Sub" then just delete the line and recompile you program.
Weltman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top