|
Question : Automate import of multiple data files, based on multiple auto-imported file layouts.
|
|
I created a VB module to import file layouts. The files, now Access tables, include Field name, description, start postion, data type, and length of fields in flat files that will be created later. There can be no guarantee that layouts will be the same for all the data files, as they all contain different types of data.
Is there a way to use the file layout information to automate the import of the data files? Perhaps using them to auto create TextFile Import Specifications? Then i could run some VB code to import the data using the created file specifications via DoCmd.TransferText.
|
|
Answer : Automate import of multiple data files, based on multiple auto-imported file layouts.
|
|
an alternative way to import data is using Schema.ini files
Now you could perhaps generate a schema.ini file on the fly then import a file using that
are you aware on how to read csv files using schema.ini ? its like creating a recordset then reading values from there then you just do whatever you want to do
e.g.. Ive knocked up a small example, the first procedure creates a schema.ini file based on the table (so if table changes, it dont matter so as long as csv file is changed to match new table)
Private Const SCHEMA_INI_PATH = "C:\EE\dyn\"
Public Sub CreateSchemaIni()
Dim iHandle As Integer Dim i As Integer Dim sCSVFile As String On Error Resume Next iHandle = FreeFile Open SCHEMA_INI_PATH & "Schema.ini" For Output As iHandle If Err.Number <> 0 Then MsgBox Err.Description Else sCSVFile = "test.csv" Print #iHandle, "[" & sCSVFile & "]" Print #iHandle, "Format=CSVDelimited"
For i = 0 To CurrentDb.TableDefs("AuditTrail").Fields.Count - 1 Print #iHandle, "Col" & i + 1 & "=" & CurrentDb.TableDefs("AuditTrail").Fields(i).Name & " " & GetDataType(CurrentDb.TableDefs("AuditTrail").Fields(i).Type) Next i Close #iHandle ImportViaSchema sCSVFile End If End Sub
Function ImportViaSchema(ByVal sCSVFile As String) Dim db As Database Dim rs As DAO.Recordset Dim i As Integer On Error GoTo ImpFailure Set db = OpenDatabase(SCHEMA_INI_PATH, False, False, "TEXT;Database=" & SCHEMA_INI_PATH & ";table=" & sCSVFile) Set rs = db.OpenRecordset(sCSVFile) If rs.EOF = True Then Debug.Print "No Records" Else rs.MoveFirst While rs.EOF = False 'Do what you want with the csv values, here Im just dumping it For i = 0 To rs.Fields.Count - 1 Debug.Print rs(i).Value Next i rs.MoveNext Wend rs.Close End If ImpOk: On Error Resume Next Set rs = Nothing Set db = Nothing Exit Function ImpFailure: MsgBox Err.Description GoTo ImpOk
End Function
Private Function GetDataType(ByVal iType As Byte) As String
Select Case iType Case dbBigInt: Case dbDecimal: Case dbInteger: Case dbLong: Case dbNumeric: Case dbSingle: GetDataType = "Integer" Case dbMemo: GetDataType = "Memo" Case dbDate: GetDataType = "DateTime" Case Else: GetDataType = "Text" End Select End Function
|
|
|
|