VBA, Excel spreadsheet to Access Database 
Author Message
 VBA, Excel spreadsheet to Access Database

This is becoming something of a periodic posting. :)

I want to programatically import the data from an Excel Spreadsheet to an
Access Database table. The user should only need to supply the file name for
the excel file. The excel file contains only one worksheet.

Now, without using the mediocre bugfeast that is TransferSpreadsheet method
I want to get all the data from the Excel file into Access.

This can be done with DAO, connecting a table to the excel file. Now it gets
interesting, we don't know the name of the worksheet itself, we only know
that it is the only worksheet in the file. Is there a way to get say an
enumeration of the worksheets? (Litterature seems to be hinting at the fact
that you can get all kinds of data out of this connection, if only you know
the proper things to ask for. Unfortunately no one seems to list the proper
things...)

So, how can I get a known excel file containing only one worksheet, the name
of which is unknown into a table in Access?

TIA
Mathias



Tue, 07 Oct 2003 22:09:34 GMT  
 VBA, Excel spreadsheet to Access Database
Hi Mathias,

If the file only contains one worksheet, you could probably use something
like

    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet

    Set xlApp = CreateObject("Excel.Application")
    Set xlWorkBook = xlApp.Workbooks.Open(Filename:=YourFileName)
    Set xlWorkSheet = xlWorkBook.Worksheets(0)

Hope this helps

David


Quote:
> This is becoming something of a periodic posting. :)

> I want to programatically import the data from an Excel Spreadsheet to an
> Access Database table. The user should only need to supply the file name
for
> the excel file. The excel file contains only one worksheet.

> Now, without using the mediocre bugfeast that is TransferSpreadsheet
method
> I want to get all the data from the Excel file into Access.

> This can be done with DAO, connecting a table to the excel file. Now it
gets
> interesting, we don't know the name of the worksheet itself, we only know
> that it is the only worksheet in the file. Is there a way to get say an
> enumeration of the worksheets? (Litterature seems to be hinting at the
fact
> that you can get all kinds of data out of this connection, if only you
know
> the proper things to ask for. Unfortunately no one seems to list the
proper
> things...)

> So, how can I get a known excel file containing only one worksheet, the
name
> of which is unknown into a table in Access?

> TIA
> Mathias



Tue, 07 Oct 2003 23:15:43 GMT  
 VBA, Excel spreadsheet to Access Database
Or you could do something like this, Mathias.  This will give you the first
row of each column of each worksheet too.

  Dim oExcel As Object
  'Dim oExcel As Excel.Application
  Dim oExcelWkbk As Object
  Dim oExcelWksht As Object
  Dim oExcelRange As Object
  Dim intColCnt As Integer
  Dim i As Integer

  Set oExcel = CreateObject("Excel.Application")
  oExcel.Workbooks.Open OFFICES_XLS
  oExcel.Application.Visible = True

  Debug.Print "Number of WorkBooks=" & CStr(oExcel.Workbooks.Count)
  For Each oExcelWkbk In oExcel.Workbooks
    Debug.Print "Excel WorkBook Name=" & oExcelWkbk.Name
    Debug.Print " Number of WorkSheets=" & CStr(oExcelWkbk.WorkSheets.Count)
    For Each oExcelWksht In oExcelWkbk.WorkSheets
      Debug.Print " Excel WorkSheet Name=" & oExcelWksht.Name
      Set oExcelRange = oExcelWksht.UsedRange
      intColCnt = oExcelRange.Columns.Count
      Debug.Print "  Number of Columns=" & CStr(intColCnt)
      If intColCnt > 0 Then
        For i = 1 To intColCnt
          Debug.Print "  Value of Row 1, Column " & CStr(i) & "=" &
CStr(oExcelRange.Cells(1, i).Value)
        Next i
      End If
    Next
  Next


Quote:
> This is becoming something of a periodic posting. :)

> I want to programatically import the data from an Excel Spreadsheet to an
> Access Database table. The user should only need to supply the file name
for
> the excel file. The excel file contains only one worksheet.

> Now, without using the mediocre bugfeast that is TransferSpreadsheet
method
> I want to get all the data from the Excel file into Access.

> This can be done with DAO, connecting a table to the excel file. Now it
gets
> interesting, we don't know the name of the worksheet itself, we only know
> that it is the only worksheet in the file. Is there a way to get say an
> enumeration of the worksheets? (Litterature seems to be hinting at the
fact
> that you can get all kinds of data out of this connection, if only you
know
> the proper things to ask for. Unfortunately no one seems to list the
proper
> things...)

> So, how can I get a known excel file containing only one worksheet, the
name
> of which is unknown into a table in Access?

> TIA
> Mathias



Tue, 07 Oct 2003 23:33:09 GMT  
 VBA, Excel spreadsheet to Access Database
Hi Mathias,

Take a look at the SourceTableName Property with some technics of
automation you can get the worksheet name, look at the other two post
for some hints on that.

I'm on a rush, leaving the office, but if you want I can try to make a
sample...

On Fri, 20 Apr 2001 16:09:34 +0200, "Mathias Karlsson"

Quote:

>This is becoming something of a periodic posting. :)

>I want to programatically import the data from an Excel Spreadsheet to an
>Access Database table. The user should only need to supply the file name for
>the excel file. The excel file contains only one worksheet.

>Now, without using the mediocre bugfeast that is TransferSpreadsheet method
>I want to get all the data from the Excel file into Access.

