It is very easy to write simple code to utilise all these three controls. The following code integrates the three. When you change the drive it updates the file and directory list, and when you change the folder it lists the files it contains.
' This event occurs when a new drive is selected
Sub Drive1_Change()
' change the folder path to the new drive
Dir1.Path = Drive1.Drive
' change the file path
File1.Path = Drive1.Drive
End Sub
' This event occurs when a new directory is selected
Sub Dir1_Change()
' change the file path
File1.Path = Dir1.Path
End Sub
As you can see, this requires very little code. However one thing you need to take into account is that sometimes the user will select the floppy drive, forgetting to put in a floppy disk. With the code above, your application would crash. To allow for these events, you need to write a bit more code. The following code does the same above, just does not crash when an invalid drive or folder is selected.
Public OldDrive As String
' This event occurs when a new drive is selected
Sub Drive1_Change()
' Trap an error if it occurs
On Error GoTo driveerr
' change the folder path to the new drive
Dir1.Path = Drive1.Drive
' change the file path
File1.Path = Drive1.Drive
' Save the new drive name
OldDrive = Drive1.Drive
'Exit procedure, no error has occured
Exit Sub
driveerr:
' error handling procedure
Select Case Err
Case 68
MsgBox
"Drive not accessable"
'
Switch back to the previosu drive
Drive1.Drive
= OldDrive
Exit
Sub
Case Else
MsgBox
"Unexpected Error." & Err & " : " &
Error
Drive1.Drive
= OldDrive
Exit
Sub
End Select
End Sub
' This event occurs when a new directory is selected
Sub Dir1_Change()
' change the file path
File1.Path = Dir1.Path
End Sub
Private Sub Form_Load()
' Save the drive name
OldDrive = Drive1.Drive
End Sub
Comments