automatically convert access db - ms-access

I have an access database, with a query made. I need to automate it so that each night this query can run and export to a tab delimited csv file. It is not possible to export a query to a csv file from within access. My question is, are there any tools that can select certain tables, or perform an sql query on an mdb file, and export to a csv file?

Actually, you can export a query to a csv file from within Access.
You can do this with a Macro using the TransferText method.
Macro:
Name = ExportQuery
Action = TransferText
Transfer Type = Export Delimited
Table Name = [name of your Access query]
File Name = [path of output file]
Has Field Names = [Yes or No, as desired]
You can execute the macro from the command line like this:
"[your MS Office path]\msaccess.exe" [your databse].mdb /excl /X ExportQuery /runtime
Since you're having trouble with TransferText in a macro try this:
1) Create a Module named "ExportQuery". In this module, create a function called "ExportQuery":
Function ExportQuery()
DoCmd.TransferText acExportDelim, , "[your query]", "[output file].csv"
End Function
2) Create a Macro named RunExportQuery:
Action = RunCode
Function Name = ExportQuery ()

VBScript works quite well with the Jet engine. However, I do not see why you say " It is not possible to export a query to a csv file from within access."
Sub TransferCSV()
DoCmd.TransferText acExportDelim, , "PutNameOfQueryHere", "C:\PutPathAnd\FilenameHere.csv", True
End Sub
Is the usual way in VBA.
EDIT:
It is possible to run a VBScript file (.vbs) from the command line. Here is some sample VBScript to output a tab delimited file.
db = "C:\Docs\LTD.mdb"
TextExportFile = "C:\Docs\Exp.txt"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source =" & db
strSQL = "SELECT * FROM tblMembers"
rs.Open strSQL, cn, 3, 3
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateTextFile(TextExportFile, True)
a = rs.GetString
f.WriteLine a
f.Close

SQL Server Integration Services is able to do the transformation that you are talking about. Don't be fooled by the name, because you don't need SQL Server in order to automate and run the packages.
http://msdn.microsoft.com/en-us/library/ms141026.aspx

Related

import multiple CSV file into Access database and save them into different tables based on the file name

