Results 1 to 9 of 9

Thread: Export Jet Table to Excel worksheet

  1. #1

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Having a spot of bother (or rather a right load of old stress and grief) exporting a table to excel.

    I can get VB6 to open Access, and even get the right db open (using OpenCurrentDatabase) but can't seem to figure out how to select a table and then export that table to an Excel 97 workbook. Would be awfully grateful, and I'd give you ten points and a gold star thing.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Here's one simple way of doing it:
    Code:
    Dim fld As Field
    Dim sText As String
    Dim rs As Recordset
    
    Set rs = YourDBObject.OpenRecordset("TableName", dbOpenSnapShot)
    Do While Not rs.EOF
        For Each fld In rs.Fields
            sText = sText & fld.Value & vbTab
        Next
        sText = sText & vbCrLf
        rs.MoveNext
    Loop
    On Error Resume Next
    Set xl = GetObject(, "Excel.Application")
    If Err Then
        Set xl = CreateObject("Excel.Application")
    End If
    Clipboard.Clear
    Clipboard.SetText sText
    xl.Workbooks.Add
    xl.Visible = True
    xl.Range("A1").Select
    xl.ActiveSheet.Paste
    Good luck!

  3. #3

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Actually, Joacim, I found a much easier way of doing it:

    Code:
    Sub ExportToExcel()
     Dim myAccess As Access.Application
    '**********************************
     Set myAccess = CreateObject("Access.Application")
     With myAccess
      .Visible = False
      .OpenCurrentDatabase ("C:\Data\UKData.mdb")
      .DoCmd.TransferSpreadsheet acExport, 8, "IMP", "C:\Windows\Desktop\Export.xls", True
      .Quit
     End With
     Set myAccess = Nothing
     MsgBox "Exported OK", vbInformation, "Export"
    End Sub
    The only bummer with this method is that I have to include the Access 8 Object Library with my project, so it gets even bigger to distribute...

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Just because you set a reference to the library doesn't mean you have to distribute it.
    If the end-user doesn't have Access they can't run the code. If you don't include the DAO library.

  5. #5

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Sorry Joacim, yeah I meant to say that - very few people in our organisation have Access on their machines, so I will have to distribute the object library...

    Cheers, for your help though...

  6. #6
    Member
    Join Date
    May 2000
    Location
    NY
    Posts
    47
    Coox, how can we specify which "Sheet" (in the excel file) is to be used to copy the data?

    Thanks,
    Rowena

  7. #7

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Rowena - do you mean which sheet you want the data to go to? Or do you just want to rename the sheet? Please elucidate...

  8. #8
    Member
    Join Date
    May 2000
    Location
    NY
    Posts
    47
    Yes Coox, which sheet I want to go the data. Actually, I am doing the reverse side. I am importing my excel data to a particular table in Access. However, my excel file has 5 sheets and I just want to transfer Sheet#5 to my table in Access. Is this possible? I hope so.

    Thanks a lot!
    Rowena

  9. #9

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Rowena,

    Right, so far I can't find a way of specifying just the name of a sheet - the method I used just seems to plump for the first spreadsheet - but I did find a way around it - just NAME the range on the sheet you want to import, eg, go to Sheet5, select the whole area to import, and then key a name in the Name Box (top left).

    Anyway, here's some code that works:
    Code:
    Private Sub Command1_Click()
     Dim myAccess As Access.Application
    '**********************************
     Set myAccess = CreateObject("Access.Application")
     With myAccess
      .Visible = False
      
      .OpenCurrentDatabase ("C:\Windows\Desktop\MyOldDb.mdb") 'Either pick an existing DB or...
      .NewCurrentDatabase ("C:\Windows\Desktop\MyNewDb.mdb") '  ...make a new one
      
      
      '"MyTable" can be an existing one or a new one
      .DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "MyTable", "C:\Windows\Desktop\MySpreadSheet.xls", , "MyNamedRange"
      .Quit
     End With
     Set myAccess = Nothing
     MsgBox "Imported OK", vbInformation, "Import"
    End Sub
    Good luck... please post here if you find how to just specify a sheet without naming a range first...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width