473,789 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

anoble1
245 New Member
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, 419 views)
Aug 28 '09 #1
2 2249
Megalog
378 Recognized Expert Contributor
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 Recognized Expert Expert
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
3419
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 the last field, it only checks the top few 'cells' to see if there is any data, if not, the field is not imported). How do I 'force' Access to import the field, regardless if there is data in the top of the field or not? For instance, I might...
9
4036
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 need to input this in an Access database, where I do a comparison with the Actual cost. The table “TblBudget” in Access is made of 4 fields, namely: (1) CostElement (2) CostCenter (3) Month (4) Amount$. At the moment this method is very cumbersome....
2
3612
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 that are "dirty" i.e. the field contents do not comply with the expected format (date/time) and they end up in a seperate table of import errors. (The records in "error" are actually empty fields.) This is a regular event and I do not want to...
1
2460
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 in fact five sheets in the Excel file. My original idea was to import the file into access and create a database file; which I did and worked beautifully. It generated five tables in the database as expected. However, I then thought why not...
2
3185
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 set this up so as to do it reliably and minimize overhead? There are currently no constraints on the destination table. Assume the user or some configuration specifies the database name, server name, and filename+fullpath. The server is SQL...
5
3177
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 districts have used the same excel template and populated the same 32 data fields (columns). I created one large excel file from all 49 files which gives me a master table of 60,000 or so records. I have tried to import this master table into access...
28
19253
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 import them into Access using a button. The data contained in the excel files is similar, so there should no formatting issues while importing. I searched through the forums and found the code by mmccarthy for importing excel files. I tried using...
1
5164
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 to implement this. I have worked out so far the code to import certain cells into 1 table, but I do not know how to import some other cells into another tables so the data would be connected and remain together. So lets say that I have 2 tables...
3
5176
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: External table is not in the expected format. (Error 3274) I’ve tried the following but it did not work: “To import data from a FoxPro database, use the Microsoft Visual FoxPro ODBC driver. To do so, follow these steps: 1. Click Start, and then...
0
9661
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10403
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5414
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4087
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2904
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.