SQL Insert Yields Fewer Records Than Anticipated

goss

Active Member
Joined
Feb 2, 2004
Messages
372
Hi all,

My SQL INSERT snippet is only yielding 28,739 records. However there are 421,956 record on my Excel worksheet:
Code:
    'Load to Access table
        'SQL
            strSQL9 = "INSERT INTO tmp_Table SELECT * FROM [tmpf$] IN '" _
            & ThisWorkbook.FullName & "' 'Excel 8.0;'"
            
        'Do it
            cnt.Execute (strSQL9)

Any idea why this may be?
Full code below
thx
w
Code:
Option Explicit

Sub ImportRecords()
    '
    'Imports all records from the specified table
    'Uses Microsoft ActiveX Data Objects 2.7 Library
    '
    'Date       Developer       Action
    '---------------------------------------------
    '01/20/12   ws              Created
    '01/20/12   ws              Temorary worksheet delete process is commented out
   
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim wstmp As Worksheet
    Dim wstmpf As Worksheet
    Dim wsTitle As Worksheet
    Dim strDBPath As String
    Dim strDb As String
    Dim strDBTable As String
    Dim strDBPathFile As String
    Dim cnt As ADODB.Connection
    Dim rst1, rst2, rst3, rst4, rst5, rst6, rst7, rst8 As ADODB.Recordset
    Dim strSQL1, strSQL2, strSQL3, strSQL4, strSQL5, strSQL6, strSQL7, strSQL8, strSQL9 As String
    Dim stConn As String
    Dim strFormula As String
    Dim lngRows As Long
    
    'Initialize
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            .DisplayAlerts = False
        End With
        lngRows = 0
        strFormula = "=C2&""_""&A2"
      
    'Instantiate objects
        Set cnt = New ADODB.Connection
        Set rst1 = New ADODB.Recordset
        Set rst2 = New ADODB.Recordset
        Set rst3 = New ADODB.Recordset
        Set rst4 = New ADODB.Recordset
        Set rst5 = New ADODB.Recordset
        Set rst6 = New ADODB.Recordset
        Set rst7 = New ADODB.Recordset
        Set rst8 = New ADODB.Recordset
        Set wb = ThisWorkbook
        Set ws = wb.Worksheets("ImportRecs")
        Set wsTitle = wb.Worksheets("iTitle")
        
    'Temprary worksheets to handle data
        wb.Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "tmp"
        wb.Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "tmpf"
        Set wstmp = wb.Worksheets("tmp")
        Set wstmpf = wb.Worksheets("tmpf")

    'Get database string values
        With ws
            strDBPath = .Range("C4")
            strDb = .Range("C5")
            strDBTable = .Range("C6")
        End With
        
    ' Get the database name.
        strDBPathFile = strDBPath
        If Right$(strDBPathFile, 1) <> "\" Then strDBPathFile = strDBPathFile & _
            "\"
        strDBPathFile = strDBPathFile & strDb

    'Connection String
        stConn = "Provider=Microsoft.Jet.OLEDB.4.0;" _
        & "Data Source=" & strDBPathFile & ";"
    
    'SQL-statements to be executed.
        strSQL1 = " SELECT OrderID FROM Orders "
        strSQL2 = " SELECT CustomerID FROM Orders "
        strSQL3 = " SELECT OrderDate FROM Orders "
        strSQL4 = " SELECT ShipName FROM Orders "
        strSQL5 = " SELECT CompanyName" & _
                 " FROM Customers " & _
                 " LEFT OUTER JOIN Orders " & _
                 " ON Customers.CustomerID " & _
                 " = Orders.CustomerID"
        strSQL6 = " SELECT ShipPostalCode FROM Orders "
        strSQL7 = " SELECT ContactName" & _
                 " FROM Customers " & _
                 " LEFT OUTER JOIN Orders " & _
                 " ON Customers.CustomerID " & _
                 " = Orders.OrdersID"
        strSQL8 = " SELECT Status FROM Orders "
        
    With cnt
        .Open (stConn) 'Open the connection.
        .CursorLocation = adUseClient 'Necessary to disconnect the recordset.
    End With
        
    With rst1
        .Open strSQL1, cnt 'Create the recordset.
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
    End With
    
    With rst2
        .Open strSQL2, cnt 'Create the recordset.
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
    End With
    
    With rst3
        .Open strSQL3, cnt 'Create the recordset.
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
    End With
    
    With rst4
        .Open strSQL4, cnt 'Create the recordset.
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
    End With
    
    With rst5
        .Open strSQL5, cnt 'Create the recordset.
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
    End With
    
    With rst6
        .Open strSQL6, cnt 'Create the recordset.
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
    End With
    
    With rst7
        .Open strSQL7, cnt 'Create the recordset.
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
    End With
    
    With rst8
        .Open strSQL8, cnt 'Create the recordset.
        Set .ActiveConnection = Nothing 'Disconnect the recordset.
    End With

        
    'Copy recordsets
        With wstmp
            .Cells(2, 1).CopyFromRecordset rst1
            .Cells(2, 2).CopyFromRecordset rst2
            .Cells(2, 3).CopyFromRecordset rst3
            .Cells(2, 4).CopyFromRecordset rst4
            .Cells(2, 5).CopyFromRecordset rst5
            .Cells(2, 6).CopyFromRecordset rst6
            .Cells(2, 7).CopyFromRecordset rst7
            .Cells(2, 8).CopyFromRecordset rst7
        End With

    'Add Customer Site (Cust_Site)
        With wstmp
            .Range("D1").EntireColumn.Insert
            lngRows = .Cells(Rows.Count, 1).End(xlUp).Row 'Find last Row
            .Range("D2:D" & lngRows).Formula = strFormula 'Create Cust_Site
        End With
        
    'Add Header Row
        wsTitle.Range("D8:L8").Copy Destination:=wstmp.Range("A1")

    'Copy data to sheet tmpf for final export to Access
        wstmp.Range("A1:I" & lngRows).Copy
        wstmpf.Range("A1").PasteSpecial (xlPasteValues)
        wstmpf.Range("A1").PasteSpecial (xlPasteFormats)

    'Format sheet tmpf
        wstmpf.Activate
        ActiveWindow.Zoom = 75
        Rows("2:2").Select
        ActiveWindow.FreezePanes = True
        wstmpf.Columns("A:I").EntireColumn.AutoFit 'RAD
        
    'Load to Access table
        'SQL
            strSQL9 = "INSERT INTO Pegging_Table SELECT * FROM [tmpf$] IN '" _
            & ThisWorkbook.FullName & "' 'Excel 8.0;'"
            
        'Do it
            cnt.Execute (strSQL9)
    
    'Tidy up
        'Delete temporary worksheets
        'After RAD, remove comments
'            wb.Worksheets.Delete ("tmp")
'            wb.Worksheets.Delete ("tmpf")
            
        'Destroy Objects
            Set wb = Nothing
            Set ws = Nothing
            Set wsTitle = Nothing
            Set wstmp = Nothing
            Set wstmpf = Nothing
            Set cnt = Nothing
            Set rst1 = Nothing
            Set rst2 = Nothing
            Set rst3 = Nothing
            Set rst4 = Nothing
            Set rst5 = Nothing
            Set rst6 = Nothing
            Set rst7 = Nothing
            Set rst8 = Nothing
        
        'Reset Excel environment
            With Application
                .ScreenUpdating = False
                .Calculation = xlCalculationManual
                .DisplayAlerts = False
            End With
        
End Sub
 
Okay - what version of Access are you using? What version of ADO are you using? Is it XL2007 or XL2010. I can try to reproduce the case here - I assume the datatype doesn't matter though if you have mixed data types in Excel it's sometimes a problem (i.e., a column that starts out with text then has numbers, or vice versa).
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Nevermind. I see you are using ADO 2.7 library.

This looks like a problem:
Code:
            strSQL9 = "INSERT INTO Pegging_Table SELECT * FROM [tmpf$] IN '" _
            & ThisWorkbook.FullName &[COLOR="Red"] "' 'Excel 8.0;'"[/COLOR]

Excel 8.0 workbooks have on 65656 rows in them. If you are working with xlsx files you should not use Jet 4.0 at all. You need the ACE provider (you can still use the 2.7 ADO library though I believe there is also a new one on that score too (it would be 6.0, I think, if you have it).

I've never tried to query an xlsx workbook - I would search for an example on the web. Maybe you just need to specify Excel 12.0 instead of Excel 8.0, and use the Ace provider instead of the Jet provider.
 
Upvote 0
Hi Xenou,

Both Access and Excel are 2007 version (32 bit)
The datatype for all fields is text (I used ADOX to get schema from the tables I inherited)

I am using ADO 2.7 for handling the SQL between Excel and Access.

thx
w
 
Upvote 0
Alright. I would probably run this from Access and pull into the database rather than from Excel and push into the database - offhand, I've forgotten how to do this the way you have it set up. I'll have to get back to you on that.

Are you sure you can't skip the Excel piece. If you are going from one database to another why stop in Excel?

Note:
here's an example of the proper connection string for an xlsx file:
http://vbadud.blogspot.com/2010/08/how-to-connect-xlsx-file-excel-workbook.html
 
Upvote 0
Hi Xenou,

I am more accustomed with Excel. I would like to use it as an interface to all of our maintenance procedures and logs that we perform daily/weekly/monthly/annually.

Will incorporate components from Essbase Spreadsheet Toolkit, potentially HFM and FDQM (Upstream) as well. Not sure if those components can be acheived in Access or not.

thx
w
 
Upvote 0
Okay. Got too late for me to try anything tonight. You are moving this data from Excel to Access currently, right? -- That is, if you could get it to work.

I'm not familiar with Essbase products at all so I've no opinion on that. Some might find that refreshing for a change ;)
 
Upvote 0
Hi Xenou,

Thanks.

You are correct for the first step of the process to get data from Linked tables in Access to Excel, perform some transformations load back to Access. Will need to load Essbase, FDQM, or HFM down the road.

The tables are linked to Oracle tables so I am wondering if there is a better way to query the Oracle tables directly, it seems the linked tables may be causing an ODBC Connection error:
https://www.mrexcel.com/board/index.php?threads/609368/

I just posted my error in trying to connect to the Oracle tables with vba/ado:
https://www.mrexcel.com/board/index.php?threads/609568/

thx
w
 
Upvote 0
Hi Xenou,

My apologies, I did not see your comment regarding ADO 2.7 and the ACE driver until late Monday night.

I changed my ADO Connection strings to
Code:
"Provider=Microsoft.ACE.OLEDB.12...."
Now the INSERT FROM worksheet returns the correct number of records :)

thx
w
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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