Solved Browsing for file code issues (1 Viewer)

5hadow

Member
Local time
Today, 02:46
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:

Isaac

Lifelong Learner
Local time
Yesterday, 23:46
Joined
Mar 14, 2017
Messages
8,738
You need some function called FileExists. Can't call it if it doesn't exist!
 

5hadow

Member
Local time
Today, 02:46
Joined
Apr 26, 2021
Messages
89
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.
 

Isaac

Lifelong Learner
Local time
Yesterday, 23:46
Joined
Mar 14, 2017
Messages
8,738
Can do Ctrl+F to Find Function FileExists text
 

isladogs

MVP / VIP
Local time
Today, 06:46
Joined
Jan 14, 2017
Messages
18,186
The code in post #1 actually says FileExist without an S at the end. Should it actually say FileExists?
 

moke123

AWF VIP
Local time
Today, 02:46
Joined
Jan 11, 2013
Messages
3,852
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:46
Joined
Sep 21, 2011
Messages
14,048
I'm going to go with your idea due to the FolderExists
 

Users who are viewing this thread

Top Bottom