Avatar of Bob Schneider
Bob SchneiderFlag for United States of America

asked on 

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

Visual Basic Classic

Avatar of undefined
Last Comment
Martin Liss
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

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>
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

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.
Avatar of Bob Schneider

ASKER

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.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

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

Avatar of Bob Schneider

ASKER

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.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

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.
Avatar of Bob Schneider

ASKER

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?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

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

Avatar of Bob Schneider
Bob Schneider
Flag of United States of America image

ASKER

Super.  So final question.  How do I denote the new location as sRFIDPath
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bob Schneider
Bob Schneider
Flag of United States of America image

ASKER

Thank you very much!
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

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
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo