在复制的文件

时间:2017-07-27 18:40:10

标签: ms-access-2013

我有一个代码可以将文件从一个位置复制到另一个位置。我希望在复制文件时,将recordID放在文件名前面(例如:150-FirstName)。这是我正在使用的代码:

Private Sub cmd_LocateFile_Click()
On Error GoTo Error_Handler
Dim sFile                 As String
Dim sFolder               As String

sFile = FSBrowse("", msoFileDialogFilePicker, "All Files (*.*),*.*")
If sFile <> "" Then
    sFolder = Application.CodeProject.path & "\" & sAttachmentFolderName & "\"
    If FolderExist(sFolder) = False Then MkDir (sFolder)
    If CopyFile(sFile, sFolder & GetFileName(sFile)) = True Then
        Me.FullFileName = sFolder & GetFileName(sFile)
    Else
    End If
End If

Error_Handler_Exit:
On Error Resume Next
Exit Sub

Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
       "Error Number: " & Err.Number & vbCrLf & _
       "Error Source: " & sModName & "\cmd_LocateFile_Click" & vbCrLf & _
       "Error Description: " & Err.Description & _
       Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
       , vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub

1 个答案:

答案 0 :(得分:0)

尝试:

Dim Id As Long
Dim sTarget As String

Id = YourRecordID  ' Set current record id.
sTarget = sFolder & CStr(Id) & "-" & GetFileName(sFile)

' Replace your current If-Then-Else-End If block.
If CopyFile(sFile, sTarget)) = True Then
    Me!FullFileName.Value = sTarget
End If