vba - Import 150 text files to Access as Tables using SQL -
i have 150 text files in csv format using pipe | seperator. files in different folders on network. filenames date specific , have suffix holds date. have created table has 3 fields: file location; file name excluding suffix; suffix (in format yymmdd).
i want create sql script import each of 150 files named in table have created 150 seperate access 2007 tables named file name excluding suffix. tried using vba filenames contain full stops , vba didn't this.
each of files has different column structure first row headers. can use features native access 2007 organisation work not allow third party addons or applications. don't have sql server available me.
i complete novice when comes access , struggling achieve approaching this. can help?
i struggled achieve wanted in access , went on excel instead. code below creates each of text files sheet within single excel workbook master table holding filename / path etc "master" sheet within workbook. deletes of sheets other "master" before recreating them , if file not found leave blank sheet.
sub importfiles() 'this script looks @ master list of files import , imports each own tab 'the files pipe (|) delimited , can in addressable location 'if files not found, import of file skipped, leaving blank worksheet 'close report file before starting import application.displayalerts = false on error resume next windows("report.xlsx").activate windows("report.xlsx").close savechanges:=true on error goto 0 application.displayalerts = true 'start looking @ sheet contains master list of files sheets("master").activate 'declare 3 arrays of unknown length dim filename() string dim filepath() string dim fullname() string 'initially there no files dim numberfiles integer numberfiles = 0 'loop on of file cells 'the master file needs structured filename, filepath , fullname in order 'change c2 cell containing first filename in master list dim filecell range dim topcell range dim filerange range set topcell = range("c2") set filerange = range(topcell, topcell.end(xldown)) each filecell in filerange 'we've found file! numberfiles = numberfiles + 1 'for each file found, extend arrays redim preserve filename(numberfiles - 1) redim preserve filepath(numberfiles - 1) redim preserve fullname(numberfiles - 1) 'now store filename, path , fullname of new file filename(numberfiles - 1) = filecell.value filepath(numberfiles - 1) = filecell.offset(0, 1).value fullname(numberfiles - 1) = filecell.offset(0, 2).value next filecell 'delete existing sheets except master , create new blank sheets each sheet in application.worksheets application.displayalerts = false if sheet.name <> "master" sheet.delete end if next sheet sheets("master").activate = 1 (numberfiles) worksheets.add(after:=sheets(sheets.count)).name = "sheet" & next thisworkbook.sheets("sheet1").select 'start process of import 'create workbook object workbook current macro running dim ws worksheet 'import each file own sheet = 0 (numberfiles - 1) set ws = thisworkbook.sheets("sheet" & + 1) 'the current sheet 'ignore missing files , carry on processing application.displayalerts = false on error resume next 'this imports delimited files ws.querytables.add(connection:="text;" & fullname(i), destination:=ws.range("$a$1")) .name = "a" & .fieldnames = true .rownumbers = false .filladjacentformulas = false .preserveformatting = true .refreshonfileopen = false .refreshstyle = xlinsertdeletecells .savepassword = false .savedata = true .adjustcolumnwidth = true .refreshperiod = 0 .textfilepromptonrefresh = false .textfileplatform = 850 .textfilestartrow = 1 .textfileparsetype = xldelimited .textfiletextqualifier = xltextqualifierdoublequote .textfileconsecutivedelimiter = false .textfiletabdelimiter = true .textfilesemicolondelimiter = false .textfilecommadelimiter = false .textfilespacedelimiter = false .textfileotherdelimiter = "|" .textfilecolumndatatypes = array(1, 1, 1) .textfiletrailingminusnumbers = true .refresh backgroundquery:=false end 'rename tabs name on master list ws.name = "" & filename(i) next 'reopen report file workbooks.open filename:=thisworkbook.path & "\report.xlsx" end sub
Comments
Post a Comment