How to map a specific drive when open dialogs do not detect the previously selected path?

USING VB 6

My code.

CommonDialog1.DialogTitle = "Open File"
CommonDialog1.Filter = "Database (1.mdb) |1.mdb"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
CommonDialog1.CancelError = True
On Error Resume Next
CommonDialog1.ShowOpen
If Err Then
    MsgBox "Select Database"
    Exit Sub
End If

      

I am using open dialog in my project. When I run the project, I selected a file from the remote system.

Let's assume the remote system is unavailable. The next time I select the open dialog, the c-drive should be displayed in the dialog that opens

Now it displays my Project folder, it should display c drive

How do I write the code for this condition?

Requires VB6 Code Reference.

+2


source to share


2 answers


This will solve what you are asking:

To obtain FileSystemObject

, you must add a link to your project in the "Microsoft Scripting Runtime".



Dim fs As New FileSystemObject
Dim currentDir As String
currentDir = fs.GetParentFolderName(CommonDialog1.FileName)
If fs.FolderExists(currentDir) Then
    CommonDialog1.InitDir = currentDir
Else
    CommonDialog1.FileName = ""
    CommonDialog1.InitDir = "C:\"
End If

      

EDIT:
 You must also set CommonDialog1.FileName = ""

+2


source


The answer you are looking for is on the VBCity site The sample code there allows you to do exactly what you ask for.



0


source







All Articles