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

Check for CD 1

Status
Not open for further replies.

JonathonC

Technical User
Aug 18, 2001
43
GB
How can I get my app to check to see if there is a CD in the CD Rom drive. If there is a CD inserted I want the app to do this:

Code:
Test = Environ("SystemRoot")
If Dir(&quot;D:\Search&quot;) <> &quot;&quot; Then
brwWebBrowser.Navigate (&quot;D:\Parts\index.htm&quot;)
Else
    Form1.Show
End If

and if there is no CD do this:

Code:
Form3.Show

Jonathon.
 
Hey Jon,

Look in the help for File System Object. This should be able to show you how to access the drives, and may have a &quot;CDInDrive&quot; function of something listed.

Jack
 
You can possibly adapt the following code: it looks for a CD drive and if it finds one it checks if the drive is ready or not.
Dim objFso As FileSystemObject
Dim objDrive As Drive

Set objFso = New FileSystemObject

For Each objDrive In objFso.Drives
Select Case objDrive.DriveType
Case CDRom
Select Case objDrive.IsReady
Case True
'do further processing
Case False
MsgBox &quot;Insert CD into drive &quot; & objDrive.DriveLetter
End Select
Case Else
End Select
Next

Set objDrive = Nothing
Set objFso = Nothing


Code may also be modified to look for a specific drive (&quot;D&quot;) if you know it beforehand (always a dangerous assumption).

In the do further processing case, you can look for a specific CD by its name (objDrive.VolumeName), its Serial Number (objDrive.SerialNumber) or its RootFolder (objDrive.RootFolder)

Another modification could allow for the presence of more than one CD on a system: above code will send the message box for each CD present (including DVDs) _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Hi, I tried the code but I got the Error User defined object not defined and it highlighted the first line of the above code. What's the problem with it? Sorry if this a stupid question, but you see i'm very new to Visual Basic.
Oh, and by the way JFrost, I had a look in the help file, and no joy, thank all the same.

Jonathon.
 
You need to reference the object Microsoft Scripting Runtime.

Go to your menu: Project->Reference and find Microsoft Scripting runtime.

Put a check by it and press ok. Then run it again.

Hope that helps.
 
hi Jonathonc :

You can get the drives you have by using the following API functions:

1- GetLogicalDriveStrings (ByVal nBufferLength As Long,
ByVal LpBuffer As String) As Long
2- GetDriveType (ByVal nDrive As String) As Long

The first one gives you all you drives
The second one gives you the type of the drive

You can use this code to get your CD-Rom dive

Private Function Getcdromdrive() As String
Dim L As Long
Dim St As String * 60
Dim Dr As String
Dim C As String * 1
St = Space$(60)
L = GetLogicalDriveStrings(60, St)
Dim I As Integer
Dim Typ As Long
Dim Finish As Boolean
Finish = False
I = 0
Do While (I < L) And Not Finish
Dr= &quot;&quot;
C=&quot; &quot;
Do While C <> Chr(0(
I = I + 1
C = Right(Left(St, I), 1(
If C <> Chr(0) Then Dr = Dr & C
Loop
Typ = GetDriveType(Dr(
If Typ = 5 Then Finish = True
Loop
Getcdromdrive = Dr
End Function
 
Tuan3249 showed you all the way.... _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Thanks everyone for your reply's, it's now working fantasticly.

Thanks again
Jonathon.
 
One final thing which I forgot to mention earlier, I have replaced 'do further processing in the above code (Posted by rvBasic) with:
Test = Environ(&quot;SystemRoot&quot;)
If Dir(&quot;D:\search&quot;) <> &quot;&quot; Then
brwWebBrowser.Navigate (&quot;D:\Parts\index.htm&quot;)
Else
Form1.Show
End If

This checks to see if the correct CD is in the drive by looking for the file &quot;search&quot; and if it finds it, it opens an HTML page stored on the CD.
Is there a way of replacing &quot;D:&quot; (third line down) with the users drive letter? So something like
brwWebBrowser.Navigate (&quot;[driveletter]:\Parts\index.htm&quot;)

Jonathon.

 
Try brwWebBrowser.Navigate objDrive.DriveLetter & &quot;:\Parts\index.htm&quot;

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top