Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Copy File and store new link
Chris Pardy 
   
4 years ago
Hi, I've tried to create a VBA script for attaching Files to note entries.. I can select a file and get the link no problem, but I need to take it one step further and copy the file into the current project path, and then store the link.

Im getting Error 2465 "Microsoft Access can't find the field '|1' referred to in your expression. Ive checked the string values I am getting for Source and Destination and I think they are ok.. but I don't really understand the CopyFile function and ive just found that code posted elsewhere.

The line of code highlighted in the debug is:

fso.CopyFile Source, Destination, [overwrite]

Here is my code.. Referenced functions are below.

Private Sub btnAddFile_Click()

Dim fso As Object
Dim Source As String
Dim Destination As String
Dim FN As String

    Set fso = VBA.CreateObject("Scripting.FileSystemObject")
    
    Source = PickFile()
    
    FN = FileNameFromPath(Source)
        
    Destination = CurrentProject.Path & "\Note Attachments\" & Format(Date, "yyyy") & "\" & Format(Date, "mmmm") & "\"
    
        If Len(Dir(Destination, vbDirectory)) = 0 Then
        myMakeDir Destination
        End If
    
    fso.CopyFile Source, Destination, [overwrite]
    
    NoteLinkAdd = Destination & FN
    
    Set fso = Nothing

End Sub

from debug.print of the variables

Source = \\olddept.isi.aplace.comm\Win10_Migration\Users\myname\Desktop\myfile.pdf

Destination = C:\Local Docs\Note Attachments\2022\October\



Public Function PickFile() As String

    Dim FO As Object
    Set FO = Application.FileDialog(3)
    FO.Show
    PickFile = FO.SelectedItems(1)
    
End Function


Public Sub myMakeDir(strFolderName As String)

On Error Resume Next

Dim a, t As String, i As Integer

a = Split(strFolderName, "\")
t = a(0)

For i = 1 To UBound(a)
  
  t = t & "\" & a(i)
  MkDir t

Next

End Sub
Kevin Robertson  @Reply  
          
4 years ago
Hi Chris,

Couple of things I notice:

1. Your destination is missing the filename.
2. The line fso.CopyFile Source, Destination, [overwrite]
        [overwrite] should be a Boolean value (True/False)
        fso.CopyFile Source, Destination, True
Kevin Yip  @Reply  
     
4 years ago
What is the value of [overwrite]?  Enclosed in square brackets, it is treated as a field, and that may be why you get the "can't find field" error.  Try:

fso.CopyFile Source, Destination, True

or

fso.CopyFile Source, Destination, False

"Overwrite" is an optional argument for CopyFile, and it should be a boolean value.  It tells Access whether to overwrite an existing file or not.  It is optional, so it can be omitted.  If omitted, its default value is True.

CopyFile has quite a few caveats, so read all about them here:
ttps://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/copyfile-method
Chris Pardy OP  @Reply  
   
4 years ago
Hi Kevin And Kevin... the Boolean was the issue.. thanks for pointing that out. turns out unless you want to change the file name the destination does not need a filename specified.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/7/2026 7:06:52 AM. PLT: 1s