(Drive1.Drive) CD IN ??

Posted on 2001-08-29
Last Modified: 2011-08-18
Hi i am using Dir1.Path = Drive1.Drive to pas the selcted drive to a dir1 box

the problem is when i have no CD in mu CDrom bay and this is selected my prgram falls over ??

Question by:silki
LVL 20

Expert Comment

ID: 6436904
LVL 28

Accepted Solution

vinnyd79 earned 50 total points
ID: 6436944
why not do something like this:

On Error GoTo NMEDIA
Dir1.Path = Drive1.Drive
Exit Sub
MsgBox "No Media in Drive!"
Drive1.Drive = Left(Dir1.Path, 2)

Expert Comment

ID: 6440401
Option Explicit
Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
Private Sub Command1_Click()
 Dir1.Path = IIf(SetCurrentDirectory(Left(Drive1.Drive, 2) = 0), Dir1.Path, Drive1.Drive)
End Sub

The setcurrentdirectory (api) will fail when, the directory or drive doesn't exits, so when u dont have a CD in ur drive, this api will fail, hence u can assign the previous drive.

Author Comment

ID: 6440476
Simple yet effective !!



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
How to add contact name to VBA code to automate emails in outlook using a Template 11 71
VBA error replacing data 6 37
MsgBox 2 47
clicking a shape in a frame array vb6 3 40
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

17 Experts available now in Live!

Get 1:1 Help Now