pcc
Well-known Member
- Joined
- Jan 21, 2003
- Messages
- 1,353
I have tools that are used by many users around the company, and their machines have differing connected drives. Sometimes I need to write a temporary file to (any) drive, and therefore need to find a drive letter that is read-write, so that the temp file can be written to this drive.
As a belt and braces job, I am trying every drive from A to Z, but am struggling to get the error handling to work. In the example here, I get an error when the drive is A, (I have no A drive) and the code goes to 'nextdrive'. However, after resetting the error handler, it then fails when looking for the B drive, (I have no B drive either) with 'Path not found' error.
What am I missing? I always seem to struggle with error handling!
All suggestions welcomed. Thanks
As a belt and braces job, I am trying every drive from A to Z, but am struggling to get the error handling to work. In the example here, I get an error when the drive is A, (I have no A drive) and the code goes to 'nextdrive'. However, after resetting the error handler, it then fails when looking for the B drive, (I have no B drive either) with 'Path not found' error.
What am I missing? I always seem to struggle with error handling!
Code:
Sub check_available_drive()
Dim ff As Integer
ff = FreeFile
init = 1
drv = "a"
retry:
On Error GoTo nextdrive
tfile = drv & ":\" & Environ("username") & ".csv"
Open tfile For Append As #ff
Close #ff
Kill tfile
' once it gets here then it has found a drive to write to
MsgBox drv
Exit Sub
nextdrive:
Close #ff
On Error GoTo 0
init = init + 1
Select Case init
Case 2: drv = "b": Case 3: drv = "c": Case 4: drv = "d": Case 5: drv = "e": Case 6: drv = "f": Case 7: drv = "g":
Case 8: drv = "h": Case 9: drv = "i": Case 10: drv = "j": Case 11: drv = "k": Case 12: drv = "l": Case 13: drv = "m":
Case 14: drv = "n": Case 15: drv = "o": Case 16: drv = "p": Case 17: drv = "q": Case 18: drv = "r": Case 19: drv = "s":
Case 20: drv = "t": Case 21: drv = "u": Case 22: drv = "v": Case 23: drv = "w": Case 24: drv = "x": Case 25: drv = "y":
Case 26: drv = "z"
End Select
GoTo retry
End Sub
All suggestions welcomed. Thanks