Solved Browsing for file code issues

5hadow

Member
Local time
Today, 16:19
Joined
Apr 26, 2021
Messages
89
Hello,

Is there something obvious why my code below does not work?

Code:
Private Sub btnBrowse_Click()
  
    ' File targets
    Dim sourceFile, targetFile, target As String
  
    ' Error Location
    On Error GoTo Error_btnBrowse_Click

    ' Check to see if its an add or remove
    If btnBrowse.Caption = "Browse" Then
      
        With Application.FileDialog(3)
      
            .Title = "Choose WI File Location"
            .InitialFileName = DLookup("fileBaseOutput", "tblConfig")
            .AllowMultiSelect = False
          
            ' Show the box
            If .show = -1 Then
              
                ' Get the name
                sourceFile = .SelectedItems(1)
                  
                target = Right(sourceFile, Len(sourceFile) - InStrRev(sourceFile, "\"))
                targetFile = DLookup("fileBaseOutput", "tblConfig") + "\" + target
              
                If Not FileExist(targetFile) Then
                MoveFile sourceFile, targetFile
                End If
              
                ' Set the old data to nothing
                txtLocation.Value = target
                btnBrowse.Caption = "Remove"
          
            End If
          
        End With
  
    ElseIf btnBrowse.Caption = "Remove" Then
  
        ' Set the temporary name for the file
        Dim tempFileName As String
        tempFileName = txtLocation.Value
      
        ' Rename the button
        btnBrowse.Caption = "Browse"
      
        ' Requery to get the data
        txtLocation.Value = ""
      
        sourceFile = DLookup("fileNotices", "tblConfig") + "\" + tempFileName
        targetDir = Application.CurrentProject.Path + "\Removed"
        targetFile = targetDir + "\" + tempFileName
      
        ' Make a removed directory
        If Not FolderExists(targetDir) Then
            MkDir targetDir
        End If
      
        ' Delete the file in removed
        If FileExists(targetFile) Then
            Kill targetFile
        End If
      
        ' Move the file
        MoveFile sourceFile, targetFile
      
    End If
  
    ' Requery
    DoCmd.RunCommand acCmdSaveRecord
  
    ' Get Out
    Exit Sub


I get "Compile Error" Sub or function not defined

1622654450237.png
 
Last edited:
You need some function called FileExists. Can't call it if it doesn't exist!
 
You need some function called FileExists. Can't call it if it doesn't exist!
That makes sense. I'm looking trough this inherited code to see if it's defined somewhere else.
 
Can do Ctrl+F to Find Function FileExists text
 
The code in post #1 actually says FileExist without an S at the end. Should it actually say FileExists?
 
Personally I would have a public function to browse for the file which would clean out a lot of what you have. I might also consider using fileSystemObject .

One thing that stands out is that you are using a + sign to concatenate. If there is a null in the string then the resultant string will be null. Use the & to concatenate.
 
I'm going to go with your idea due to the FolderExists
 

Users who are viewing this thread

Back
Top Bottom