Creating a Document Database using Microsoft Access - ms-access

I am attempting to create a table within a database which store all of the documents related to the database "in it". What I really want to do is have a file uploaded and have vba code which copies the file to a network location, renames the file by concatenating two fields from the document table form (eliminating the issue of duplicate file names in the external location), and then stores the file name and file path in a file path field in the table. I am very new to access and vba so I am having difficulty getting everything to work. The code I currently have is below:
Option Compare Database
Private Sub Command15_Click()
Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = False
If f.Show Then
For i = 1 To f.SelectedItems.Count
sFile = Filename(f.SelectedItems(i), sPath)
MsgBox sPath & "---" & sFile
Next
End If
End Sub
Public Function Filename(ByVal strPath As String, sPath) As String
sPath = Left(strPath, InStrRev(strPath, "\"))
Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
End Function
I can not seem to get a handle on how to move, rename by concatenating the two fields from the form, or store the path in the path field of the table. I have been to the following locations to obtain what information I could
ms access browse for file and get file name and path
VBA to copy a file from one directory to another
I am currently using Microsoft Access 2010, and I do not wish to use the file attachment field type because of database size constraints. Currently I press a button and a file explorer appears to navigate to the file being uploaded, and the path and file name are entered into strings. After this point I am lost. If any other information is needed please let me know. Thanks in advance for the assistance.

I believe your approach to managing the documents is right. In most cases, it doesn't make much sense to store documents in the database itself when the filesystem is a more suited to this job.
What you are doing is fairly straightforward but the main complexity will come from the correct management of the various paths and filenames and extracting the right information from them.
It can become tricky if you're not using some helper functions to to dissect and recompose the various bits of the paths.
I have created a sample database that has a few functions. Might not be exactly in line with what you need but you can easily play around with it to suit your particular case.
The sample database includes a Tools VBA module that has a few useful functions to split a Path into its constituents.
Basically, the database has 2 forms.
The main form allows you to set the network path where the files are to be saved.
You can then select a pre-defined Account number (listed in the Account table) associated with a document, then click the upload button.
This creates a new record in the Document table and opens a form where you can edit the document title and click a button to upload a file to the server.
The file selected by the user is copied to the server after its path has been transformed.
I took the assumption that the file would keep its original extension, the filename would be renamed to the ID of the Document record where the file information is saved (like 5845.pdf) and that the folder where the file is saved on the server would be the account number, so that a source file selected by the user
C:\Users\user\Desktop\SuperSecretFile.pdf
would be saved as, for instance:
\\docserver\files\123-55547\5845.pdf
The Main form also allows you to update an existing record, open the file from the server, open the server's folder where the file is located or even copy the server file back to the user's computer with the original name of the file.
I'll let you play around with it. Let me know if you have any issues.

Related

How can I save two files with the same name inside the same folder, without renaming any using php?

How can I save two files with the same file name in the same folder without renaming anyone using php?
For instance: A user has an audio file name "first.mp3"; and another user uploads another file named: "first.mp3"; and I want to save these two files without renaming any so that when people are downloading the audio from the front end, the name does not change.
I can do this by concatenating a random number to differentiate the files but I want to beat this method of renaming.
Should I be saving each file inside a unique folder and save the file names to database? but this method will create too many folders which i don't think it is appropriate.
You cannot have two files with the same name in the same folder.
You would either have to add a random string to the end of each file like you suggest or save each user's files in a directory allocated to their account.
Regards,
Leslie
Saving multiple files with the same name within the same folder is just not possible.
I'd opt for a strategy that would involve saving the original filename somewhere (in a database, for example) along with the name/path of the actual file. When the User downloads the file (presumably through a web app of some sorts), you can set the name of the file via headers with your language of choice.
You could even rename the files to something completely random when they're uploaded so you can have them all in one folder - as long as you store the original filename somewhere, you can always set it before you serve it back to the end user.

How can I automatically link an Access front-end file to a back-end?

I have a pair of Access files, a front end and a back end, that I share with several users and update frequently. I'm looking for a way to automatically link the two files so my users don't need to use the Linked Table Manager. I suspect that a solution can be created with VBA. I reviewed a few possibilities, here and here, but they don't apply to my specific scenario, below.
The two files always exist in the same user directory.
These are the only two Access files in each directory.
The directories are located on the users' local C: drives (in a Dropbox folder).
The names of the two files change slightly with each version/build update.
Example front-end name: "DigExams_0.10.f005.u101.accdb"
Example back-end name: "DigExams_backend_0.10.b001.u101.accdb"
Naming convention: "Filename*_major#.minor#.build#.user#.accdb"
[ * The back end file also includes the word "backend". ]
I was thinking that some code could be embedded into a button on the front end that would (OnClick) run the Linked Table Manager, look for the only other Access file in the directory (or the file with "backend" in the name), and then link all of the tables to it. If there was a conflict, such as a third Access file in the directory, an error would appear.
Advice? Suggestions? Thanks!
The target file is stored in the Connect property of the TableDef object.
You will need to replace the releveant part of the Connect property with the new file name.
Dim tdf as TableDef
For each tdf in CurrentDB.TablDefs
StringToBeParsed =tdf.Connect
.... 'here goes the code to replace the connection string with new target file
Next

SSIS For Each Loop crashes the flat file connection

I created a simple SSIS package to import a flat file (.txt) into a database table. Tested that and it works perfectly. Since I have several files to import, I added a foreach loop to go through all the files, added the variables as recommended in several examples found on the net but now my flat file connection manager returns an error of "A valid file name must be selected." and the package will not run. I have so far been unsuccessful in finding the solution for this issue and would appreciate any suggestions by the SSIS gurus of this forum. Many thanks in advance!
Here is what I have in the way of variables:
SourceFileFolder which is the path to the folder that contains the files
FileName a string containing one of the names of the files I am seeking to import
SourceFilePath which is an expression driven variable that incorporates the previous two variables concatenated together. I can click "Evaluate Expression" and copy and paste it into windows explorer and open the file
ArchivePath which is an expression driven variable that creates the path to archive the file to once it is processed.
As the message says this is related to your connection manager not gathering the connection string. This can be handled using the following:
First of all clear the expression on the SourceFilePath variable.
With your Foreach Loop Container, set it up as follows:
This will use your variable SourceFileFolder as the Folder, you could also just hardcode the folder name C:\ for instance. Also make sure your folder is qualified correctly, I.E. make sure it finishes with a slash C: won't work but C:\ will work.
Next you need to map the fully qualified name to your other variable SourceFilePath
This should now store the full name of the file the loop has found into the SourceFilePath variable. For Instance C:\File.txt, you can now use this as a connection string expression on your file connection manager.
Under the properties of the connection manager make sure the expression is set to ConnectionString and then use the SourceFileName variable.
ALSO MAKE SURE DELAY VALIDATION IS SET TO TRUE
This hopefully should mean you can loop through the files.

Automatically populate an Access database from a script

I have a script that downloads data from a database into a series of CSV files. After they're downloaded, they must be loaded into an Access database for reporting (I use DoCmd.TransferText, and have a saved text import specification). Every time I run the job that generates the data and downloads into CSV, I usually need to load into a fresh copy of the unpopulated version of the Access database. Is there a way to automate this in a batch script?
In short, I need to be able to:
copy the unpopulated Access file to a new file with the timestamp in the name
load certain CSV files that match a pattern (such as "data_for_reporting_2_20111024_135142.csv") in the directory into the Access file.
I think you can use VBScript to do what you need.
copy the unpopulated Access file to a new file with the timestamp in
the name
FileSystemObject.CopyFile "c:\somefolder\template.mdb", "c:\dest\new.mdb"
See CopyFile Method.
load certain CSV files that match a pattern (such as
"data_for_reporting_2_20111024_135142.csv") in the directory into the
Access file.
You can examine the Files Collection of your CSV folder, determine which of those file names match your target pattern, then run DoCmd.TransferText with each matching file name.
You would run DoCmd.TransferText from an Access application instance:
Option Explicit
Dim appAccess
Dim strMdb
Const cstrFolder = "c:\dest\"
strMdb = "new.mdb"
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase cstrFolder & strMdb, False
So, do the Transfertext from that instance variable:
appAccess.DoCmd.TransferText [your options]
Edit: This would be faster for me to create and test in VBA. So I think I would use that instead of VBScript.
Create a function, SnarfCSV, in a standard module in your template MDB. Then create a macro, mcrSnarfCSV, with the SnarfCSV function as its runcode action. Then after you copy the template MDB to the new MDB, open the new one with the /x command line switch to run the macro.
"Path to MSACCESS.EXE" "Path to your db file" /x mcrSnarfCSV

Storing PDFs in MS Access Database using Forms

I need to store PDF files in an Access database on a shared drive using a form. I figured out how to do this in tables (using the OLE Object field, then just drag-and-drop) but I would like to do this on a Form that has a Save button. Clicking the save button would store the file (not just a link) in the database. Any ideas on how to do this?
EDIT:
I am using Access 2003, and the DB will be stored on a share drive, so I'm not sure linking to the files will solve the problem.
We have several databases that contain 10's of thousands of documents (pdf, doc, jpg, ...), no problem at all. In Access, we use the following code to upload a binary object to a binary field:
Function LoadFileFromDisk(Bestand, Optional FileName As String = "")
Dim imgByte() As Byte
If FileName = "" Then FileName = strFileName
Open FileName For Binary Lock Read As #1
ReDim imgByte(1 To LOF(1))
Get #1, , imgByte
Close #1
If Not IsEmpty(imgByte) Then Bestand.Value = imgByte
End Function
In this case, Bestand is the field that contains the binary data.
We use MS SQL Server as a backend, but the same should work on an Access backend.
If you used the same concept but upsized to SQL Server- storing PDFs inside of an Image datatype (or varbinary(max)) then you could SEARCH INSIDE THE PDFs using Full Text Search.
I show that Microsoft says you can do this for any file type where you can register an IFILTER product.. and I just was at the Adobe website the other day and say that their Acrobat IFILTER is indeed FREE.
Maybe this will help: ACC2000: Reading, Storing, and Writing Binary Large Objects (BLOBs).
What they do: Read a file in chunks and add it to a blob using a VBA function.
A field of OLE Object, by default would use a Bound Object Frame on the form. Right click on it and you can Insert an object. It comes complete with browsing for the file. Double-click on the field and the actual document will open.
I recommend going with David's advice and link. Unless you have a need to transfer a single file and want all the PDF's included. Size and performance will be an issue.
If security is an issue and the Access file is the only control you have (You are unable to set security on the folder containing all the linked files.), then you would have to embed.