VBA for Access, import Excell files into Access tables

G

George

Hi there,

I use this code

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97,
"TableName", "C:\File.xls"

to import 1 Excel file into Access table named TableName.

Now how do I import many files?

Full path to files is in field named fullPath, table "Table3" :

fullPath
C:\Marchello\FUNCS.XLS
C:\Marchello\PROTTPLN.XLS
C:\Marchello\PROTTPLV.XLS
C:\Marchello\SOLVSAMP.XLS

Also I need to name each of table using short names of appropriate
Excel files.

Could you please show me solution?
 
G

George

Ken,
thanx, I've seen your page before, I have to say it doesn't describe
all my situation of sure, so I decided to post my question here.

As a result of my work I need to create table with such fields:

field1 - names of parsed Excel files
field2 - strings found
field3 - "create", "change" or "delete":

each Excel file prepared to parse consists of three blocks:
create value
value_A_1
....
value_A_N
change value
value_B_1
....
value_B_N
delete vaule
value_C_1
....
value_C_N

So I guess Excel files (more than 300) must be imported in separate
tables named as names of Excel files.

Could you please give me advise how do I perform this?
Thanx in advance.

31/01/2009, 19:19, "Ken Snell \(MVP\)"
 
K

Ken Snell \(MVP\)

Your description of the data in the EXCEL files is not clear. Can you be
more specific about the "blocks" of data -- show real data and the cells in
which the data are found.

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


Ken,
thanx, I've seen your page before, I have to say it doesn't describe
all my situation of sure, so I decided to post my question here.

As a result of my work I need to create table with such fields:

field1 - names of parsed Excel files
field2 - strings found
field3 - "create", "change" or "delete":

each Excel file prepared to parse consists of three blocks:
create value
value_A_1
....
value_A_N
change value
value_B_1
....
value_B_N
delete vaule
value_C_1
....
value_C_N

So I guess Excel files (more than 300) must be imported in separate
tables named as names of Excel files.

Could you please give me advise how do I perform this?
Thanx in advance.

31/01/2009, 19:19, "Ken Snell \(MVP\)"
 
G

George

Ken,

I really work with this file at home.
As an example please find printscreen here:
http://marchello.ccx-grads.org/example.jpg

I can't post into usenet from my workplace.
The structure of real files is the same.
"Create", "change", "delete" words are not in English, also I have
"F3" column with description of 7-digit values (it is not important
for our task).

Please find current version of working solution below (two modules).

Now I have to merge these two modules into one;
temporary table for importing Excel files must be the one;
I have to add the name of each spreadsheet into table named
tblOutput;
drop all tables except tblOutput after work.
I can't know all names of tables with import errors (description of 7-
digit values is rather to long to import), otherwise I would use sql
with DROP.

Please, be so kind, suggest me the way of solution.
Thanks ahead.

Your description of the data in the EXCEL files is not clear. Can you be
more specific about the "blocks" of data -- show real data and the cells in
which the data are found.




http://marchello.ccx-grads.org/solution.txt

Sub test()

Dim rstCurr As DAO.Recordset
Dim dbsCurr As Database
Dim MyPath As String
Dim MyFile As String
Dim ShortFile As String
MyPath = "C:\Marchello\"
Set dbsCurr = Access.CurrentDb
Set rstCurr = dbsCurr.OpenRecordset("Table1", dbOpenDynaset)
MyFile = Dir(MyPath)
Do While MyFile <> ""
rstCurr.AddNew
'rstCurr.Fields("1").Value = Time$
'rstCurr.Fields("2").Value = Date$
'rstCurr.Fields("3").Value = MyPath
'rstCurr.Fields("4").Value = MyFile
'rstCurr.Update
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, MyFile,
MyPath & MyFile
MyFile = Dir
Loop

End Sub









Sub test()

Dim db As DAO.Database, tdf As DAO.TableDef
Dim strAction As String
Dim rstIn As DAO.Recordset, rstOut As DAO.Recordset

