-1

I am exporting data in Excel table to SQL Server Database, If exists UPDATE else INSERT.

The following VBA code works well for exporting to ACCESS Database, BUT NOT TO SQL SERVER DATABASE TABLE.

Error Message appear :Invalid Use of Property for .Index and .Seek.

Please Help !!! Toh

Sub ExcelDataToSql ()

Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Dim lastrow As Long, o As Long

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.Open "Provider=SQLNCLI11;Server=***;Database=****;Trusted_Connection=yes;"
rs.CursorLocation = adUseServer
rs.Open "InventorySQL", cn, 1, 3, adCmdTableDirect

' Get Lastrow
Worksheets("InventoryEXCEL").Select
lastrow = Worksheets("InventoryEXCEL").Cells(rows.Count, 1).End(xlUp).Row
r = 2 ' the start row in the worksheet
For o = 2 To lastrow

    'Check For Duplicate In Database SQL
    With rs
        .Index = "PrimaryKey"
        .Seek Range("A" & r).Value

        If .EOF Then
            .AddNew            
            'If No Duplicate insert New Record
            rs.Fields("oPartno") = Range("A" & r).Value
            rs.Fields("oDesc") = Range("B" & r).Value
            rs.Fields("oCost") = Range("C" & r).Value
        .update
        Else          
            ' If Duplicate Found Update Existing Record
            rs.Fields("oDesc") = Range("B" & r).Value
            rs.Fields("oCost") = Range("C & r).Value
            .Update
        End If
    End With

Next o
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    MsgBox "Posting Completed"
End Sub

. Index = "PrimaryKey" --- Sysntax Error : Invalid Use of Property .Seek Range ("A" & r).Value Sysntax Error :

2
  • 1
    Why are r and mysal tagged here? SQL Server would seem a more appropriate tag if I have not misunderstood? Commented Nov 24, 2016 at 15:48
  • Thank you for your feedback ... this is my first post in stackoverflow ...will be more careful in future... My apology. Commented Nov 25, 2016 at 11:42

2 Answers 2

1

Reference:Seek Method and Index Property Example (VB)

The MSDN example passes an Array as the first parameter.

rstEmployees.Seek Array(strID), adSeekFirstEQ

The first parameter's name os KeyValues which also implies an array

enter image description here

I would try this first

.Seek Array(Range("A" & r).Value)

It might also be beneficial to use one of the SeekEnum value

enter image description here

Update: TOH the OP found that this was the relavent code snippet

MSDN also suggest checking if the Provider supports .Index and .Seek

If rstEmployees.Supports(adIndex) And rstEmployees.Supports(adSeek) Then
Sign up to request clarification or add additional context in comments.

17 Comments

Thank you for your help. It seems that the Provider does not support .index and .seek property and method.
Thanks for the Update. I amended my answer.
Try your method .seek array(range("A" & r).valuye, seekEnum... Runtime Error 3251 - Current provider does not support the necessary interface for index functionality.
@Toh Do youknow about ConnectionStrins.Com? They have a complete list of providers.
Yes... I use the provider from ConnectionStrins.Com. How to know which provider provide the functionality for the .index and .seek. The connection currently used with no connection problem.
|
1

My Problem is resolved by work around.

Many resources indicated that Sql Providers do not support the index and seek function. So I avoid Index and Seek

I work around by importing the excel worksheet into Sql server as source table... thereafter Merge the target table with source table... if match UPDATE, if Not Match INSERT.

select * from InventoryTableSQL
select * from InventoryTableFromExcel

Merge InventoryTableSQL as T
using InventoryTableFromExcel as S
on t.oPartno = s.oPartno
when matched then
    update set t.oPartno = s.oPartno,
    t.oDesc = s.oDesc,
    t.oCost = s.oCost
when not matched by target then
    insert(oPartno, oDesc, oCost) values(s.oPartno, s.oDesc, s.oCost));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.