VB6 Common Dialog Question

I'm using a common dialog in a vb6 program and I need to change the location that I am storing as sPath.  How can I do that with this code...I don't want to open any files, I just want to change the location of my default path (sPath).  (Also, whenever I use this code, once I close it, reopens a file dialog window.

Here is the variable declaration in the page head:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1

Open in new window


Here is the procedure:
Private Sub cmdSetRaceFile_Click()
    'On Error GoTo ErrorHandler
    
    Dim strFileName As String

    Call Hourglass

    cdlChangePath.Filter = "Race Files (*.*)|*.*"
    cdlChangePath.InitDir = sPath
    cdlChangePath.ShowOpen
    
    strFileName = cdlChangePath.FileName
    
    ShellExecute hWnd, "open", strFileName, vbNullString, vbNullString, SW_SHOWNORMAL
    
    Call Arrow
    
    Exit Sub
ErrorHandler:
    Beep
    ErrorHandler

Open in new window

Bob SchneiderCo-OwnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Do you have sPath defined somewhere? If so before cmdSetRaceFile is clicked do

sPath = <my new path>

Or you  could simply to this in cmdSetRaceFile_Click

cdlChangePath.InitDir = <my new path>
Martin LissOlder than dirtCommented:
To find out what is causing it to open again, put a breakpoint on line 18 (by clicking in the left-hand margin of that row) and press f8 repeatedly to see where the code goes after that. Please see my Using the VB6 Debugger article for more information.
Bob SchneiderCo-OwnerAuthor Commented:
Thank you!  I think I am using a cdl that I use to open a file when what I really want is to find a way to change the sPath value to a different file.  In other words, I want sPath = <where the cdl ends up at>.  Does that make sense and, if so, how can I do that?

In summary:  I have a file path pre-set and held in the variable sPath.  In some cases I need to change the variable to another location/directory.  That is what I am after.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Martin LissOlder than dirtCommented:
The new code is in lines 13 to 20 and please see the comment at line 23.
   Private Sub cmdSetRaceFile_Click()
    'On Error GoTo ErrorHandler
    
    Dim strFileName As String

    Call Hourglass

    cdlChangePath.Filter = "Race Files (*.*)|*.*"
    cdlChangePath.InitDir = spath
    cdlChangePath.ShowOpen
    
    
    Dim intBackSlash As Integer
    With cdlChangePath
        intBackSlash = InStrRev(.FileName, "\")
        ' If you want the last \
        spath = Left$(.FileName, intBackSlash)
        ' if you don't
        spath = Left$(.FileName, intBackSlash - 1)
    End With

    
    ' If you don't want to actually do anything with the file then
    ' delete the next 3 lines

    strFileName = cdlChangePath.FileName
    
    ShellExecute hWnd, "open", strFileName, vbNullString, vbNullString, SW_SHOWNORMAL
    
    Call Arrow
    
    Exit Sub
ErrorHandler:
    Beep
    ErrorHandler
End Sub

Open in new window

Bob SchneiderCo-OwnerAuthor Commented:
I can make this work but it wants me to select a file when all I really want to do is select the path up to that folder.
Martin LissOlder than dirtCommented:
I'm sorry but I'm not sure what you mean, so let's ignore the code for now and please explain what it is that you want to do. I assume that there is a value in sPath.
Bob SchneiderCo-OwnerAuthor Commented:
sPath typically goes to the directory of the race I am timing (5K, marathon, triathlon, etc.).  Sometimes we want to access the data from a different location.  So, by default, sPath could be "c:\h51_software\CCMeet\meets\2019\Meet1 Name".  That is the directory that opens when I click on a command button.  What I want to do is change that to something like "c:\h51_software\CCMeet\meets\2019\Meet2 Name".

Does that make sense?
Martin LissOlder than dirtCommented:
By default this will open the cdl at the last path used and after that if you want to change the path, just navigate to that path, open a file and it will use that path the next time.

Private Sub cmdSetRaceFile_Click()

    With cdlChangePath
        .CancelError = True
        .Filter = "Race Files (*.*)|*.*"
        .FilterIndex = 1
        
        On Error Resume Next
        .CancelError = True
        .ShowOpen
        
        If Err.Number <> 0 Then
            MsgBox "No file was selected"
            Exit Sub
        End If
        On Error GoTo 0
    End With
End Sub

Open in new window

Bob SchneiderCo-OwnerAuthor Commented:
Super.  So final question.  How do I denote the new location as sRFIDPath
Martin LissOlder than dirtCommented:
Private Sub cmdSetRaceFile_Click()
Dim intBackSlash As Integer

    With cdlChangePath
        .CancelError = True
        .Filter = "Race Files (*.*)|*.*"
        .FilterIndex = 1
        
        On Error Resume Next
        .CancelError = True
        .ShowOpen
        
        If Err.Number <> 0 Then
            MsgBox "No file was selected"
            Exit Sub
        End If
        
        On Error GoTo 0
        
        intBackSlash = InStrRev(.FileName, "\")
        sRFIDPath = Left$(.FileName, intBackSlash)
    End With
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob SchneiderCo-OwnerAuthor Commented:
Thank you very much!
Martin LissOlder than dirtCommented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2018
              Experts Exchange Top Expert VBA 2018
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.