Scripting an import task

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm looking for a way to import a text file into access using VBS or JS?
Is it possible and if yes, where can I found the resources to do so?

Thank you for your answer,

R14edge
 
It's possible. I've pasted a sample VBScript below, and there's a more
general-purpose one (to execute an arbitrary query) at
http://www.j.nurick.dial.pipex.com/Code/index.htm

The data source syntax in the sample below assumes a CSV file with field
names in a header row.(the HDR= argument). For files other than CSV, you
have to specify the format in a schema.ini file. For documentation, see

Create a Schema.ini file based on an existing table in your database:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;155512
How to Use Schema.ini for Accessing Text Data
http://support.microsoft.com/default.aspx?scid=kb;EN-US;149090
Schema.ini File (Text File Driver)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcjetschema_ini_file.asp

I'm looking for a way to import a text file into access using VBS or JS?
Is it possible and if yes, where can I found the resources to do so?



'Sample VBScript to import data from a textfile into
'a table in an MDB database without opening Access

'Modify DB_NAME, TBL_NAME, DATA_SOURCE as required
'and the code that builds strSQL as necessary.

'If TBL_NAME exists, appends to it; otherwise creates it.

Option Explicit

Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String

Const DB_NAME = "C:\Temp\Test 2003.mdb"
Const TBL_NAME = "My_Table"
Const DATA_SOURCE = "[Text;HDR=Yes;Database=C:\Temp\;].MyFile#txt"

Set oJet = CreateObject("DAO.DBEngine.36")
Set oDB = oJet.OpenDatabase(DB_NAME)

For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next

If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If

oDB.Execute strSQL

oDB.Close
 
Back
Top