Export from Excel to Access

FTM

New Member
Joined
Mar 28, 2013
Messages
40
Hello everyone,
I have a problem that I've been working on for quite a while now. I have recieved a lot of help from members of this board but I'm still stuck. Here is the spreadsheet...

PartDescription Pack QuantityInjection Molded PartsPost Injection Processed PartsLess that full boxes
24831/3 Basket728 0
24802/3 Basket4033 0
2468Bottom Freezer Basket1858 0
2438Meat Pan22176 0
2411Vegetable Pan Frame2233 0
2586-1Vegetable Pan Frame20 300
25543/4 Basket4026j0
2464Full Basket2224 0
2561Bottom Freezer Basket1822 0
2549Meat Pan22130 0
2456Vegetable Pan Frame204 0
2586-2Vegetable Pan Frame20 200
26751/3 Basket929 0
26742/3 Basket3624 0
2676Bottom Freezer Basket 2432 0
2666Vegetable Pan Frame10145I0
2586-3Vegetable Pan Frame20 280
1752Ice Box Door Outer1418 0
1818Ice Box Door Outer14 00
1753Ice Box Door Inner1945 0
1817Ice Box Door Inner19 00
1405Air Tower35225 0

<colgroup><col style="width: 48pt;" width="64"> <col style="width: 103pt; mso-width-source: userset; mso-width-alt: 5010;" width="137"> <col style="width: 48pt;" width="64"> <col style="width: 50pt; mso-width-source: userset; mso-width-alt: 2450;" width="67"> <col style="width: 52pt; mso-width-source: userset; mso-width-alt: 2523;" width="69"> <col style="width: 48pt;" width="64"> <tbody>
</tbody>


I have this script that somebody wrote for me and I've fiddled with, a lot...

Sub Export_Inventory_Report_to_Access_29()
'
' Test2 Macro

Dim rng As Range
Dim s(1) As String
Dim SQL As String
Dim i As Long
Dim db As Object 'DAO.Database
Dim rs As Object 'DAO.Recordset
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
Sheets("Inventory Report").Select
Range("A2").Select

'//Open Home Workstation Database

Set db = DBEngine(0).OpenDatabase("C:\users\Mann\My Documents\Production Report and Inventory Management\Inventory Report and Machine Utilization Database\Inventory Report.Accdb")

Set rs = db.OpenRecordset("Inventory Report")

'//Range of values to update

Set rng = Sheet1.Range("A2:H26").CurrentRegion

'//First Half of SQL Statement

s(0) = "INSERT INTO Table1 (Part, Description, Pack Quantity, Box Count, Odds, Actual Inventory) VALUES ( "

'//Loop rows and create rest of the SQL Statement, then run SQL commands

For i = 2 To rng.Rows.Count

If Len(rng.Cells(i, 4).Value) <> 0 Then

rs.AddNew
rs.Fields("Part").Value = rng.Rows(i).Cells(1).Value
rs.Fields("Description").Value = rng.Rows(i).Cells(2).Value
rs.Fields("Pack Quantity").Value = rng.Rows(i).Cells(3).Value
rs.Fields("Box Count").Value = rng.Rows(i).Cells(4).Value
rs.Fields("Odds").Value = rng.Rows(i).Cells(6).Value
rs.Fields("Actual Inventory Total").Value = rng.Rows(i).Cells(7).Value
rs.Update

Else
Exit For

End If
Next i

'//Close database
'On Error Move Next

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing

End Sub

Now...

What I want to do is to loop through the spreadsheet and copy data from the spreadsheet to the MS Access Database if there is data <> 0 in the D Column, cell by cell. I want to be able to loop past cells that are empty and formatted in the dark blue.

Truth be told I've written four different scripts that loop through the afforementioned script and then exits the loop when the contents of the D Column = 0.

I can move the Row labeled 1752 to the database. The Script that I hacked on that attempts the same move with 1753 ether copies 1752 or doesn't do anything. Same thing with the row labeled 1405.

So...

Who has time and the inclination to assist me in this endevor?

Thanks in advance for your assistance.

Regards,

FTM
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Why not assign a value to empty cells or perhaps assign a format to the columns so it can search and pull data?
 
Upvote 0
Why not assign a value to empty cells or perhaps assign a format to the columns so it can search and pull data?

I did exactly as you suggest thinking that I would write an MS Access query to go through the database and blow qaway all the records containing bad data. Then I thought what a hassle that would be as the database got bigger and bigger. So I decided to stick with data integrity on the spreadsheet side of the problem.

I'm thinkingt that what I'll end up doing is write a script using nested loops, "For i = 2483 and j <> 0..." something like that.

Unless anyone else has better ideas.

Thanks in advance for your help.

Regards,

FTM
 
Upvote 0
K...

Here's what I did. It Works, in the event that anyone needs a reference. I'm sure that somebody that knows what they're doing can streamline the process.

Thanks everyone for the assistance :).

Sub Export_Inventory_Report_to_Access_IMPPs()
'
' Export_Inventory_Report_to_Access Macro
'
' Declare variables...
Dim FinalRow As Integer
'Dim FinalCol As Integer
Dim i As Integer
' Dim j As Integer
' Dim k As Integer
' Dim l As Integer
' Dim Press As String
' Dim Shift As String
Dim Sheet As String
Dim rng As Range
Dim s(1) As String
Dim SQL As String
Dim db As Object 'DAO.Database
Dim rs As Object 'DAO.Recordset

Application.ScreenUpdating = False
'//Open Laptop Database

'Set db = DBEngine(0).OpenDatabase("C:\users\220042816\My Documents\Production Report and Inventory Management\Inventory Report and Machine Utilization Database\Inventory Report.Accdb")

'//Open Home Workstation Database

Set db = DBEngine(0).OpenDatabase("C:\users\Mann\My Documents\Production Report and Inventory Management\Inventory Report and Machine Utilization Database\Inventory Report.Accdb")

Set rs = db.OpenRecordset("Inventory Report")

' Calculate the length of the form...
FinalRow = (Cells(Rows.Count, 2).End(xlUp).Row)
'FinalCol = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To FinalRow Step 1

'Select cells to examine based on For/Next loop Step...

Sheets("Inventory Report").Select
Cells(i, 4).Select

'Check for Valid Data...

If ActiveCell <> 0 Then

'Transfer data from the Inventory Report Spreadsheet to the Inventory Report and Machine Utilization Database
'Inventory Report Table...
'//Range of values to update

Set rng = Sheet1.Range("A2:J26").CurrentRegion

'//First Half of SQL Statement

s(0) = "INSERT INTO Table1 (Part, Description, Pack Quantity, Box Count, Odds, Actual Inventory, Inventory Target) VALUES ( "

rs.AddNew
rs.Fields("Part").Value = rng.Rows(i).Cells(1).Value
rs.Fields("Description").Value = rng.Rows(i).Cells(2).Value
rs.Fields("Pack Quantity").Value = rng.Rows(i).Cells(3).Value
rs.Fields("Box Count").Value = rng.Rows(i).Cells(4).Value
rs.Fields("Odds").Value = rng.Rows(i).Cells(6).Value
rs.Fields("Actual Inventory Total").Value = rng.Rows(i).Cells(7).Value
rs.Fields("Inventory Target").Value = rng.Rows(i).Cells(10).Value
rs.Update

End If

Next i

rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
Application.ScreenUpdating = True
'
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,106
Messages
6,128,863
Members
449,473
Latest member
soumyahalder4

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