PC Review


Reply
Thread Tools Rate Thread

Changing drive letter in VBA

 
 
=?Utf-8?B?emhqMjM=?=
Guest
Posts: n/a
 
      9th Jun 2007
For mobility reasons, I store all my VBA programmes in a thumb drive (USB).
And in some of my VBA programmes, also contain the drive path with the
associated drive letters. (e.g. opening a file)

As I add more USB devices to my PC, these drive letters keep changing such
that the VBA programme can't read the correct drive letter where the files
are stored.

Any solution to fix the drive letter? Thanks.

zhj23
 
Reply With Quote
 
 
 
 
Zone
Guest
Posts: n/a
 
      9th Jun 2007
I had a sorta similar problem caused by putting my files on other
computers that used network drives that kept getting assigned
different drive letters. My solution was that I added a Control
worksheet to my files. I named one of the cells in that sheet
DriveLoc and put the drive specification in there. Then I just got
the drive spec from that sheet and put it in a string variable named
DrivePath and concatenated it with the rest of the filepath anywhere
in my program that was going to use the network drive. Maybe not an
ideal solution, but at least you have to change the drive spec in only
one place! I guess you could alternatively use a global constant to
hold the drive spec. It was easier for my users to change it on the
Control sheet. HTH, James

On Jun 9, 10:06?am, zhj23 <z...@discussions.microsoft.com> wrote:
> For mobility reasons, I store all my VBA programmes in a thumb drive (USB).
> And in some of my VBA programmes, also contain the drive path with the
> associated drive letters. (e.g. opening a file)
>
> As I add more USB devices to my PC, these drive letters keep changing such
> that the VBA programme can't read the correct drive letter where the files
> are stored.
>
> Any solution to fix the drive letter? Thanks.
>
> zhj23



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      9th Jun 2007
Perhaps a bit of brute force -

Sub test3()
Dim bRes As Boolean
Dim nFirstDriveToSearch As Long
Dim sKnownFile As String
Dim sDrive As String

nFirstDriveToSearch = Asc("E") ' or simply 69 (maybe start with C)

' a known file that only exists in the drive to be found
sKnownFile = "aler.ini" ' this in root but could add folder

bRes = GetMyDrive(nFirstDriveToSearch, sKnownFile, sDrive)

If bRes Then
MsgBox sDrive
Else
MsgBox "not found"
End If

End Sub

Function GetMyDrive(nChr As Long, sFile As String, sDrive As String) As
Boolean
Dim bRes As Boolean
On Error Resume Next
For i = nChr To Asc("Z")
sDrive = Chr(i) & ":\"
bRes = False
bRes = GetAttr(sDrive) = vbDirectory
If bRes Then
bRes = fbFileExists(sDrive & sFile)
If bRes Then
Exit For
End If
End If
Next
GetMyDrive = bRes
End Function

Function fbFileExists(ByVal sFile As String) As Boolean
Dim nAttr As Long

On Error Resume Next
nAttr = GetAttr(sFile)
fbFileExists = (Err.Number = 0) And ((nAttr And vbDirectory) = 0)
On Error GoTo 0

End Function

Regards,
Peter T

"zhj23" <(E-Mail Removed)> wrote in message
news:9B88722B-D2D7-468B-8E8D-(E-Mail Removed)...
> For mobility reasons, I store all my VBA programmes in a thumb drive

(USB).
> And in some of my VBA programmes, also contain the drive path with the
> associated drive letters. (e.g. opening a file)
>
> As I add more USB devices to my PC, these drive letters keep changing such
> that the VBA programme can't read the correct drive letter where the files
> are stored.
>
> Any solution to fix the drive letter? Thanks.
>
> zhj23



 
Reply With Quote
 
=?Utf-8?B?emhqMjM=?=
Guest
Posts: n/a
 
      10th Jun 2007
Thanks for the various suggestions. I would look into them.

zhj23.

"zhj23" wrote:

> For mobility reasons, I store all my VBA programmes in a thumb drive (USB).
> And in some of my VBA programmes, also contain the drive path with the
> associated drive letters. (e.g. opening a file)
>
> As I add more USB devices to my PC, these drive letters keep changing such
> that the VBA programme can't read the correct drive letter where the files
> are stored.
>
> Any solution to fix the drive letter? Thanks.
>
> zhj23

 
Reply With Quote
 
=?Utf-8?B?dXJrZWM=?=
Guest
Posts: n/a
 
      10th Jun 2007

"zhj23" wrote:

> Thanks for the various suggestions. I would look into them.
>
> zhj23.
>
> "zhj23" wrote:
>
> > For mobility reasons, I store all my VBA programmes in a thumb drive (USB).
> > And in some of my VBA programmes, also contain the drive path with the
> > associated drive letters. (e.g. opening a file)
> >
> > As I add more USB devices to my PC, these drive letters keep changing such
> > that the VBA programme can't read the correct drive letter where the files
> > are stored.
> >
> > Any solution to fix the drive letter? Thanks.
> >
> > zhj23


Maybe you could also try naming your USB drives and then getting the drive
letter:

Sub getUSBDrive()

Set fso = CreateObject _
("Scripting.FileSystemObject")

For Each drive In fso.Drives

If drive.IsReady Then
If UCase(drive.VolumeName) = "MYUSBDRIVE" Then
Debug.Print drive.VolumeName, drive.DriveLetter
End If
End If

Next

End Sub


--
urkec
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Drive letter of System Drive =?Utf-8?B?RGVhbg==?= Windows XP Help 2 15th Mar 2007 08:51 PM
Changing drive letter of the boot/WinXP drive/partition dave Windows XP Help 29 10th Oct 2005 04:14 PM
Adding 2nd hard drive and changing drive letter names kp Windows XP General 8 22nd Nov 2004 02:05 PM
Replacing a Hard Drive or changing a boot drive letter Jim Windows XP Hardware 0 29th Aug 2003 06:49 AM
Changing drive letter of boot drive Sharon Windows XP General 0 6th Aug 2003 03:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:53 AM.