Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    nguyeda is offline Novice
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    21
    This is my most recent attempt by using my export script from Access to Excel. I'm just trying to reverse it haha



    I deleted a lot of the code that I thought was defiantly irrelevent but not all of it obviously.

    Code:
    Option Compare Database
     
    Public Function ExportAdvancedExcel()
     
    Dim strWorksheet As String
    Dim strWorkSheetPath As String
    Dim appExcel As Excel.Application
     
    Dim sht As Excel.Worksheet
    Dim wkb As Excel.Workbook
    Dim rng As Excel.Range
    Dim strTable As String
    Dim strRange As String
    Dim strSaveName As String
    Dim strPrompt As String
    Dim strTitle As String
    Dim strDefault As String
     
    strSaveName = "C:\Users\NguyenDH\Desktop\Generic SAR\Master.xlsx"
     
    Set appExcel = GetObject(, "Excel.Application")
    appExcel.Workbooks.Open (strSaveName)
    Set wkb = appExcel.ActiveWorkbook
    Set sht = appExcel.ActiveSheet
    Set Sht1 = appExcel.Worksheets(2)
    Set Sht2 = appExcel.Worksheets(3)
     
    With sht
     
    strTable = "MCHB"
    DoCmd.SetWarnings Flase
     
    DoCmd.TransferSpreadsheet transfertype:=acImport, _
    spreadsheettype:=acSpreadsheetTypeExcel9, _
    TableName:=strTable, FileName:=strSaveName, _
    hasfieldnames:=True
    End With
    With Sht1
    strTable1 = "T001W"
    DoCmd.SetWarnings Flase
     
    DoCmd.TransferSpreadsheet transfertype:=acImport, _
    spreadsheettype:=acSpreadsheetTypeExcel9, _
    TableName:=strTable1, FileName:=strSaveName, _
    hasfieldnames:=True
     
    End With
    With Sht2
    strTable2 = "UnOblg"
    DoCmd.SetWarnings Flase
     
    DoCmd.TransferSpreadsheet transfertype:=acImport, _
    spreadsheettype:=acSpreadsheetTypeExcel9, _
    TableName:=strTable2, FileName:=strSaveName, _
    hasfieldnames:=True
     
    End With
     
    Set appExcel = GetObject(, "Excel.Application")
    End Function

  2. #17
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I made a test excel file with three sheets (sht, sht1, sht2) put in some bogus data.

    Made three tables in access with the names worksheet1, worksheet2, worksheet3, gave them field names identical to the first row on each of my worksheets and used this code and it worked perfectly:

    Code:
    Dim strSaveName As String
    strSaveName = CurrentProject.Path & "\TestImport.xlsx"
    
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "worksheet1", strSaveName, True, "sht$"
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "worksheet2", strSaveName, True, "sht1$"
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "worksheet3", strSaveName, True, "sht2$"
    the sheet name has to be encapsulated in "" marks and have a $ mark at the end of the name to tell access you're importing the whole sheet, not just a range of values on the first worksheet.

  3. #18
    nguyeda is offline Novice
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    21
    Thanks! That worked!!

    But it doesn't work in these scenrios in the future for flexibility:


    1) Overwrite the original tables in access OR

    2) Update the tables in access if I add/Change:

    - Columns
    - Column Cell Formats (Number/Date/Text/General/etc)

    Ideas? I suppose I could just delete the tables in access and let it reimport everything...

  4. #19
    nguyeda is offline Novice
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    21
    Also, during the import or after, can we make 1 of the columns in each table a Primary key?

    Edit: May be I should scratch that because the column I use to relate to others is the item number or order number and it's not unique in every table, so I guess that won't work as a primary key?

  5. #20
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    There are two ways you can handle the overwrite.

    One is to purge the data first (run a delete query on the table) then perform your import.
    Two, drop the table and recreate it before each import. This would require probably a little more setup (particularly if you want to make it flexible for future use). For maximum flexibility you would likely have to define the fields and data types you wanted your fields to be in a table and run a procedure that would delete (DROP) the existing table, then recreate it based on the fields of your table creation table.

    As far as the autonumber field goes, you should be able to put that on your table and the import will basically ignore the field when importing data. Just add an autonumber field. Just be aware that your data will likely have new primary key fields from week to week so you will not be able to rely on establishing links based on the primary key in access but will have to use the fields that your root database was using to link them.

  6. #21
    nguyeda is offline Novice
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    21
    Thanks! What is the syntax for maximum flex? Can you give me a random example to go off of?

  7. #22
    nguyeda is offline Novice
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    21
    The queries work significantly faster now by the way

  8. #23
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Here's an example of defining table structure through tables. Just make sure the database and the excel file are in the same folder and open the form, select the file to be processed (note this assumes your files will be named the same each week). I haven't provided for any of the data entry necessary but the structure works.

  9. #24
    nguyeda is offline Novice
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    21
    That looks AWESOME. Thanks so MUCH! I think I'll go ahead and mark this as solved!

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 04-12-2011, 10:14 AM
  2. Replies: 3
    Last Post: 08-04-2010, 09:35 AM
  3. Complex query system for map database
    By brian.tunks in forum Queries
    Replies: 2
    Last Post: 07-28-2010, 07:07 AM
  4. Complex Query/Queries for a Report
    By Rawb in forum Queries
    Replies: 3
    Last Post: 02-04-2010, 07:44 AM
  5. Importing Excel into Access with few requirements
    By pickolizac in forum Import/Export Data
    Replies: 1
    Last Post: 02-18-2009, 12:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums