Access 

DAO - Question 10

 

 

 

How do I import text data?

[Access]   [ADO]   [DAO]

; ;    Question 10 -- How do I import text data?

; ;   First we need to setup some variables...    dpath = DirScript()    spec = "DAO-Export.txt"    dTable = "NewTable-DAO" ;    tfile = StrCat(dpath, spec)    dbname = StrCat(dpath, "Winbatch-Access-Tutorial.mdb") ; ;   Now open the DAO objects...    dao = ObjectCreate("DAO.DBEngine.36")    ws = dao.CreateWorkspace("JetWorkspace", "admin", "") ; ;   specify the input file's type, path and name... ;    ifile = "Text;DATABASE=%dpath%;table=%spec%" ; ;   Open up the input file as though it were a database... ;    db = ws.OpenDatabase(dpath,@FALSE,@FALSE,ifile)    sqlstr = `SELECT * INTO [%dTable%] IN '%dbname%' FROM [%spec%]`    db.execute(sqlstr) ;    db = 0    ws = 0    dao = 0 ; ;   Now open up Access and take a peek at the new table and it's data. ; ;   Note: you'll get an error if the table already exists, so you can do two things, ;   either delete the table by hand, or write a script to do so. Another option would ;   be to use a different SQL statement, such as "INSERT INTO..." instead of "SELECT INTO..." ;   this will append data, so be careful, you don't accidentally duplicate data unnecessarily.