Solved

Rename file before upload using VBA in MS Access

Posted on 2010-11-12
3
446 Views
Last Modified: 2012-05-10
In short, after creating a new record, I have a process in which the user can add attachments to that record (the attachments are saved in the server and not the database as object).
I would like to insert a process right before the upload that will rename the file selected and use the number of the record as new name without changing the file extension.
Example: If I perform the upload from Record # 123 then if I select file from: Drive:\whatever\whatever\test. Whatever
Then, before the upload the file name will be changed to: Drive:\whatever\whatever\123.whatever
As in this example, everything stays the same except for the file name which has changed from test to 123

Thank you,
Silvio

strInputFilePath = ahtCommonFileOpenSave( _
                OpenFile:=True, _
                DialogTitle:="Please select an input file...", _
                Flags:=ahtOFN_HIDEREADONLY)

  strFilename = Dir(strInputFilePath)
  

---the file re-name process need to be here---
  
  
  strNewFilePath = "\\hmcapp1\Data\WQDBASES\wqd\Attach\" & Me.Site

  Me.txtWhatFile = strNewFilePath

If FileExists("\\hmcapp1\Data\WQDBASES\wqd\Attach\" & Me.Site & "\" & strFilename) Then
    If MsgBox(strFilename & " already exists." & vbCrLf & _
      "Would you like to replace it?", vbYesNo + vbQuestion, "Attention!") = vbYes Then
        GoTo Overwrite
      Else
        Exit Sub
    End If
End If

Open in new window

0
Comment
Question by:Cobra967
3 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 350 total points
ID: 34121643
How about adding something like this

2 new variables

Dim sOldFile As String
Dim sNewFile As String


Then set sOldFile (instead of hardcoding path in FileExists) then use that. Remember to change RECNUMBER to what you want



sOldFile = "\\hmcapp1\Data\WQDBASES\wqd\Attach\" & Me.Site & "\" & strfilename

'if file exists
If Dir$(sOldFile) <> "" Then
    'Create copy replacing prefix of file with RECNUMBER (you change this to whatever, this is just an example)
    sNewFile = "\\hmcapp1\Data\WQDBASES\wqd\Attach\" & Me.Site & "\" & RECNUMBER & Mid$(strfilename, InStr(1, strfilename, "."))
    FileCopy sOldFile, sNewFile
End If
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 150 total points
ID: 34121807


you can use the command Name

Name OldFile as NewFile
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 34122028
what if they want to upload more than one file for a record?

When I do this, I generally have a 1-to-many table (tbl_RecordFiles) which contains an ID field (autonumber), the RecordNumber, The original file name, and a notes field.  I use the ID field as the numeric value, and generally copy these files to a location other than the original folder with the new file name.  I rarely change the name of the original file, or move it from the original location.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question