Automatically populate an Access database from a script - ms-access

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

Related

SSIS package configurations

how to load multiple Excel files using SSIS. I have had Packages in the past where I was looping through multiple Text files in a folder and loading them into SQL server tables.
Create a variable named FileName, set the scope to ImportMultipleExcelFiles, Data type is String.
Add a Foreach Loop Container in the Control Flow Task.
Edit the Foreach Loop Container, in the Collection section change the Enumerator value to Foreach File Enumerator
You need to change the Enumerator configuration as shown below :
Folder: Provide a complete folder path location where all our Excel
source files are stored.
Files: you need to read the Excel files from our source folder, so
enter *.xls in the Files section, this will make sure our SSIS
package will read all available .xls files from the source folder.
Here * indicates that the Excel file name can be anything, but file
extension will be .xls. If we need to read data from a specific Excel
file name then we have to configure it accordingly.
Retrieve File Name: select the Fully Qualified radio button.
Then, create variable mappings for the Foreach Loop container, select the "User::FileName" variable and set the Index value to 0 in the Variable Mappings section.
Add a Data Flow Task inside the Foreach Loop Container.
Right click on the recently added Data Flow task and click on Properties and mark the DelayValidation property to True.
Add an Excel Source in the Data Flow Task and create a new connection to any of the Excel source files.
You have to make the Excel connection dynamic so that it can connect to each Excel file in the source folder. To make the Excel source connection dynamic, right click on Excel Source Connection and then click on Properties.
Expand the Expression Properties, then select the Connection String property and then click on the expression icon, in the expression window :
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+#[User::FileName]+";Extended Properties=\"Excel 8.0;HDR=YES\";"
Finally, execute the SSIS package and see the result.

I want to export from Access to Excel a Table I created in Access without Access asking me if I want to overwrite the existing file

Is it possible without VBA? I can send the file the first time with no problem. I can manually go in and delete the file then run Access - no problems. I want Access to run and then overwrite the existing Excel file without asking me if I want it overwritten.
I do not think is possible without VBA.
Delete the file before exporting.
Dim strFile As String
strFile = "your filepath here"
If Dir(strFile) <> "" Then Kill(strFile)
'export code here

ssis renaming a file wthout knowing existing name

I have set up part of my ssis package to check a folder and move the csv file to another folder using a file system task.
Is it possible to rename a file without knowing what the source name will be? as I have been told it will be a new name on a daily bases. can I use a wildcard in a file system task? would I be best renaming before moving what is best practise?
For an unknown file name you can wrap your file system task in a foreach loop
wrap your file system task in a foreach loop and set to file enumerate
set the folder to your source location
set search string to *.csv
set to full file path
map to a variable called fname
use fname as variable in file system task for source
Make sure you delay validation on connection manager

Sourcefile name change in SSIS Package

I have created SSIS packages which imports Excel files from a folder. The name of the Excel file changes based on months.
e.g.: I:\Test\User_09-05-2016.xlsx or
I:\Test\User_09-06-2016.xlsx etc.....
I want to create SQL jobs to run the packages because I will get a new file every month.
I want to archive the excel file after successful package execution(to keep only one excel file in folder at a time).
How can I automate the process?
Just use a ForEach container within SSIS.
Import each spreadsheet within that folder.
Delete (or move) each spreadsheet after processing.
Alternative approach to FE loop - define path and filename of Excel file on the package run.
If you know rules for naming Excel files, create a string variable Excel_FilePath containing complete path with Expression like [User::Folder Path]+"\\"+[User::Filename]+".xlsx". Then take your Excel file Connection Manager and add the following expression to the ConnectionString property "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + #[User::Excel_FilePath] + ";Extended Properties=\"Excel 8.0;HDR=YES\";"
In Package properties - set DelayValidation=true.
More details on this approach with screenshots and exactly your question reviewed.

Creating a Document Database using Microsoft 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.