Solved Application.FileDialog. Problem with InitialFileName

zelarra821

Registered User.
Local time
Today, 09:24
Joined
Jan 14, 2019
Messages
835
I have created two FileDialogs, one to select a folder and another to select a file with a default extension. I have noticed that, in the name of the file, it puts the last folder of the path that I give it. This gives me an error when I want to select the route that I pass through the system. I have recorded a video for you:
 

Attachments

Why don't you post code?

Or not. I probably couldn't do anything with it because of language.
 
First off, apologies. The video has no audio because it was only intended to demonstrate the behavior. But I don't know, maybe I was wrong. In the video you can see how I open the dialog box, select the file name, select it and it gives an error. Then I show how in the code it takes the path, which coincidentally refers to the Files folder that appears in the dialog, which doesn't even exist.

Anyway, here's the code.

Code:
Function BuscaCarpeta(FNAme As Form, Optional RutaInicial As String, Optional RutaAntigua As String, _
        Optional Formulario As String, Optional Campo As String) As String
    On Error GoTo err_lbl
    Dim fDialog As Office.FileDialog
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
        With fDialog
            .AllowMultiSelect = False
            .ButtonName = "Seleccionar"
            .Title = NombreBD
            .InitialFileName = Ruta
            .InitialView = msoFileDialogViewDetails
            .Filters.Clear
            If .Show = True Then
                BuscaCarpeta = .SelectedItems(1)
                If RutaInicial = GetDBPath Then Exit Function
                If RutaInicial <> "" Then
                    CopiarArchivos RutaInicial, BuscaCarpeta
                End If
            Else
'                MsgBox "Has cancelado la selección de la carpeta.", vbInformation, NombreBD
                If Not RutaAntigua = "0" Then
                    BuscaCarpeta = RutaAntigua
                End If
                If Not Formulario = "" Then Forms(Formulario).Controls(Campo) = 0
                Select Case FNAme.ActiveControl.Name
                    Case "RutaCarpeta"
                        DespuesDeActualizarLaRutaDeCarpeta
                    Case "CopiaDeSeguridad"
                        DespuesDeActualizarCopiaDeSeguridad
                End Select
                Exit Function
             End If
        End With
Salida:
    Exit Function
err_lbl:
    MsgBox "BuscaCarpeta: " & Err.Number & " " & Err.Description, vbInformation, NombreBD
    Resume Salida
End Function
 
Code:
' ...
            .InitialFileName = Ruta
' ...

Where is variable Ruta declared and set?

Did you mean to use RutaInicial ?
 
I establish it with this function, based on a field of a table where I indicate if I want to change the path of the Files folder (where the favicon and logo are uploaded), or leave it in the same path of the database. In the function, there is another to choose the name of the folder, which in this case is called Archivos (Files).

Code:
Function Ruta(Optional CrearCarpeta As Boolean = False) As String
    On Error GoTo err_lbl
    Dim Carpeta As Object
    Set Carpeta = CreateObject("Scripting.FileSystemObject")
    Select Case DLookup("RutaCarpeta", "T00Configuracion")
        Case -1
            Ruta = DLookup("RutaCarpetaDonde", "T00Configuracion")
        Case 0
            Ruta = GetDBPath & "\" & NombreCarpetaArchivos
            If CrearCarpeta = True Then
                If Dir(Ruta, vbDirectory) = "" Then
                    Carpeta.CreateFolder (Ruta)
                End If
            End If
    End Select
Salida:
    Exit Function
err_lbl:
    MsgBox "Ruta: " & Err.Number & " " & Err.Description, vbInformation, NombreBD
    Resume Salida
End Function

Code:
Function NombreCarpetaArchivos() As String
    On Error GoTo err_lbl
    NombreCarpetaArchivos = "Archivos"
Salida:
    Exit Function
err_lbl:
    MsgBox "NombreCarpetaArchivos: " & Err.Number & " " & Err.Description, vbInformation, NombreBD
    Resume Salida
End Function
 
I'm only guessing at your code, but I suspect it might be a timing issue, whilst the new folder is created.

Try something like:
Code:
Function Ruta(Optional CrearCarpeta As Boolean = False) As String
' ...
                If Dir(Ruta, vbDirectory) = "" Then
                    Carpeta.CreateFolder (Ruta)
                    DoEvents
                    ' or use Sleep() function for a second or two
                End If
' ...
End Function
 
I tried to watch your video but got a codec not supported message.
Quick question - why not use Application.FileDialog (msoFileDialogFilePicker) instead?
 
I use folder because it is to choose a folder. I have another function where I use file, because it's for choosing files. I'll explain why. It is a Database Configuration form, where the user chooses the favicon (the icon he wants for the Windows taskbar), the logo (which will be used for reports and for the menu, with the purpose of last to decorate this one); the path of the file folder, to save the favicon and logo, and the files that you can upload in the database; activate the backup and choose the folder you want, and the number of backups to save; and choose the colors of the database and the text, both forms and reports. This is why I have one file and another folder. And the problem comes to me that I think it takes the Files folder, (Archivos) which does not exist. That is why I was asking if a file name could not be set, because I have looked in the Access help and I have not seen anything, or I have not known how to look at it. In any case, based on what they have told me, I am going to redo the code of this form, because I use functions and procedures within others, and the natural thing is to have each of them separated and call them as needed. In the end, trying to create a function or procedure that serves three or four different purposes is very difficult.
 
The solution is simple: you have to add "\" to the end of the path that you pass to it.
 

Users who are viewing this thread

Back
Top Bottom