Solved

Rename file before upload using VBA in MS Access

Posted on 2010-11-12
3
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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 …

735 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