How-To... - Check wether or not a disk is in the drive

If your program is using Drive Boxes, and the user changes the drive to, for example, the A:\ and there is no disk in, then your program will display an error and exit. Not very nice for the person using it. To prevent this from happening you have to catch the error and tell your program what to do when the error occurs. To do this you use the Err Object, more specifically, the Err.Number part of the Object.

If you cut and copy this code, make sure you change the names to suit your needs. If you are following this How-To, put a DriveListBox(Named - drvDrive), and a DirListBox(Named - dirListing). Now, go to the General Declerations of your form and paste the following code into it :-

Sub Error_Handler()
'If the error is number 68(Device Unavailable) then
If Err.Number = 68 Then

'Show a message box, and store users response....
response = MsgBox("The Drive Is Unavailble. Please Insert Disk And Click Retry", 21)

'Start a Case Statement using the response variable...
Select Case response

'If Retry was clicked, then re-run the Drive_Change procedure...

Case vbRetry: drvDrive_Change

'If Cancel was clicked, then change the DirListBox
'back to the previous drive...

Case vbCancel: drvDrive.Drive = dirListing.Path

End Select 'End select statement

End If

End Sub

Private Sub drvDrive_Change()
'When an error occurs, 'continue with next line....
On Error Resume Next

'Update the DirListBox....
dirListing.Path = drvDrive.Drive

Error_Handler 'Run Error_Handler procedure...

End Sub

Private Sub Form_Load()
'Centre form on user's screen...
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

End Sub

'<----END OF CODE---->

You can download the fully-documented Project file from here.


This site is Copyright © 1999 of David Cowan. VB How-To, Dodgesoft and DodgeSoft Productions is Copyright © 1999 of David Cowan. Source-code and programs are copyright of their respective owners. For a messageboard, click here