Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Macro/Cmd to Import Excel file into Access

Posted on 2010-09-14
10
Medium Priority
?
1,318 Views
Last Modified: 2013-11-28
I have a temp table in Access that I import data into and then (based on various criteria) append to a second table.

I'd like to automate the import process into the temp table.  Currently I delete current records and then go through the import wizard, etc, etc.  I'm planning to use a macro and can use all the STD commands fir deleting current records and running update/append queries.  My issue is the import of te Excel file.  Is there a command or something I can run within the macro to do the import?  I will be importing a file with a STD name and set file path location.  It will be imported into an existing table.  Currently using Excel 2003 but will soon be upgrading to 2010 if that needs to be considered.  I am using Access 2000.  Thanks.
0
Comment
Question by:vsllc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 2000 total points
ID: 33675228
If file name and path is always same, you can link this file to your DB and run queries from macro to delete/append records to existing table. Excel 2010 can save in format of Excel 2003 if there will be any problems with linking Excel 2010 file to Access 2000.
0
 
LVL 40

Expert Comment

by:als315
ID: 33675254
I can recommend you to use csv format if you have in one column mixed text and numeric cells.
0
 

Author Comment

by:vsllc
ID: 33675301
Can't export from source system as csv.

I never thought of linking to Excel.  I'll give it a try.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 40

Expert Comment

by:als315
ID: 33675496
Excel can save file as csv. It can be done with simple macro, which you can run from Access before import, or it is possible from VBA open Excel and save your file as csv. Do it if in linked file some cells will have wrong values.
0
 

Author Comment

by:vsllc
ID: 33675642
Does the link only impact alphanumeric fields?

How would I add a macro to do thus within my Access macro?  My goal is to basically make this a 1 click process so I don't want to have to open Excel to save ad csv and then start process.
0
 
LVL 40

Expert Comment

by:als315
ID: 33682138
You can use this sub for file conversion (compiled from many sources):
(change path from current to your)
Sub SaveAsCSV_xlFile(XlsFileName as string, CSVFileName as string)
    Dim oXL As Object
    Dim sFullPath, sFullPath1 As String
'   Create a new Excel instance
    Set oXL = CreateObject("Excel.Application")
'   Full path of excel file to open
    On Error GoTo ErrHandle
    sFullPath = CurrentProject.Path & XlsFileName
    sFullPath1 = CurrentProject.Path & CSVFileName
'   Open and save as it
    With oXL
        .Visible = True
        .Workbooks.Open (sFullPath)
        .ActiveWorkbook.SaveAs FileName:=sFullPath1, FileFormat:=6
    End With
ErrExit:
    Set oXL = Nothing
    Exit Sub
    
ErrHandle:
    oXL.Visible = False
    MsgBox Err.Description
    GoTo ErrExit
End Sub

Open in new window

0
 
LVL 35

Expert Comment

by:Norie
ID: 33685137
Why not take a look at the DoCmd.TransferSpreadsheet method?

That's the usual way to import data from Excel into Access.

It has various arguments you can specify and you can use it to create a new table or append to an existing table.

That should do the import then you can run your queries from code using DoCmd.RunSQL.

No need to open Excel really, in fact if you try to import from an open file you might run into problems.
0
 
LVL 40

Expert Comment

by:als315
ID: 33693322
Transferspreadsheet very often can not correctly import fields. Is better use csv file, import (or link) it as text file, and then make conversion to Access tables.
0
 
LVL 35

Expert Comment

by:Norie
ID: 33693613
Eh, how?

If you do things properly and the data is well-defined then you should have no problem with TransferSpreadsheet.

Well not any problem you would have importing the same data in CSV format using TransferText.

If there was a problem it would most likely be something to do with data types and it would apply to both methods.

One solution for that is to import to an existing table with the correct data types for the fields.

Another is to import into a table where the fields are all text.

Once you've got the data in Access you can go on and convert it as required, as you've indicated.
0
 

Author Closing Comment

by:vsllc
ID: 33695988
Thanks.  This worked.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question