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!! - Private Sub Command35_Click()
-
On Error GoTo Err_Command0_Click
-
Dim appExcel As Object
-
Dim workBook As Object
-
Dim workSheet As Object
-
Dim i As Integer
-
Dim FdrID As Integer
-
Dim dbs_curr As Database
-
Dim records As Recordset
-
Dim sqlStatement As String
-
Dim message As String
-
Set dbs_curr = CurrentDb
-
-
-
' Open an existing spreadsheet
-
Set appExcel = GetObject("S:\Workgroups\file1.xls (This one works)
-
GetObject ("S:\Workgroups\file2.xls (This one doesnt work currently)
-
GetObject ("S:\Workgroups\File3.xls (This one doesnt work currently)
-
-
' Don't show spreadsheet on screen
-
appExcel.Application.Visible = False
-
-
Set workSheet = appExcel.Worksheets(1)
-
-
i = 2
-
While workSheet.Cells.Range("A" & i & ":A" & i).Value <> ""
-
sqlStatement = "SELECT GetData.* FROM GetData WHERE PDArea IS NULL"
-
Set records = dbs_curr.OpenRecordset(sqlStatement, dbOpenDynaset, dbSeeChanges, dbOptimistic)
-
records.AddNew
-
records!PDArea = workSheet.Cells.Range("A" & i & ":A" & i).Value
-
records!Company = workSheet.Cells.Range("B" & i & ":B" & i).Value
-
records!Supervisor = workSheet.Cells.Range("C" & i & ":C" & i).Value
-
records!GF = workSheet.Cells.Range("D" & i & ":D" & i).Value
-
records!Foreman = workSheet.Cells.Range("E" & i & ":E" & i).Value
-
records!current = workSheet.Cells.Range("F" & i & ":F" & i).Value
-
records!ReportedIllnesses = workSheet.Cells.Range("G" & i & ":G" & i).Value
-
records!Sick = workSheet.Cells.Range("H" & i & ":H" & i).Value
-
records!Date = workSheet.Cells.Range("I" & i & ":I" & i).Value
-
records.Update
-
-
i = i + 1
-
Wend
-
-
-
' Release objects
-
Set workSheet = Nothing
-
Set workBook = Nothing
-
Set appExcel = Nothing
-
-
MsgBox "Done"
-
-
Exit_Command0_Click:
-
Exit Sub
-
-
Err_Command0_Click:
-
MsgBox Err.Description
-
Resume Exit_Command0_Click
-
-
End Sub
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. - Private Sub ExcelImport()
-
Dim appExcel As Object
-
Dim workBook As Object
-
Dim workSheet As Object
-
Dim i As Integer
-
Dim FdrID As Integer
-
Dim dbs_curr As Database
-
Dim records As Recordset
-
Dim sqlStatement As String
-
Dim message As String
-
Dim rsImport As DAO.Recordset
-
-
On Error GoTo ExcelImport_Error
-
-
Set dbs_curr = CurrentDB
-
-
Set rsImport = CurrentDB.OpenRecordset("tblExcelSources", dbOpenSnapshot)
-
-
' Loop through excel filepaths stored in the table 'tblExcelSources'
-
Do While Not rsImport.EOF
-
rsImport.MoveFirst
-
-
Do While Not rsImport.EOF
-
' Open an existing spreadsheet (full file path stored in tblExcelSources.FileName)
-
Set appExcel = GetObject(rsImport!Filename)
-
-
' Don't show spreadsheet on screen
-
appExcel.Application.visible = False
-
-
Set workSheet = appExcel.Worksheets(1)
-
-
i = 2
-
While workSheet.Cells.Range("A" & i & ":A" & i).Value <> ""
-
sqlStatement = "SELECT GetData.* FROM GetData WHERE PDArea IS NULL"
-
Set records = dbs_curr.OpenRecordset(sqlStatement, dbOpenDynaset, dbSeeChanges, dbOptimistic)
-
records.AddNew
-
records!PDArea = workSheet.Cells.Range("A" & i & ":A" & i).Value
-
records!Company = workSheet.Cells.Range("B" & i & ":B" & i).Value
-
records!Supervisor = workSheet.Cells.Range("C" & i & ":C" & i).Value
-
records!GF = workSheet.Cells.Range("D" & i & ":D" & i).Value
-
records!Foreman = workSheet.Cells.Range("E" & i & ":E" & i).Value
-
records!Current = workSheet.Cells.Range("F" & i & ":F" & i).Value
-
records!ReportedIllnesses = workSheet.Cells.Range("G" & i & ":G" & i).Value
-
records!Sick = workSheet.Cells.Range("H" & i & ":H" & i).Value
-
records!Date = workSheet.Cells.Range("I" & i & ":I" & i).Value
-
records.Update
-
-
i = i + 1
-
Wend
-
-
' Release objects
-
Set workSheet = Nothing
-
Set workBook = Nothing
-
Set appExcel = Nothing
-
-
rsImport.MoveNext
-
Loop
-
Loop
-
Set rsImport = Nothing
-
MsgBox "Done"
-
-
On Error GoTo 0
-
Exit Sub
-
-
ExcelImport_Error:
-
-
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ExcelImport()"
-
-
End Sub
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: - 'First, set a Reference to the Microsoft Office XX.X Object Library
-
-
Dim strButtonCaption As String
-
Dim strDialogTitle As String
-
Dim varItem As Variant
-
-
'Define your own Captions if necessary
-
strButtonCaption = "Select .xls"
-
strDialogTitle = "Select Files to Process"
-
-
With Application.FileDialog(msoFileDialogFilePicker)
-
With .Filters
-
.Clear
-
.Add "Excel Woorkboks", "*.xls" 'Allow only Excel Files
-
End With
-
'The Show Method returns True if 1 or more files are selected
-
.AllowMultiSelect = True 'Critical Line
-
.FilterIndex = 1
-
.ButtonName = strButtonCaption
-
.InitialFileName = vbNullString
-
.InitialView = msoFileDialogViewSmallIcons
-
.Title = strDialogTitle
-
If .Show Then
-
For Each varItem In .SelectedItems
-
'Process varItem which will contain the Absolute Path to the .xls
-
'...
-
Next varItem
-
End If
-
End With
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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....
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |