Solved

Rename file before upload using VBA in MS Access

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

914 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

14 Experts available now in Live!

Get 1:1 Help Now