Solved

Rename file before upload using VBA in MS Access

Posted on 2010-11-12
3
415 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 119

Assisted Solution

by:Rey Obrero
Rey Obrero 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
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 …

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now