Solved

MICROSOFT ACCESS 2007 - importing excel files into a table.

Posted on 2011-02-15
7
347 Views
Last Modified: 2012-05-11
I have the following code to import excel files into access table.  But it only inserts the first row of each file.  I need it to insert all the rows of each column.


Public Function MYPREMLOAD()

Dim strPath As String
Dim appExcel As Excel.Application
Dim strFolderPath As String
Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim strSQL As String
  
DoCmd.SetWarnings False
  strSQL = "Delete * From tblCustomer;"
  DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
  
Set MyDB = CurrentDb()

Set MyRS = MyDB.OpenRecordset("tblCustomer", dbOpenDynaset)
  
'strFolderPath = "C:\Customers\"
'strPath = "C:\Customers\*.xls"     'Set the path.
  
strFolderPath = "X:\Special Risk\miketesting\POI\"
strPath = "X:\Special Risk\miketesting\POI\*.xls"
  
strPath = Dir(strPath, vbNormal)   'Retrieve the first entry.
Set appExcel = CreateObject("Excel.Application")
  
Do While strPath <> ""    'Initiate the loop
  appExcel.Workbooks.Open strFolderPath & strPath
  appExcel.Visible = True
  appExcel.Sheets("SR").Select
    With MyRS
      .AddNew
        !Policy = appExcel.Range("D2").Value
        !Effective = appExcel.Range("F2").Value
        !Yr = appExcel.Range("G2").Value
        !Premium = appExcel.Range("J2").Value
      .Update
    End With
  strPath = Dir           'Next entry
Loop
  
appExcel.Quit
Set appExcel = Nothing
  
MyRS.Close
Set MyRS = Nothing
  
MsgBox "This Process has completed!"
  

End Function

Open in new window

0
Comment
Question by:centralmike
[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
  • 4
  • 2
7 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34905317
Try this

Const xlUp = -4162

Public Function MYPREMLOAD()
    Dim strPath As String, strFolderPath As String, strFolderPath As String
    Dim appExcel As Excel.Application
    Dim MyDB As DAO.Database, MyRS As DAO.Recordset
    Dim LastRow As Long
    
    DoCmd.SetWarnings False
    strSQL = "Delete * From tblCustomer;"
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
 
    Set MyDB = CurrentDb()
    
    Set MyRS = MyDB.OpenRecordset("tblCustomer", dbOpenDynaset)
     
    strFolderPath = "X:\Special Risk\miketesting\POI\"
    strPath = "X:\Special Risk\miketesting\POI\*.xls"
     
    strPath = Dir(strPath, vbNormal)
    Set appExcel = CreateObject("Excel.Application")
     
    Do While strPath <> ""
        appExcel.Workbooks.Open strFolderPath & strPath
        appExcel.Visible = True
        With MyRS
            LastRow = Sheets("SR").UsedRange.SpecialCells(xlCellTypeLastCell).Row
            
            For i = 2 To LastRow
                .AddNew
                !Policy = appExcel.Range("D" & i).Value
                !Effective = appExcel.Range("F" & i).Value
                !Yr = appExcel.Range("G" & i).Value
                !Premium = appExcel.Range("J" & i).Value
                .Update
            Next
            
        End With
        strPath = Dir
    Loop
     
    appExcel.Quit
    Set appExcel = Nothing
     
    MyRS.Close
    Set MyRS = Nothing
     
    MsgBox "This Process has completed!"
End Function

Open in new window


Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34905325
Replace line 1

Const xlUp = -4162

by

Const xlCellTypeLastCell = 11

Sid
0
 

Author Comment

by:centralmike
ID: 34906695
Thanks the code works great.  Just one addition question.  What if I want to start reading the files at line 3 of all the worksheets.  Is there away do accomplish that?

Thanks

Mike
0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 34906730
Change

For i = 2 To LastRow

to

For i = 3 To LastRow

Sid
0
 

Author Comment

by:centralmike
ID: 34907388
Can column names be used instead of column letters?  What if the policy is column "M" instead of column "D".  Can you reference the name of column in stead of the letter?

Thanks again Mike
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34910056
Yes

Col A - 1
Col B - 2

and so on...

So if you want to refer to Range("A1") then you can also say Cells(1,1) or if you want to refer to range("A2") the you can also refer to it as cells(2,1) where the syntax is

Cells(Row,Column)

Sid
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Port# 500 and 4500 not open by ISP 10 92
site - site VPN 3 81
Sonicwall VPN and DHCP Setup 10 95
SSL-VPN Solution 8 36
Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

710 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