Solved

Import data from a excel file to Access table

Posted on 2004-08-23
1
220 Views
Last Modified: 2012-05-05
Hi All

I have a location in the system where the user stores an excel file with the data for the week. The data from this excel file has to be appended in to an access table.
The system has to pick the latest file from the folder (by picking the file with the latest date) and append the data in to the table.

How can this be done. Any code help will be highly appreciated.

Thanks
Ram

0
Comment
Question by:sramkris
[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
1 Comment
 
LVL 5

Accepted Solution

by:
peterpuscas earned 100 total points
ID: 11877434
Hi sramkris,

you can use the function getLatestFile as it.
You have to change the folder (C:\TMP\TMPACCESS\), the table name (YourTable),
probably the spreadSheet type and the range from the spreadSheet


Private Sub Command0_Click()
  On Error Resume Next
  DoCmd.DeleteObject acTable, "YourTable"
  On Error GoTo 0
  Dim myFile As String
  myFile = getLatestFile("C:\TMP\TMPACCESS\")
  If Len(myFile) > 0 Then
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "YourTable", myFile, True, "A1:D3"
  End If
  '  Label1.Caption = myFile
End Sub

Function getLatestFile(myFolder As String) As String
  Dim fs, f
  Dim fileDate, lastDate As Date
  Dim crtFile, lastFile As String
 
  getLatestFile = ""
  Set xx = Application.FileSearch
  Application.FileSearch.LookIn = myFolder
  Application.FileSearch.FileName = "*.xls"
  xx = Application.FileSearch.Execute(msoSortByLastModified, msoSortOrderAscending, True)
 
  If Application.FileSearch.FoundFiles.Count > 0 Then
    '  getLatestFile = Application.FileSearch.FoundFiles(1)
     Set fs = CreateObject("Scripting.FileSystemObject")
     For i = 1 To Application.FileSearch.FoundFiles.Count
     crtFile = Application.FileSearch.FoundFiles(i)
     Set f = fs.GetFile(crtFile)
     fileDate = f.DateLastModified
     If i = 1 Then
       lastDate = fileDate
       lastFile = crtFile
     ElseIf lastDate < fileDate Then
       lastDate = fileDate
       lastFile = crtFile
     End If
     Next i
     getLatestFile = lastFile
  End If
End Function

Good Luck,
Peter
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

628 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