' Point to this database
Set db = CurrentDb
' Open the output recordset
Set rstOut = db.OpenRecordset("tblOutput", _
dbOpenDynaset, dbAppendOnly)
' Loop through all tabledefs
For Each tdf In db.TableDefs
' Look for a table name starting with "r"
'If tdf.Name Like "r*" Then
If Left(tdf.Name, 1) = "r" Then
' Found one - open it
Set rstIn = db.OpenRecordset("SELECT F2 " & _
"FROM [" & tdf.Name & "] " & _
"WHERE Len(F2 & """") > 0")
' Process all the records
Do Until rstIn.EOF
' See if keyword
If (rstIn!F2 = "Create") Or (rstIn!F2 = "Change") _
Or (rstIn!F2 = "Delete") Then
' Just save the action
strAction = rstIn!F2
Else
' Make sure we have a good action
If Len(strAction) > 0 Then
' Write an output record
rstOut.AddNew
rstOut!Field1 = tdf.Name
rstOut!Field2 = rstIn!F2
rstOut!Field3 = strAction
rstOut.Update
End If
End If
' Get the next record
rstIn.MoveNext
Loop
' Close the input
rstIn.Close
End If
' Get the next table
Next tdf
' Clean up
rstOut.Close
Set rstIn = Nothing
Set rstOut = Nothing
Set tdf = Nothing
Set db = Nothing

End Sub
 
G

George

Ken,

I really work with this file at home.
As an example please find printscreen here:
http://marchello.ccx-grads.org/example.jpg

I can't post into usenet from my workplace.
The structure of real files is the same.
"Create", "change", "delete" words are not in English, also I have
"F3" column with description of 7-digit values (it is not important
for our task).

Please find current version of working solution below (two modules).

Now I have to merge these two modules into one;
temporary table for importing Excel files must be the one;
I have to add the name of each spreadsheet into table named
tblOutput;
drop all tables except tblOutput after work.
I can't know all names of tables with import errors (description of 7-
digit values is rather to long to import), otherwise I would use sql
with DROP.

Please, be so kind, suggest me the way of solution.
Thanks ahead.

Your description of the data in the EXCEL files is not clear. Can you be
more specific about the "blocks" of data -- show real data and the cells in
which the data are found.




http://marchello.ccx-grads.org/solution.txt

Sub test()

Dim rstCurr As DAO.Recordset
Dim dbsCurr As Database
Dim MyPath As String
Dim MyFile As String
Dim ShortFile As String
MyPath = "C:\Marchello\"
Set dbsCurr = Access.CurrentDb
Set rstCurr = dbsCurr.OpenRecordset("Table1", dbOpenDynaset)
MyFile = Dir(MyPath)
Do While MyFile <> ""
rstCurr.AddNew
'rstCurr.Fields("1").Value = Time$
'rstCurr.Fields("2").Value = Date$
'rstCurr.Fields("3").Value = MyPath
'rstCurr.Fields("4").Value = MyFile
'rstCurr.Update
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, MyFile,
MyPath & MyFile
MyFile = Dir
Loop

End Sub









Sub test()

Dim db As DAO.Database, tdf As DAO.TableDef
Dim strAction As String
Dim rstIn As DAO.Recordset, rstOut As DAO.Recordset

' Point to this database
Set db = CurrentDb
' Open the output recordset
Set rstOut = db.OpenRecordset("tblOutput", _
dbOpenDynaset, dbAppendOnly)
' Loop through all tabledefs
For Each tdf In db.TableDefs
' Look for a table name starting with "r"
'If tdf.Name Like "r*" Then
If Left(tdf.Name, 1) = "r" Then
' Found one - open it
Set rstIn = db.OpenRecordset("SELECT F2 " & _
"FROM [" & tdf.Name & "] " & _
"WHERE Len(F2 & """") > 0")
' Process all the records
Do Until rstIn.EOF
' See if keyword
If (rstIn!F2 = "Create") Or (rstIn!F2 = "Change") _
Or (rstIn!F2 = "Delete") Then
' Just save the action
strAction = rstIn!F2
Else
' Make sure we have a good action
If Len(strAction) > 0 Then
' Write an output record
rstOut.AddNew
rstOut!Field1 = tdf.Name
rstOut!Field2 = rstIn!F2
rstOut!Field3 = strAction
rstOut.Update
End If
End If
' Get the next record
rstIn.MoveNext
Loop
' Close the input
rstIn.Close
End If
' Get the next table
Next tdf
' Clean up
rstOut.Close
Set rstIn = Nothing
Set rstOut = Nothing
Set tdf = Nothing
Set db = Nothing

End Sub
 
G

George

Ken,

I really work with this file at home.
As an example please find printscreen here:
http://marchello.ccx-grads.org/example.jpg

I can't post into usenet from my workplace.
The structure of real files is the same.
"Create", "change", "delete" words are not in English, also I have
"F3" column with description of 7-digit values (it is not important
for our task).

Please find current version of working solution below (two modules).

Now I have to merge these two modules into one;
temporary table for importing Excel files must be the one;
I have to add the name of each spreadsheet into table named
tblOutput;
drop all tables except tblOutput after work.
I can't know all names of tables with import errors (description of 7-
digit values is rather to long to import), otherwise I would use sql
with DROP.

Please, be so kind, suggest me the way of solution.
Thanks ahead.

Your description of the data in the EXCEL files is not clear. Can you be
more specific about the "blocks" of data -- show real data and the cells in
which the data are found.




http://marchello.ccx-grads.org/solution.txt

Sub test()

Dim rstCurr As DAO.Recordset
Dim dbsCurr As Database
Dim MyPath As String
Dim MyFile As String
Dim ShortFile As String
MyPath = "C:\Marchello\"
Set dbsCurr = Access.CurrentDb
Set rstCurr = dbsCurr.OpenRecordset("Table1", dbOpenDynaset)
MyFile = Dir(MyPath)
Do While MyFile <> ""
rstCurr.AddNew
'rstCurr.Fields("1").Value = Time$
'rstCurr.Fields("2").Value = Date$
'rstCurr.Fields("3").Value = MyPath
'rstCurr.Fields("4").Value = MyFile
'rstCurr.Update
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, MyFile,
MyPath & MyFile
MyFile = Dir
Loop

End Sub









Sub test()

Dim db As DAO.Database, tdf As DAO.TableDef
Dim strAction As String
Dim rstIn As DAO.Recordset, rstOut As DAO.Recordset

' Point to this database
Set db = CurrentDb
' Open the output recordset
Set rstOut = db.OpenRecordset("tblOutput", _
dbOpenDynaset, dbAppendOnly)
' Loop through all tabledefs
For Each tdf In db.TableDefs
' Look for a table name starting with "r"
'If tdf.Name Like "r*" Then
If Left(tdf.Name, 1) = "r" Then
' Found one - open it
Set rstIn = db.OpenRecordset("SELECT F2 " & _
"FROM [" & tdf.Name & "] " & _
"WHERE Len(F2 & """") > 0")
' Process all the records
Do Until rstIn.EOF
' See if keyword
If (rstIn!F2 = "Create") Or (rstIn!F2 = "Change") _
Or (rstIn!F2 = "Delete") Then
' Just save the action
strAction = rstIn!F2
Else
' Make sure we have a good action
If Len(strAction) > 0 Then
' Write an output record
rstOut.AddNew
rstOut!Field1 = tdf.Name
rstOut!Field2 = rstIn!F2
rstOut!Field3 = strAction
rstOut.Update
End If
End If
' Get the next record
rstIn.MoveNext
Loop
' Close the input
rstIn.Close
End If
' Get the next table
Next tdf
' Clean up
rstOut.Close
Set rstIn = Nothing
Set rstOut = Nothing
Set tdf = Nothing
Set db = Nothing

End Sub
 
K

Ken Snell \(MVP\)

Because the data in your EXCEL file is so unstructured, you're going to need
to read each cell in the file and write it to a recordset so that you then
can write the recordset's data (just the data you want) into your ACCESS
table.

This article shows how you can set up the generic code to do this:

Write Data From an EXCEL Worksheet into a Recordset using Automation
http://www.accessmvp.com/KDSnell/EXCEL_ImpExp.htm#WriteFileRst

You'll need to give careful consideration to the structure of the table on
which you'll base the recordset, and you'll need to 'carry down' values from
columns A and B into subsequent records that you write into the recordset so
that you'll know what each record is supposed to mean. ACCESS will not
retain the order of the rows from EXCEL after you write the data into a
table.

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/



Ken,

I really work with this file at home.
As an example please find printscreen here:
http://marchello.ccx-grads.org/example.jpg

I can't post into usenet from my workplace.
The structure of real files is the same.
"Create", "change", "delete" words are not in English, also I have
"F3" column with description of 7-digit values (it is not important
for our task).

Please find current version of working solution below (two modules).

Now I have to merge these two modules into one;
temporary table for importing Excel files must be the one;
I have to add the name of each spreadsheet into table named
tblOutput;
drop all tables except tblOutput after work.
I can't know all names of tables with import errors (description of 7-
digit values is rather to long to import), otherwise I would use sql
with DROP.

Please, be so kind, suggest me the way of solution.
Thanks ahead.

Your description of the data in the EXCEL files is not clear. Can you be
more specific about the "blocks" of data -- show real data and the cells
in
which the data are found.




http://marchello.ccx-grads.org/solution.txt

Sub test()

Dim rstCurr As DAO.Recordset
Dim dbsCurr As Database
Dim MyPath As String
Dim MyFile As String
Dim ShortFile As String
MyPath = "C:\Marchello\"
Set dbsCurr = Access.CurrentDb
Set rstCurr = dbsCurr.OpenRecordset("Table1", dbOpenDynaset)
MyFile = Dir(MyPath)
Do While MyFile <> ""
rstCurr.AddNew
'rstCurr.Fields("1").Value = Time$
'rstCurr.Fields("2").Value = Date$
'rstCurr.Fields("3").Value = MyPath
'rstCurr.Fields("4").Value = MyFile
'rstCurr.Update
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, MyFile,
MyPath & MyFile
MyFile = Dir
Loop

End Sub









Sub test()

Dim db As DAO.Database, tdf As DAO.TableDef
Dim strAction As String
Dim rstIn As DAO.Recordset, rstOut As DAO.Recordset

' Point to this database
Set db = CurrentDb
' Open the output recordset
Set rstOut = db.OpenRecordset("tblOutput", _
dbOpenDynaset, dbAppendOnly)
' Loop through all tabledefs
For Each tdf In db.TableDefs
' Look for a table name starting with "r"
'If tdf.Name Like "r*" Then
If Left(tdf.Name, 1) = "r" Then
' Found one - open it
Set rstIn = db.OpenRecordset("SELECT F2 " & _
"FROM [" & tdf.Name & "] " & _
"WHERE Len(F2 & """") > 0")
' Process all the records
Do Until rstIn.EOF
' See if keyword
If (rstIn!F2 = "Create") Or (rstIn!F2 = "Change") _
Or (rstIn!F2 = "Delete") Then
' Just save the action
strAction = rstIn!F2
Else
' Make sure we have a good action
If Len(strAction) > 0 Then
' Write an output record
rstOut.AddNew
rstOut!Field1 = tdf.Name
rstOut!Field2 = rstIn!F2
rstOut!Field3 = strAction
rstOut.Update
End If
End If
' Get the next record
rstIn.MoveNext
Loop
' Close the input
rstIn.Close
End If
' Get the next table
Next tdf
' Clean up
rstOut.Close
Set rstIn = Nothing
Set rstOut = Nothing
Set tdf = Nothing
Set db = Nothing

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top