Import One Excel Table - Different Parts into multiple access tables?

ExcelGirl1975

New Member
Joined
Nov 10, 2006
Messages
22
Hello!

Can I set up a datasheet in excel to be used to import different portions of the database into multiple access tables?

I'm thinking I could create several named ranges in excel (e.g. columns A-B in excel are my first named range, columns C-F are my second named range) and choose to import in Access from a named range.

And could I create a link between the excel datasheet named rangeed and the access tables and the if I need to import new data from the same named ranges (appending to my tables)?

Thanks!!

Tyra
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I have done something similar in that I import into Excel and then transfer the data into Access. I am doing a one for one situation but I am sure you could run this in successive VBA scripts and accomplish what you are looking to do.

Basically I have a worksheet called 830. I run a macro that takes a text file and puts the pertinent information into the worksheet. From there, I run the UpdateAccess830() routine.

The routine counts the rows of data,connects to the datasource(my access table),then copies the information one row at a time. Since I only have between 2 and 200 rows, it is very quick. When it is done, a message box reports the number of rows updated.

I have the import and the update attached to a button in the excel workbook so that I can just hit a button and go.

HTH.

*/
Sub UpdateAccess830()

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim lngRow As Long
Dim lngID
Dim j As Long
Dim sSQL As String
Dim LR As Integer
Dim Upd As Integer

Worksheets("830").Select

LR = Range("A" & RowS.Count).End(xlUp).Row
Upd = LR - 1

lngRow = 2

Do While lngRow <= LR

sSQL = "SELECT * FROM 830;"


Set cnn = New ADODB.Connection
MyConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source = S:\Public Folder\EDI for ****\EDITracker.accdb"
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With

Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, LockType:=adLockOptimistic

With rst
.AddNew
.Fields("Sequence") = Cells(lngRow, 1).Value
.Fields("Order#") = Cells(lngRow, 2).Value
.Fields("Buyer Code") = Cells(lngRow, 3).Value
.Fields("Vendor Code") = Cells(lngRow, 4).Value
.Fields("PO#") = Cells(lngRow, 5).Value
.Fields("Line #") = Cells(lngRow, 6).Value
.Fields("Part#") = Cells(lngRow, 7).Value
.Fields("UM") = Cells(lngRow, 8).Value
.Fields("Ship to") = Cells(lngRow, 9).Value
.Fields("ReleaseQty") = Cells(lngRow, 10).Value
.Fields("FRCST Qual") = Cells(lngRow, 11).Value
.Fields("FRCST Tmg") = Cells(lngRow, 12).Value
.Fields("Start Date") = Cells(lngRow, 13).Value
.Fields("End Date") = Cells(lngRow, 14).Value
.Fields("RAN#") = Cells(lngRow, 15).Value
.Fields("IssueDate") = Cells(lngRow, 16).Value
.Fields("ForecastQTY") = Cells(lngRow, 17).Value
.Fields("FirmQTY") = Cells(lngRow, 18).Value
.Fields("YearWK") = Cells(lngRow, 19).Value

rst.Update
End With



' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing

lngRow = lngRow + 1
Loop
MsgBox "You just updated " & Upd & " records"
End Sub
/*
 
Upvote 0
You are kind for sharing the script you used to perform this task, however, I am not very familiar with VBA tothe extent that I could adapt this script for my own personal use. (I'm assuming I would open up a Visual Basic window in the excel worksheet I am using and cut and paste the script in and substitute my field names?).

Would you or anyone else know how to do this from the "front end"

Thanks!
 
Upvote 0
I set up a form on one of the worksheets that selects the update type that I want to do. I have 5 different options. The forms can be created by using the insert controls. this lets you put in combo boxes, buttons etc. Buttons can then be linked to macros and the macros can call the vba routines.

There is an addin that you can get for Excel that puts a developers tab on Excel. I have found this most useful.

The vba code is pretty simple when you get into it. Dim is declaring a variable, some are required like the connections others you can just declare the name and let VBA take care of the rest.

The routine has a DO While loop which basically says to keep repeating until you get to the point where the row you are on is the last row.

Inside the do while loop I open my data source (this code is copied from other people so I will not take credit) then the with rst I update the Access table based on the SQL query with the information from the Excel spreadsheet. Cells(R,C).Value where R is row and C is column lets you walk through and put things in the right buckets.

The rst.Update writes the record to Access. Once the record is written the record set is written and I add one to the row then check to see if I am at the end.

I am not sure if this helps. The information that exists on this board is beyond amazing. I am sure there are other ways to get this done, I feel very confident with this method though.

Good luck.
 
Upvote 0
ExcelGirl,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
Here is some code that might help you.<o:p></o:p>
The trick is always to not make things more complex than needed.<o:p></o:p>
In this example the only things you have to look after are:<o:p></o:p>
<o:p> </o:p>
- take a good look at how the queries are written to understand how you can rewrite them for your situation<o:p></o:p>
- make sure your named ranges have column headers, this is a key thing to avoid really big trouble<o:p></o:p>
<o:p> </o:p>
You’ll see two variants for the same. One using <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:City w:st="on"><st1:place w:st="on">ADO</st1:place></st1:City> the other using the Access ‘do command’ structure. It’s really up to you which one to use. <st1:place w:st="on"><st1:City w:st="on">ADO</st1:City></st1:place> is faster and more structured, but as we are not talking about really big amounts of complex data, the Access native command will be more than sufficient. <o:p></o:p>
<o:p> </o:p>
In this example we have an Excel sheet with two named ranges, DataSetA and DataSetB.<o:p></o:p>
We want to write these to two tables, tblDataSetA and tblDataSetB.<o:p></o:p>
<o:p> </o:p>
tblDataSetA has two fields: Name, Description
tblDataSetB has two fields: Location, Country
<o:p> </o:p>
The first row of the SQL statement will be: Insert Into ( Name, Description )<o:p></o:p>
Now we have to ‘tell’ which values we want to insert.<o:p></o:p>
In this example the named range DataSetA has headers named Name and Description. It is not necessary to have the same names in your range as in your Access table, as long as you keep the same order. <o:p></o:p>
So the next line would be: Select Name, Description From DataSetA. Make sure you understand this. If the named range had headers like, Q and Z, where Q is the column with Descriptions and Z holding the Names. You have to write it like: Select Z, Q From DataSetA to get the right column into the desired field. Are you sure you understand this part?<o:p></o:p>
Ok, the last part of the SQL statement tells where to find the data, we use the IN operator to point to the Excel file and tell it is Excel, it will look like: <o:p></o:p>
In '" yourExcelFilePathAndName "' 'Excel 8.0;' <o:p></o:p>
<o:p> </o:p>
These are the basics to understand how the SQL is written.
One last thing, the principle used is that we are working in Access and pull data from Excel, and not working in Excel and pushing data to Access.<o:p></o:p>
This means you’ll have to create a module in Access. To run the code other than from the module, create a form with a command button. The problem is I don’t know your level of knowledge on this.<o:p></o:p>
<o:p> </o:p>
<o:p>Here are the SQL functions:</o:p>
<o:p>
Code:
<o:p>Public Function SQL_WriteDataSetA(ByVal sFilename As String) As String</o:p>
<o:p>'Pull data from range DataSetA in Excel and insert into tblDataSetA
SQL_WriteDataSetA = "Insert Into tblDataSetA ( Name, Description ) " _
                  & "Select Name, Description From DataSetA " _
                  & "In '" & sFilename & "' 'Excel 8.0;'"</o:p>
<o:p>End Function
Public Function SQL_WriteDataSetB(ByVal sFilename As String) As String</o:p>
<o:p>'Pull data from range DataSetB in Excel and insert into tblDataSetB
SQL_WriteDataSetB = "Insert Into tblDataSetB ( Location, Country ) " _
                  & "Select Location, Country From DataSetB " _
                  & "In '" & sFilename & "' 'Excel 8.0;'"</o:p>
<o:p>End Function </o:p>
</o:p>

The 'Native' version:
Code:
Public Sub PullDataFromExcel()
[COLOR=seagreen]'This is the code for your commandbutton[/COLOR]
Dim sYourSheet As String 'The full path and name of your Excelsheet
sYourSheet = [B][COLOR=darkred]"D:\YourFolder\YourFile.xls"[/COLOR][/B]
DoCmd.SetWarnings False
            'Execute the SQL statments
            DoCmd.RunSQL (SQL_WriteDataSetA(sYourSheet))
            DoCmd.RunSQL (SQL_WriteDataSetB(sYourSheet))
DoCmd.SetWarnings True
End Sub

And the ADO version:
Code:
Public Sub PullDataFromExcelADO()
[COLOR=seagreen]'The ADO variant needs a reference to the:
'Microsoft ActiveX Data Object 2.x library[/COLOR]
Dim oCn As New ADODB.Connection
Dim sYourSheet As String 'The full path and name of your Excelsheet
sYourSheet = [COLOR=darkred][B]"D:\YourFolder\YourFile.xls"
[/B][/COLOR]Set oCn = CurrentProject.Connection
With oCn
        .Execute (SQL_WriteDataSetA(sYourSheet))
        .Execute (SQL_WriteDataSetB(sYourSheet))
End With
End Sub

Maybe this is not complete. Why?<o:p></o:p>
If you run the code, it will insert data to the two tables. Over and over again. So maybe you only want to insert new or updated records. You could simply delete all data first before doing the insert, but most of the time this is not the way to go.<o:p></o:p>
This is where you could use temporary tables to write to from Excel and from there on updated or insert to the desired tables using the right SQL statement. But for now I guess you have enough info to get started.<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top