>This can be done with DAO, connecting a table to the excel file. Now it gets
>interesting, we don't know the name of the worksheet itself, we only know
>that it is the only worksheet in the file. Is there a way to get say an
>enumeration of the worksheets? (Litterature seems to be hinting at the fact
>that you can get all kinds of data out of this connection, if only you know
>the proper things to ask for. Unfortunately no one seems to list the proper
>things...)

>So, how can I get a known excel file containing only one worksheet, the name
>of which is unknown into a table in Access?

>TIA
>Mathias

---------
Pedro Gil
http://www.geocities.com/pmpg98_pt

DDPI
http://www.users.bigpond.com/papwalker/DDPI.HTML
https://sourceforge.net/projects/ddpi/



Wed, 08 Oct 2003 01:34:08 GMT  
 VBA, Excel spreadsheet to Access Database
When a column in excel contains cells that contain only digits, and cells
that contain strings then one of these gets ditched when imported. Both with
TransferSpreadsheet and the DAO version.

In my last import the strings were preserved, while all integer fields in
the column were replaced with "#NUM" in the Access Table.

So, how on earth can I import a worksheet with columns something like
123
Olle
Sand
Gruva
475
923

Without this turning into:
#NUM
Olle
Sand
Gruva
#NUM
#NUM

in Access. It is not possible with TransferSpreadsheet, and even with the
DAO solution proposed it looks the same.

Below is the code used to transfer: (Note db and sourcefile are known before
this block.)

            Dim xlApp As Object
            Dim xlWorkBook As Object
            Dim xlWorkSheet As Object
            Dim xlWrkNamr As String
            Dim tdf As TableDef

            Set xlApp = CreateObject("Excel.Application")
            Set xlWorkBook = xlApp.Workbooks.Open(fileName:=sourcefile)
            Set xlWorkSheet = xlWorkBook.Worksheets(1)
            xlWrkName = CStr(xlWorkSheet.Name) & "$"
            Set tdf = db.CreateTableDef("LinkedCustomerData")
            tdf.Connect = "Excel 8.0;DATABASE=" & sourcefile
            tdf.sourceTableName = xlWrkName
            db.TableDefs.Append tdf
            SQLStr = "SELECT * INTO SourceData FROM LinkedCustomerData"
            db.Execute SQLStr



Quote:
> Or you could do something like this, Mathias.  This will give you the
first
> row of each column of each worksheet too.

>   Dim oExcel As Object
>   'Dim oExcel As Excel.Application
>   Dim oExcelWkbk As Object
>   Dim oExcelWksht As Object
>   Dim oExcelRange As Object
>   Dim intColCnt As Integer
>   Dim i As Integer

>   Set oExcel = CreateObject("Excel.Application")
>   oExcel.Workbooks.Open OFFICES_XLS
>   oExcel.Application.Visible = True

>   Debug.Print "Number of WorkBooks=" & CStr(oExcel.Workbooks.Count)
>   For Each oExcelWkbk In oExcel.Workbooks
>     Debug.Print "Excel WorkBook Name=" & oExcelWkbk.Name
>     Debug.Print " Number of WorkSheets=" &

CStr(oExcelWkbk.WorkSheets.Count)

- Show quoted text -

Quote:
>     For Each oExcelWksht In oExcelWkbk.WorkSheets
>       Debug.Print " Excel WorkSheet Name=" & oExcelWksht.Name
>       Set oExcelRange = oExcelWksht.UsedRange
>       intColCnt = oExcelRange.Columns.Count
>       Debug.Print "  Number of Columns=" & CStr(intColCnt)
>       If intColCnt > 0 Then
>         For i = 1 To intColCnt
>           Debug.Print "  Value of Row 1, Column " & CStr(i) & "=" &
> CStr(oExcelRange.Cells(1, i).Value)
>         Next i
>       End If
>     Next
>   Next



> > This is becoming something of a periodic posting. :)

> > I want to programatically import the data from an Excel Spreadsheet to
an
> > Access Database table. The user should only need to supply the file name
> for
> > the excel file. The excel file contains only one worksheet.

> > Now, without using the mediocre bugfeast that is TransferSpreadsheet
> method
> > I want to get all the data from the Excel file into Access.

> > This can be done with DAO, connecting a table to the excel file. Now it
> gets
> > interesting, we don't know the name of the worksheet itself, we only
know
> > that it is the only worksheet in the file. Is there a way to get say an
> > enumeration of the worksheets? (Litterature seems to be hinting at the
> fact
> > that you can get all kinds of data out of this connection, if only you
> know
> > the proper things to ask for. Unfortunately no one seems to list the
> proper
> > things...)

> > So, how can I get a known excel file containing only one worksheet, the
> name
> > of which is unknown into a table in Access?

> > TIA
> > Mathias



Fri, 10 Oct 2003 16:35:03 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Creating excel Spreadsheet from Access VBA

2. Open excel spreadsheet from within Access using VBA

3. linking an excel spreadsheet to an access database using vb

4. Converting Excel spreadsheet to Access database

5. Excel spreadsheet from an Access database using VB?

6. linking an excel spreadsheet to an access database from vb6

7. Accessing Excel Spreadsheets as a database

8. Save embedded excel spreadsheet in access to excel file

9. problem with access coding accessing an excel spreadsheet

10. Running excel add-in (added into excel - not access) in Access VBA

11. extracting data from an excel spreadsheet into vba code

12. Saving Excel Spreadsheet to STS thru VBA

 

 
Powered by phpBB® Forum Software