Results 1 to 14 of 14
  1. #1
    rickscr is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2010
    Posts
    21

    importing spreadsheet that doesn't match table

    I need to get the data from Excel spreadsheets into an existing Access database table. The problem is that the fields don't match.

    I can't really explain how it needs to work except to show an example (see attached).

    This is a simplified representation. In reality, there are about 20 fields in the Excel files and 15 in the Access table. Data is collected for multiple sites. Each site has it's own spreadsheet for a specific time period. The 'Type' field corresponds to the spreadsheet 'Data' field.

    I've been thinking that some sort of query could do it, but I don't know where to start.
    Last edited by rickscr; 03-25-2011 at 04:47 PM. Reason: Changed attachments, clarified things

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    the pictures really don't help. those look like you're own creations. are they at all related?? for instance, the second pic shows a TYPE, but the first one doesn't.

    If this is a correct rep of the problem, I would manipulate the data in whatever program you're more comfortable with. More than likely, you're gonna be looking at the vba editor to some degree for code writing purposes.

  3. #3
    rickscr is offline Novice
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2010
    Posts
    21
    The images are my own creations to illustrate the problem simply. Hopefully these are less confusing.

    This is what the Excel input files look like for Site A:


    This is how the Access table needs to work:


    Basically, the variables need to go top to bottom instead of left to right, but they still need to match up to the correct date.

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Rick,

    Have you experimented with the transpose function in the paste special menu of Excel??

  5. #5
    rickscr is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    21
    I tried transpose, but it's not what I need.

    Here is some basic code I whipped up in Excel that does what I want. Is it possible to do something like this with Access VBA?

    Code:
    Private Sub CommandButton1_Click()
    
    'count rows that have data
    Dim Rcount As Integer
    Rcount = Application.CountA(Range("A:A"))
    
    Dim CopyRow As Integer
    Dim PasteRow As Integer
    Dim Variable As String
    Dim VariableCol As Integer
    
    PasteRow = 2
    
    'loop through columns and row, copying data down
    For VariableCol = 2 To 4
        For CopyRow = 2 To Rcount
            Variable = Cells(1, VariableCol)
            
            Cells(PasteRow, 7) = Cells(CopyRow, 1) 'Date
            Cells(PasteRow, 8) = Cells(CopyRow, VariableCol) 'Datavalue
            Cells(PasteRow, 9) = Variable 'Variable name
            
            PasteRow = PasteRow + 1
        Next CopyRow
    Next VariableCol
    
    End Sub

  6. #6
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    that's exactly what I was saying earlier, Rick. You already wrote the code. Why won't that work for you?? Just run it and then do your import. Or is that not sufficient enough?

    YES, you can do it in access. With recordset code of course. Anyone can probably write it, but why write it if it's not necessary. Right?

  7. #7
    rickscr is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    21
    I would really prefer to do everything in Access. I have thousands of spreadsheets to import, each with 20+ variables. I will also have minimally trained users importing 25 similar spreadsheets every week this summer.

    Ideally, they would click one button, select the file, choose the site name from a combobox, and be done.

    Of course, if I can't figure it out, I'll go with plan B--using the code I created in Excel--which will make things much more tedious.

  8. #8
    rickscr is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    21
    Here's some thoughts:
    Can I use docmd.TransferSpreadsheet and run a series of imports?
    Each import in the loop would grab a different range.

    For example:
    Loop1 would import the dates and variable 1 values.
    Loop2 would import the dates and variable 2 values.
    Loop3 would import the dates and variable 3 values.

    I think TransferSpreadsheet only works with contiguous ranges, but maybe there's some way to get around this?


    Alternately, I import the spreadsheet exactly as it is into a temporary table. Then I could run an append query that grabbed the date, variable values, and got the site from a form. The downside is that I'd need to create about 20 queries; one for each variable.
    I've attached an example of this method.

  9. #9
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Rick,

    your test db didn't make much sense to me. the tables do, but the append query I don't think is relevant to this scenario of yours.

    My two ideas would be:

    • run a function that manipulates the data via recordset objects
    • use an ADO connection between Access and Excel to iterate and copy data via code directly


    of course, there is always the 'copyfromrecordset' option available from Excel code, which I believe applies to named ranges as well as sheet names. I know for a fact that you can query named ranges from an Excel sheet when you're using ADO.

  10. #10
    rickscr is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    21
    Pardon my ignorance, but what are recordset objects and ADO?

  11. #11
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Rick,

    If you want to upload what you've got, you might get lucky. Someone might just give you a template to go by.

    I could even do it for you, but I can't when I don't have time. If you do upload the file, I'm sure I could get some code back to you within the next 2 days or so, but of course that's not a promise. It's up to you.

    Recordsets are objects in VBA and ADO is a VBA programming library/DLL.

  12. #12
    rickscr is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    21
    Well, here's what I have so far. It seems to work alright using a series of append queries. It may not be elegant, but might be the easiest solution.

    I really appreciate your help with this. There's not really any rush for getting this done, but if you have the time and want to write some code, that would be awesome!

  13. #13
    rickscr is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    21
    I think I've solved it. I figured out how to run SQL in VBA. Now I can modify the SQL to accept variables and use just one query.

  14. #14
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by rickscr View Post
    I think I've solved it. I figured out how to run SQL in VBA. Now I can modify the SQL to accept variables and use just one query.
    Great! Post up again if you need assistance.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Importing Spreadsheet
    By derfalpha in forum Access
    Replies: 1
    Last Post: 01-04-2011, 10:47 AM
  2. Undefined error importing spreadsheet
    By kbremner in forum Import/Export Data
    Replies: 1
    Last Post: 10-23-2010, 05:57 PM
  3. Importing Excel 2007 spreadsheet into Access 2002
    By jhjr in forum Import/Export Data
    Replies: 1
    Last Post: 06-17-2010, 02:05 PM
  4. importing excel spreadsheet issues
    By majortool in forum Import/Export Data
    Replies: 1
    Last Post: 01-24-2008, 06:29 PM
  5. Type error when importing Excel spreadsheet
    By jlmnjem in forum Database Design
    Replies: 2
    Last Post: 08-27-2006, 07:26 PM

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