473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need Help Importing 4 Different Excel Files With The Same Format to Access

anoble1
245 128KB
I have a button in my database that when you hit tit, it imports just 1 excel sheet and puts it in a table. Works great. I am wanting to grab 4 more excel sheets from different locations and store them in the same table.
Here is my code.
I pasted my code in a .txt file so it can be easily read.
Thanks!!
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command35_Click()
  2. On Error GoTo Err_Command0_Click
  3.     Dim appExcel As Object
  4.     Dim workBook As Object
  5.     Dim workSheet As Object
  6.     Dim i As Integer
  7.     Dim FdrID As Integer
  8.     Dim dbs_curr As Database
  9.     Dim records As Recordset
  10.     Dim sqlStatement As String
  11.     Dim message As String
  12.     Set dbs_curr = CurrentDb
  13.  
  14.  
  15.     ' Open an existing spreadsheet
  16.       Set appExcel = GetObject("S:\Workgroups\file1.xls (This one works)
  17.       GetObject ("S:\Workgroups\file2.xls (This one doesnt work currently)
  18.       GetObject ("S:\Workgroups\File3.xls (This one doesnt work currently)
  19.  
  20.     ' Don't show spreadsheet on screen
  21.       appExcel.Application.Visible = False
  22.  
  23.     Set workSheet = appExcel.Worksheets(1)
  24.  
  25.     i = 2
  26.     While workSheet.Cells.Range("A" & i & ":A" & i).Value <> ""
  27.         sqlStatement = "SELECT GetData.* FROM GetData WHERE PDArea IS NULL"
  28.         Set records = dbs_curr.OpenRecordset(sqlStatement, dbOpenDynaset, dbSeeChanges, dbOptimistic)
  29.         records.AddNew
  30.         records!PDArea = workSheet.Cells.Range("A" & i & ":A" & i).Value
  31.         records!Company = workSheet.Cells.Range("B" & i & ":B" & i).Value
  32.         records!Supervisor = workSheet.Cells.Range("C" & i & ":C" & i).Value
  33.         records!GF = workSheet.Cells.Range("D" & i & ":D" & i).Value
  34.         records!Foreman = workSheet.Cells.Range("E" & i & ":E" & i).Value
  35.         records!current = workSheet.Cells.Range("F" & i & ":F" & i).Value
  36.         records!ReportedIllnesses = workSheet.Cells.Range("G" & i & ":G" & i).Value
  37.         records!Sick = workSheet.Cells.Range("H" & i & ":H" & i).Value
  38.         records!Date = workSheet.Cells.Range("I" & i & ":I" & i).Value
  39.         records.Update
  40.  
  41.         i = i + 1
  42.     Wend
  43.  
  44.  
  45.     ' Release objects
  46.     Set workSheet = Nothing
  47.     Set workBook = Nothing
  48.     Set appExcel = Nothing
  49.  
  50.     MsgBox "Done"
  51.  
  52. Exit_Command0_Click:
  53.     Exit Sub
  54.  
  55. Err_Command0_Click:
  56.     MsgBox Err.Description
  57.     Resume Exit_Command0_Click
  58.  
  59. End Sub
Attached Files
File Type: txt Code.txt (2.4 KB, 411 views)
Aug 28 '09 #1
2 2226
Megalog
378 Expert 256MB
Here's a quick way to implement this.. All you have to do is create a table called tblExcelSources, and give it one field named 'Filename'.
Store the full paths of the excel files in that table, and then call the function below. This will loop through all the records in the new table, running your excel import for each one.

Back up your data before doing this!

Also, please use the code tags when posting any follow-up scripts.

Expand|Select|Wrap|Line Numbers
  1. Private Sub ExcelImport()
  2.     Dim appExcel As Object
  3.     Dim workBook As Object
  4.     Dim workSheet As Object
  5.     Dim i As Integer
  6.     Dim FdrID As Integer
  7.     Dim dbs_curr As Database
  8.     Dim records As Recordset
  9.     Dim sqlStatement As String
  10.     Dim message As String
  11.     Dim rsImport As DAO.Recordset
  12.  
  13.    On Error GoTo ExcelImport_Error
  14.  
  15.     Set dbs_curr = CurrentDB
  16.  
  17.     Set rsImport = CurrentDB.OpenRecordset("tblExcelSources", dbOpenSnapshot)
  18.  
  19.     ' Loop through excel filepaths stored in the table 'tblExcelSources'
  20.     Do While Not rsImport.EOF
  21.         rsImport.MoveFirst
  22.  
  23.         Do While Not rsImport.EOF
  24.             ' Open an existing spreadsheet (full file path stored in tblExcelSources.FileName)
  25.             Set appExcel = GetObject(rsImport!Filename)
  26.  
  27.             ' Don't show spreadsheet on screen
  28.             appExcel.Application.visible = False
  29.  
  30.             Set workSheet = appExcel.Worksheets(1)
  31.  
  32.             i = 2
  33.             While workSheet.Cells.Range("A" & i & ":A" & i).Value <> ""
  34.                 sqlStatement = "SELECT GetData.* FROM GetData WHERE PDArea IS NULL"
  35.                 Set records = dbs_curr.OpenRecordset(sqlStatement, dbOpenDynaset, dbSeeChanges, dbOptimistic)
  36.                 records.AddNew
  37.                 records!PDArea = workSheet.Cells.Range("A" & i & ":A" & i).Value
  38.                 records!Company = workSheet.Cells.Range("B" & i & ":B" & i).Value
  39.                 records!Supervisor = workSheet.Cells.Range("C" & i & ":C" & i).Value
  40.                 records!GF = workSheet.Cells.Range("D" & i & ":D" & i).Value
  41.                 records!Foreman = workSheet.Cells.Range("E" & i & ":E" & i).Value
  42.                 records!Current = workSheet.Cells.Range("F" & i & ":F" & i).Value
  43.                 records!ReportedIllnesses = workSheet.Cells.Range("G" & i & ":G" & i).Value
  44.                 records!Sick = workSheet.Cells.Range("H" & i & ":H" & i).Value
  45.                 records!Date = workSheet.Cells.Range("I" & i & ":I" & i).Value
  46.                 records.Update
  47.  
  48.                 i = i + 1
  49.             Wend
  50.  
  51.             ' Release objects
  52.             Set workSheet = Nothing
  53.             Set workBook = Nothing
  54.             Set appExcel = Nothing
  55.  
  56.             rsImport.MoveNext
  57.         Loop
  58.     Loop
  59.     Set rsImport = Nothing
  60.     MsgBox "Done"
  61.  
  62.    On Error GoTo 0
  63.    Exit Sub
  64.  
  65. ExcelImport_Error:
  66.  
  67.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ExcelImport()"
  68.  
  69. End Sub
Aug 28 '09 #2
ADezii
8,834 Expert 8TB
If you do not wish to store the Absolute Paths to the Excel Files internally for whatever reasons (accidental deletion, syntax error on Absolute Paths, etc.), you can Open an Office Dialog Box, then process the File(s) selected. This would be an excellent choice if the Files existed in the same location, but can still be used singularly:
Expand|Select|Wrap|Line Numbers
  1. 'First, set a Reference to the Microsoft Office XX.X Object Library
  2.  
  3. Dim strButtonCaption As String
  4. Dim strDialogTitle As String
  5. Dim varItem As Variant
  6.  
  7. 'Define your own Captions if necessary
  8. strButtonCaption = "Select .xls"
  9. strDialogTitle = "Select Files to Process"
  10.  
  11. With Application.FileDialog(msoFileDialogFilePicker)
  12.   With .Filters
  13.     .Clear
  14.     .Add "Excel Woorkboks", "*.xls"     'Allow only Excel Files
  15.   End With
  16.   'The Show Method returns True if 1 or more files are selected
  17.     .AllowMultiSelect = True       'Critical Line
  18.     .FilterIndex = 1
  19.     .ButtonName = strButtonCaption
  20.     .InitialFileName = vbNullString
  21.     .InitialView = msoFileDialogViewSmallIcons
  22.     .Title = strDialogTitle
  23.   If .Show Then
  24.     For Each varItem In .SelectedItems
  25.       'Process varItem which will contain the Absolute Path to the .xls
  26.       '...
  27.     Next varItem
  28.   End If
  29. End With
Aug 31 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Grim Reaper | last post by:
I am importing a .csv file into Access that has 37 fields. My problem is that sometimes the last field only has data at the end of the column (it looks like when you import a file into Access, for...
9
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then...
2
by: nutthatch | last post by:
I want to be able to import an Excel spreadsheet into Access 2K using the macro command Transferspreadsheet. However, the file I am importing (over which I have no control) contains some records...
1
by: Geoff Jones | last post by:
Hi I have a question which I hope somebody can answer. I have written a VB application with which I want to import an Excel file, analyze the data within it and do some calculations. There are...
2
by: Snozz | last post by:
The short of it: If you needed to import a CSV file of a certain structure on a regular basis(say 32 csv files, each to one a table in 32 databases), what would be your first instinct on how to...
5
by: hharriel | last post by:
Hi, I am hoping someone can help me with an issue I am having with excel and ms access. I have collected data (which are in individual excel files) from 49 different school districts. All...
28
by: kkadakia | last post by:
I get a daily excel file for a entire month which I want to transfer into Access at the end of the month. So, there are around 20-25 excel files I get by the end of the month, and I would like to...
1
by: thadson | last post by:
Hi, I'm trying to import specific cells from MS Excel 2000 spreadsheets to MS Access 2000 tables then move the spreadsheets to a different directory. I'm very new to this and I'm having trouble...
3
by: HistoricVFP | last post by:
Hello, I’ve been given the task of importing .dbf files from a very old version of Visual FoxPro (version 2.1) into Access (2003). When I import the data straight to Access it errors with: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.