473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Importing Excel SpreadSheets into Access, from Access, only if not hidden.

I have the project that may never end in front of me.

I am creating a routine that will take SpreadSheets from Excel and
bring them into Access.

I am not using any "DoCmd"s because the goal is for the import code
to be moved to a stand alone VB app which will use the Access DB as a
workspace to process the data from the spreadsheets. Quite honestly,
done right this may not even require Access or Excel to be on the
users machine. Right now all code and testing occurs in Access.

The code I use to bring the sheets into Access looks like:

Public rsTable As ADODB.Recordset
Public strSheet As String
Public StrExceptionReason As String
Public coListOfExcelTables As New Collection

Private cnMvConnection As ADODB.Connection
Private strPath As String
Sub ImportMembers()
Dim i As Integer
Dim j As Integer

Dim cat As ADOX.Catalog
Set cat = New ADOX.Catalog

'The folowing is here for testing - replace with your Path & File.
'I use split and an array because I will need to process many Excel
files at once.
'They will be passed to the final executable "command line style."
strTableName = "X:\MBS_JOBS\71228A\71228ASHIPPER.XLS"
aryShippers = Split(strTableName, " ")

For i = 0 To (CInt(UBound(aryShippers)) -
CInt(LBound(aryShippers)))
strPath = aryShippers(i)

Connect (strPath)

GetExcelTables

For j = 1 To coListOfExcelTables.Count
'I add "xl_" to the tables name so I can separate them
from other
'tables which I have in the DB (helps during testing).
strSheet = "xl_" & coListOfExcelTables(j)

GetExcelData
MakeRowsUnique

'Do LOTS of other stuff - so I can get what I need, change
what needs
'to change and store the results in a pre-existing table
in this DB.
Next

cat.ActiveConnection = CurrentProject.Connection
For j = 1 To coListOfExcelTables.Count
' Delete the table, if it exists - to make way for the
next set
' of Sheets from the next file..
On Error Resume Next
cat.Tables.Delete "xl_" & coListOfExcelTables(j)
'cat.Tables.Refresh
Next
Next

cnMvConnection.Close
Set cnMvConnection = Nothing
End Sub
Private Function Connect(ByVal strFullPathToExcelSpreadsheet As
String) As Boolean
'Make and open a connection to a given Excel spreadsheet
Set cnMvConnection = New ADODB.Connection

With cnMvConnection
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" &
strFullPathToExcelSpreadsheet _
& ";Extended Properties=Excel 8.0;"
.Open
End With
End Function
Private Function GetExcelTables()
'Get a list of tables available in the Excel spreadsheet which was
opened in the "Connect" function
Dim rs As ADODB.Recordset
Dim iTableNumber As Integer: iTableNumber = -1

Set coListOfExcelTables = New Collection
Set rs = New ADODB.Recordset

'Use the ADODB.Connection.OpenSchema method to get the structure
of the Excel spreadsheet
With cnMvConnection
Set rs = .OpenSchema(adSchemaTables)
End With

With rs
Do While Not .EOF
iTableNumber = iTableNumber + 1
'The TABLE_NAME field gives the name of the Table (Sheet).

'Thanks go out to Aceto De'Fabul, Utter Access VIP for
help
'getting Excel Spreadsheets with multi-word names to
import.
'As the code is here it will pull in every single Sheet as
a
'table, but not "PrintArea"s
If Right(.Fields("TABLE_NAME").Value, 10) <"Print_Area"
Then
If Left(.Fields("TABLE_NAME").Value, 1) = "'" Then
'Name of Sheet is two or more words.
coListOfExcelTables.Add
(Mid(.Fields("TABLE_NAME").Value, 2, _
(InStrRev(.Fields("TABLE_NAME").Value, "'") -
3)))
Else
'Name of Sheet is one word.
coListOfExcelTables.Add
(Left(.Fields("TABLE_NAME").Value, _
Len(.Fields("TABLE_NAME").Value) - 1))
End If
End If
.MoveNext
Loop
.Close
End With
Set rs = Nothing
End Function
Private Function GetExcelData() As ADODB.Recordset
Dim strSQL As String

'I have a specific range of data I need to collect - A11:I90
strSQL = "SELECT * INTO [" & strSheet & "] " & _
"FROM [Excel 8.0;HDR=Yes;database=" & strPath & ";].[" &
_
Right(strSheet, Len(strSheet) - 3) & "$A11:I90];"
CurrentProject.Connection.Execute strSQL
Application.RefreshDatabaseWindow
End Function
Private Function MakeRowsUnique()
'Add an autonumber column to ENSURE Row uniqueness.
' If you don't need it just Rem the call to it above.
Set f = CurrentDb.TableDefs(strSheet).CreateField("Unique" ,
dbLong)
f.Attributes = f.Attributes + dbAutoIncrField
f.OrdinalPosition = 0
CurrentDb.TableDefs(strSheet).Fields.Append f
End Function
This code is FAST, by the way. I am able to import the Sheets I need
from several Workbooks in the time it would take to do one using the
old Row-by-Row method that I continue to run into during searches on
the net.

Speed is important in what I am trying to do I have a lot that I need
to do - not shown here.

So what is the problem? Simple really - I need to know how to modify
the code above so that I only import a certain Sheet only if it is not
hidden.

Any suggestions?

Jul 19 '07 #1
0 2896

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Grim Reaper | last post by:
I am importing a .csv file into Access that has 37 fields. My problem is that sometimes the last field only has data at the end of the column (it looks like when you import a file into Access, for...
2
by: Mr. Bungle | last post by:
When importing excel from access I am fully aware that one can import directly into a table. Can you get as specific via code or something to import data from an excel sheet to a FORM (Not Table)...
2
by: nutthatch | last post by:
I want to be able to import an Excel spreadsheet into Access 2K using the macro command Transferspreadsheet. However, the file I am importing (over which I have no control) contains some records...
1
by: Brian Graham | last post by:
There's an old Access app here that I've been updating. The original "programmer" set things up poorly and every month the end-user has been loading multiple spreadsheets by creating new tables and...
2
by: jason.teen | last post by:
Hi, I am having trouble importing a spreadsheet from Excel into an Access Database. I have noticed that even specifying the Cell Type of the Excel Data Cell, When I import it, still get...
0
by: DrewYK | last post by:
I have the project that may never end in front of me. I am creating a routine that will take SpreadSheets from Excel and bring them into Access. I am not using any "DoCmd"s because the goal...
1
by: thadson | last post by:
Hi, I'm trying to import specific cells from MS Excel 2000 spreadsheets to MS Access 2000 tables then move the spreadsheets to a different directory. I'm very new to this and I'm having trouble...
3
by: Umoja | last post by:
Hi All, This seems like a simple thing, but I can’t seem to figure it out. I am familiar with transferring excel spreadsheets into access using the TransferSpreasheet function...
1
by: bardwell | last post by:
We have created 4 complex Excel spreadsheets with computations (no data) that we don't want to reprogram in Access. I am hoping we can open the Excel spreadsheets, and use them in the Access program...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.