I've got about 100 CSV files that I'm trying to import them into Access and then rename the tables based on the file names.
Here is the code I've found but the "tablename" should be my file name. however, I can't get it to work as I'm new to scripting.
Function Import_multi_csv()
Dim fs, fldr, fls, fl
Set fs = CreateObject("Scripting.FileSystemObject")
Set fldr = fs.getfolder("D:Files\")
Set fls = fldr.files
For Each fl In fls
If Right(fl.Name, 4) = ".csv" Then
DoCmd.TransferText acImportDelim, , "TableName", "D:Files\" & fl.Name, False
End If
Next fl
End Function
Also, I have three columns in my files and I want the third column to be imported as a double.
Any help will be appreciated.
It should be this:
DoCmd.TransferText acImportDelim, , "[" & fs.GetBaseName(fl.Name) & "]", "D:Files\" & fl.Name, False
As for your second question, you could create, save, and use an import specification.

need to dynamically insert attachment to an access form from a local system and save the address into a table

Hey i am new to access database.
I am creating a form in which i need to attach a excel file from the local system. I tried to use the attachment control to attach the document. But i am not able to store it into a table. I need to use that excel document for my further processing. I need to get the path from which the data is selected from my local system.
I hard coded the path and i was able to do my operation but now i need to dynamically fetch the data from the location.
thanks in advance
My code for hard coding looks like this
Private Sub Command4_Click()
Dim dbs As DAO.Database
Set dbs = CurrentDb
If (ifTableExists("featuretable") = True) Then
dbs.Execute "Delete * from featuretable"
End If
Dim filepath As String
filepath = "C:\Users\jolly#iese.fhg.de\Desktop\featurevalues.xlsx"**
DoCmd.TransferSpreadsheet acImport, , "featuretable", filepath, True
fmfeaturesubform.Form.Requery
End Sub
"Attach" and "import" are completely different things. i guess you want to import the excel sheet.
one way would be use the Application.FileDialog:
http://msdn.microsoft.com/en-us/library/office/ff196794(v=office.15).aspx
another way would be search your current folder and import matching filenames:
Dim mBaseFolder As String
Dim mFname as string
mBaseFolder = "C:\test\" ' or application.CurrentProject.Path
mFname = Dir(mBaseFolder & "*.xls")
Do While fname <> ""
DoCmd.TransferSpreadsheet acImport, , "featuretable", mFname , True
mFname = dir()
Loop

VBA calling an access query to fill Excel File

Run-time error '3190'
Too many fields defined
I recieve this error when I click on a button that calls queries in a DoCmd.TransferSpreadsheet:
'------------------------------------------------------------
' Creates the excel file in the path passed to it
' Returns the path of the file including the file name and extension
'------------------------------------------------------------
Function CreateExcelFile(Path As String) As String
Dim outputFileName As String
outputFileName = Path & "SummaryTemplate.xlsx"
Dim Queries(1 To 4) As String
Queries(1) = "qryProcessAuditScores" 'Audit scores
Queries(2) = "qryProcessAuditStations" 'Audit Stations
Queries(3) = "qryProcessNCs" 'Number of NC's
Queries(4) = "qryProcessAuditCount" 'number of audits from the year
Dim qry
For Each qry In Queries
DoCmd.TransferSpreadsheet _
acExport, _
acSpreadsheetTypeExcel12, _
qry, _
outputFileName, _
True
Next
CreateExcelFile = outputFileName 'return the full path
End Function
When I run the queries by hand in Access I recieve no such error. I have tried Compact and Repair Database with no luck. Any other Ideas?
Thanks
I ended up running sql commands to put the queries into tables, ran the TransferSpreadsheet with those tables, and then ran sql commands to drop the tables.

Updating the data in an ODBC datasource with that in local Access DB?

I have a remote ODBC data source 'A' whose values is to be updated according to the table 'B' in the local access database. How can I do the same ?. I tried using pass through queries, however I am not able to access both the remote and local source in ONE SINGLE query. How should I do the same?
How does link tables work? Can I link my local table to the ODBC database dynamically using VBA?
In your Access database simply create a Linked Table for your ODBC data source:
For detailed instructions, see
About importing and linking data and database objects
Once that is done, you can use the linked table and the local table(s) in the same query from within Access:
You can't create a link dynamically that I am aware, though you could refresh a link that already existed.
What sort of joining is required? If you're just updating a single or a few rows, you might do this: (I can only write this using ado (means adding a reference to microsoft.activex data objects)
dim ss as string 'tempstr, sqlstr whatever you want to call it
dim cn as object: set cn = createobject("adodb.connection")
cn.Connection = [connection string required for ODBC datasource]
cn.Open
dim rst as object: set rst = createobject("adodb.recordset")
rst.open "SELECT required_data_column_list FROM localTable [WHERE ...]" _
, currentproject.connection, adOpenStatic, adLockReadOnly
do while not rst.eof
ss = "UPDATE ODBC_TableName SET ColumnA = '" & rst.Fields(3) & "' [, ... ]
ss = ss & " WHERE ... "
cn.Execute ss
do while cn.State = adStateExecuting
loop
rst.movenext
loop
set rst = nothing 'these statements free up memory,
set cn = nothing 'given that these objects are unmanaged
Hope this helps

VBA procedure to import csv file into access

i need procedure in VBA to import data into access from csv excel file without some records,, as header and footer. Example,,, i have table in csv file, which contains some
sentence which not belong table date
A1 this is some sentence title.......
A2 title
A3.......
A7 DATA DATA DATA DATA DATA
A8 rows DATA DATA DATA DATA DATA
......
....
A256 DATA DATA DATA DATA
A257 this is some sentence
My Acess shoud contain only rows between A7 to A256. Does anyone knows procedure or whatever in VBA who solves my problems ?
thanks a lot
Edit
The easiest way to do it is to link the CSV-file into the Access database as a table. Then you can work on this table as if it was an ordinary access table, for instance by creating an appropriate query based on this table that returns exactly what you want.
You can link the table either manually or with VBA like this
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tblImport", _
FileName:="C:\MyData.csv", HasFieldNames:=true
Update
Dim db As DAO.Database
' Re-link the CSV Table
Set db = CurrentDb
On Error Resume Next: db.TableDefs.Delete "tblImport": On Error GoTo 0
db.TableDefs.Refresh
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tblImport", _
FileName:="C:\MyData.csv", HasFieldNames:=true
db.TableDefs.Refresh
' Perform the import
db.Execute "INSERT INTO someTable SELECT col1, col2, ... FROM tblImport " _
& "WHERE NOT F1 IN ('A1', 'A2', 'A3')"
db.Close: Set db = Nothing
Your file seems quite small (297 lines) so you can read and write them quite quickly. You refer to Excel CSV, which does not exists, and you show space delimited data in your example. Furthermore, Access is limited to 255 columns, and a CSV is not, so there is no guarantee this will work
Sub StripHeaderAndFooter()
Dim fs As Object ''FileSystemObject
Dim tsIn As Object, tsOut As Object ''TextStream
Dim sFileIn As String, sFileOut As String
Dim aryFile As Variant
sFileIn = "z:\docs\FileName.csv"
sFileOut = "z:\docs\FileOut.csv"
Set fs = CreateObject("Scripting.FileSystemObject")
Set tsIn = fs.OpenTextFile(sFileIn, 1) ''ForReading
sTmp = tsIn.ReadAll
Set tsOut = fs.CreateTextFile(sFileOut, True) ''Overwrite
aryFile = Split(sTmp, vbCrLf)
''Start at line 3 and end at last line -1
For i = 3 To UBound(aryFile) - 1
tsOut.WriteLine aryFile(i)
Next
tsOut.Close
DoCmd.TransferText acImportDelim, , "NewCSV", sFileOut, False
End Sub
Edit re various comments
It is possible to import a text file manually into MS Access and this will allow you to choose you own cell delimiters and text delimiters. You need to choose External data from the menu, select your file and step through the wizard.
About importing and linking data and database objects -- Applies to: Microsoft Office Access 2003
Introduction to importing and exporting data -- Applies to: Microsoft Access 2010
Once you get the import working using the wizards, you can save an import specification and use it for you next DoCmd.TransferText as outlined by #Olivier Jacot-Descombes. This will allow you to have non-standard delimiters such as semi colon and single-quoted text.