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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling while doing For....Next.....problem

Status
Not open for further replies.

Mtlca401

Programmer
Mar 18, 2003
42
US
I have a code that opens up each module connected to all forms.
The code looks something like this:

Dim strTemp As String, mdl As Module
Dim docs As Documents, doc As Document
Dim db As Database

On Error GoTo Error

Set db = CurrentDb

Set docs = db.Containers!Forms.Documents
docs.Refresh
For Each doc In docs
strTemp = doc.Name

DoCmd.OpenForm strTemp, acDesign, , , , acHidden

DoCmd.OpenModule "Form_" & strTemp
Set mdl = Modules("Form_" & strTemp)

next_doc:

DoCmd.Close acForm, strTemp, acSaveYes

Next doc

Set db = Nothing


Exit_Error:
Exit Sub

Error:
If Err.Number = 2517 Then
GoTo next_doc
Else
Msgbox Err.Number & " : " & Err.Description
End If
Resume Exit_Error

My problem is that when it gets to a form where the module has not been open at all since the form was created, then I get an error of 2517. That's fine. That is why I have the error handler, so that it goes to next_doc:. But when it gets to another form where the module has not been open at all since the form was created, the error handler does not catch and the error pops up. So basically the error handler is only working one time.

if that is confusing for you then just copy and paste this code under a button on a form and then create about 2 other forms without doing anything with them, just save them. Run the code, and see what happens. Then go open and close the modules to the forms that you haven't done anything with. You should now not get any problems. I can't figure out why it only does the error handlers once.
 
Have you tried "Resume next_doc" rather than
"Goto next_doc"? I'm not saying that's what's causing
your problem, but it might be worth a try.
 
WOW, that actually worked! Who would have thought that changing one word would make that big of a difference.

Thanks
 
I'm not surprised. I've never seen Goto used in an
error handler before, it's always resume.

 
Mtlca,

An error handler is never "resolved" until the resume
statement is executed. You never "left" the error handler
when you got the error 2517, you just transferred the
flow of execution.

Wayne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top