Solved

Rename file before upload using VBA in MS Access

Posted on 2010-11-12
3
468 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 48

Expert Comment

by:Dale Fye
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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