Solved

Error handling ... how to improve it?

Posted on 1998-11-03
4
166 Views
Last Modified: 2010-04-30
Hi,
  I am reading a list of files from a listbox and then printing each one. I added some error handling to handle missing files, which will continue with the next file to print. However, I am not very clear with the error handling of VB and it seems my code can be improved.
Soemtimes, when there is a error (listbox is empty), if I press Print again, there is a error "For loop not initialised". I know it has to do the 'nextF' statement but I am not sure how to improve/ correct it.
I provided main parts of my print function...
sorry, I don't have much points left... :(
Thanks a lot!

David

Private Sub Print_Click()
' declarations

On Error GoTo handleErr
    For i = 0 To (List1.ListCount - 1) ' read from list1
    On Error Resume Next

    Open (list1.text) For Input As #1 ' Open file.
 
    Do While Not EOF(1) ' Loop until end of file.
      ' print line      
    Loop
   
nextF:
    ' close file      
    Next i

End If ' response = vbyes
Exit Sub 'avoid error handler

handleErr:
    Select Case Err.Number  ' Evaluate error number.
        Case 32755 ' printer error
            MsgBox ("Error printing. Printing aborted.")
            Exit Sub
        Case 53: ' file not found
            MsgBox ("File " & ToPrint & " not found. Continuing with next file.")
            GoTo nextF
   
    End Select
    Err.Clear

End Sub
0
Comment
Question by:Haho
4 Comments
 
LVL 1

Expert Comment

by:powlin
ID: 1443031
to check if the listbox is empty you can use the if list1.listcount  = 0 then exit sub
for the error handling you can use Err.description and err.number to know all the error posible without a huge Select case with all the error you can check if list1.listindex = -1 if there's no item selected
0
 
LVL 4

Expert Comment

by:CraigLazar
ID: 1443032
HI
Maybe create a sub funcion that calls the error handler every time there is an error
on error goto eh:
..
..
..
..
eh:
 'call your function and in your function make sure you have the resume next statement

hope this helps
craig

0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 20 total points
ID: 1443033
Improved code

Private Sub Print_Click()
' declarations

if (list1.listcount > 0) then
    For i = 0 To (List1.ListCount - 1) ' read from list1
      on error resume next
      Open (list1.List(i)) For Input As #1 ' Open file.
      if (err.number <> 0) then
           on error goto error_readfile
           Do While Not EOF(1) ' Loop until end of file.
               ' print line
           Loop
:error_skipfile
          ' close file
       end if
    Next i
End If

Exit Sub 'avoid error handler

Error_readfile:
    Select Case Err.Number  ' Evaluate error number.
        case 0
        Case 32755 ' printer error
            MsgBox ("Error printing. Printing aborted.")
           'close file
           exit sub
        case else
            msgbox Err.desc & " skipping file"
            resume error_skipfile  
    End Select
End Sub
0
 
LVL 1

Author Comment

by:Haho
ID: 1443034
thanks !!! For the time being I am doing:
for i ...
 open file
 if err.number <> 0 then
   err msgbox
 else
  printing code
 end if
next i

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 58
A macro to Count the number of rows across all worksheets 3 84
How does CurrentUser work? 10 31
Recommendation vb6 to vb.net or others 14 114
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now