Results 1 to 2 of 2

Thread: Check if a drive exists

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    Check if a drive exists

    I'm using the following code to check if a file has been defined.
    HTML Code:
    If txtGradeObs.Text = "" Or Dir(txtGradeObs.Text) = "" Then
         MsgBox("Specify the Grade job of Obstructions", vbExclamation, MsgBoxTitle)
         Exit Sub
    End If
    I'm getting an error if the drive does not exist. I don't think this was a problem in VB6.
    I'm getting an IO exception - bad file name or number.
    How can I get this to work?
    Thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Check if a drive exists

    There are various ways to avoid or handle that error.

    Given the circumstances, I suspect you would like to show the same MsgBox if the drive does not exist, in which case you can use a simple method:
    Code:
    On Error Resume Next
    If txtGradeObs.Text = "" Or Dir(txtGradeObs.Text) = "" Then
         MsgBox("Specify the Grade job of Obstructions", vbExclamation, MsgBoxTitle)
         Exit Sub
    End If
    On Error GoTo 0  '(or if you had a different "on error" line before, repeat it here)
    This will cause the MsgBox to show (and the Exit Sub) if either of the conditions are true, or any error occurs during the Dir.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width