Import data to Access from Excel


February 4, 2008 – 1:53 pm by JP

Back in January, I promised to post a macro that imports data from Excel into Access programmatically.

(See My First Access Macro.)

It takes a given spreadsheet and imports it into a given Access file/table. The spreadsheet must have a header row, with no blank rows or columns inside the table block. You must set a reference to the appropriate Access object library (Microsoft Access x.0 Object Library).

How it works:

First it uses a simple API call to get the local username, so that the spreadsheet can be (temporarily) saved to the user's desktop. Then it checks to see if the .mdb file is already open by looking for a matching .ldb file -- if an Access .mdb file is open, a .ldb file with the same name is created in the same folder to indicate that the database file is open/locked. We don't want to update a file that is already open by another user, to avoid data corruption.

The spreadsheet is saved to the user's desktop and closed for import. Then an instance of Access is started and the spreadsheet imported into the specified table. After a Msgbox is shown to confirm the import, the temp file is deleted from the desktop.

VBA:
  1. Option Explicit
  2. ' set a reference to Access Object Library before using this code
  3.  
  4. Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" ( _
  5.     ByVal lpBuffer As String, _
  6.     nSize As Long) As Long
  7.  
  8. ' function to get username
  9. Public Function Username() As String
  10. Dim lpBuff As String * 1024
  11. GetUserName lpBuff, Len(lpBuff)
  12. Username = Left$(lpBuff, (InStr(1, lpBuff, vbNullChar)) - 1)
  13. End Function
  14.  
  15. Sub ImportReportToAccess()
  16. '
  17. ' open Access database and append ss to appropriate table
  18. '
  19. Application.ScreenUpdating = False
  20.  
  21. ' first check if db file is locked, if so then don't run!
  22. If Dir("C:\databasefile.ldb") = "" Then
  23. ' If a .mdb file is open, a matching .ldb file with the same name is opened in the same directory
  24.     Dim AWName As String
  25.     Dim UserN As String
  26.     ' the newest report
  27.     AWName = ActiveWorkbook.Name
  28.    
  29.     ActiveSheet.UsedRange
  30.     UserN = Username
  31.     Workbooks(AWName).SaveAs "C:\Documents And Settings\" & UserN & "\Desktop\" & AWName, FileFormat:=xlNormal
  32.     Workbooks(AWName).Close
  33.     Dim axsApp As Access.Application
  34.     Set axsApp = New Access.Application
  35.  
  36.     With axsApp
  37.         .OpenCurrentDatabase "C:\databasefile.mdb", True
  38.         .DoCmd.OpenTable "table name"
  39.         .DoCmd.TransferSpreadsheet acImport, , "table name", "C:\Documents And Settings\" & UserN & "\Desktop\" & AWName, True
  40.         .CloseCurrentDatabase
  41.     End With
  42.  
  43.     MsgBox "import complete"
  44.  
  45.     Kill "C:\Documents And Settings\" & UserN & "\Desktop\" & AWName
  46.  
  47. Else
  48.     MsgBox ("Database file appears to be locked. Please try again later."), vbCritical
  49.     Exit Sub
  50. End If
  51.  
  52. ExitProc:
  53. Set axsApp = Nothing
  54. Application.ScreenUpdating = True
  55. End Sub

Enjoy,
JP


If you enjoyed this page:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Access, Excel, VBA, automation
Tags: , , , ,

This post has 1,059 views since February 4, 2008 – 1:53 pm.
  1. 2 Responses to “Import data to Access from Excel”

  2. Great. What if the Excel file must be browsed? How do you use the dialog box in your code?

    By JN on Oct 30, 2008

  3. Hi JN,

    Not sure what you mean. If you need to look at the file first, you can do that before you import to Access.

    --JP

    By JP on Oct 30, 2008

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Personal.xls workbook Not Available When You Start Microsoft Excel || Format SSN - Updated formula »