Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:50:11+00:00 2026-06-05T21:50:11+00:00

Hi friends i am working on export excel rows to Sql Server 2008 Table

  • 0

Hi friends i am working on export excel rows to Sql Server 2008 Table
in that way i am checking the row already exist in table or not

my table has

sap_code
depot
size
entry_date

if table exist that record skip that row and check next row of excel with table

here goes my working code

' ===== Export Using ADO =====

Function ExportRangeToSQL(ByVal sourceRange As Range, _
    ByVal conString As String, ByVal table As String) As Integer

    On Error Resume Next

    ' Object type and CreateObject function are used instead of ADODB.Connection,
    ' ADODB.Command for late binding without reference to
    ' Microsoft ActiveX Data Objects 2.x Library

    ' ADO API Reference
    ' http://msdn.microsoft.com/en-us/library/ms678086(v=VS.85).aspx

    ' Dim con As ADODB.Connection
    Dim con As Object
    Set con = CreateObject("ADODB.Connection")

    con.ConnectionString = conString
    con.Open

    ' Dim cmd As ADODB.Command
    Dim cmd As Object
    Set cmd = CreateObject("ADODB.Command")

    cmd.CommandType = 1             ' adCmdText

       ' Dim rst As ADODB.Recordset
    Dim rst As Object
    Set rst = CreateObject("ADODB.Recordset")

    With rst
        Set .ActiveConnection = con
        .Source = "SELECT * FROM " & table
        .CursorLocation = 3         ' adUseClient
        .LockType = 4               ' adLockBatchOptimistic
         .CursorType = 1             ' adOpenKeyset
        .CursorType = 0             ' adOpenForwardOnly
        .Open

        ' Do While Not .EOF
        '    .MoveNext
        ' Loop

        ' Column Mappings

        Dim tableFields(100) As Integer
        Dim rangeFields(100) As Integer

        Dim exportFieldsCount As Integer
        exportFieldsCount = 0

        Dim col As Integer
        Dim index As Integer

        For col = 1 To .Fields.Count - 1
            index = Application.Match(.Fields(col).Name, sourceRange.Rows(1), 0)
            If index > 0 Then
                exportFieldsCount = exportFieldsCount + 1
                tableFields(exportFieldsCount) = col
                rangeFields(exportFieldsCount) = index
            End If
        Next

        If exportFieldsCount = 0 Then
            ExportRangeToSQL = 1
            Exit Function
        End If

        ' Fast read of Excel range values to an array
        ' for further fast work with the array

        Dim arr As Variant
        arr = sourceRange.Value

        ' Column names should be equal
        ' For col = 1 To exportFieldsCount
        '     Debug.Print .Fields(tableFields(col)).Name & " = " & arr(1, rangeFields(col))
        ' Next

        ' The range data transfer to the Recordset

        Dim row As Long
        Dim rowCount As Long
        rowCount = UBound(arr, 1)


        Dim val As Variant

        For row = 2 To rowCount

        ' Testing the Ledger data to insert
        Dim qu As String
        Dim br, de, si, da As String
       br = arr(row, rangeFields(1))  ' sap_code from excel
       de = arr(row, rangeFields(2)) ' depot from excel
       si = arr(row, rangeFields(3)) ' size from excel
       da = arr(row, rangeFields(5)) ' entry_date from excel

     Set con = CreateObject("ADODB.Connection")

    con.ConnectionString = conString
    con.Open


      Dim rstTest As ADODB.Recordset
      Set rstTest = New ADODB.Recordset
      With rstTest
       .CursorLocation = adUseClient
       .Open "select TOP 1 sap_code, depot, size, entry_date from openstock where " + "sap_code='" + br + "' and depot='" + de + "' and size='" + si + "' and entry_date='" + da + "' ORDER BY id DESC", con, adOpenStatic, adLockBatchOptimistic, adCmdText
  MsgBox "SAP_CODE" & br & "Depot" & de & "Size" & si & "entry_date" & da & "Duplicate Entry Not Entered into Database"
         If br = rstTest.Fields("sap_code").Value And _
            de = rstTest.Fields("depot").Value And _
            si = rstTest.Fields("size").Value And _
            da = rstTest.Fields("entry_date").Value Then


            Else

      End With  **NOte:  Error showing here as End With with out With**
            .AddNew
            For col = 1 To exportFieldsCount
                val = arr(row, rangeFields(col))
                If IsEmpty(val) Then
                Else
                    .Fields(tableFields(col)) = val
                End If
            Next
            End If
        Next   **NOte: Problem showing here as Next with out FOR**

        .UpdateBatch

    End With

    rst.Close
    Set rst = Nothing


    con.Close
    Set con = Nothing

    ExportRangeToSQL = 0

End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:50:12+00:00Added an answer on June 5, 2026 at 9:50 pm

    Suggestion: Always indent your code. So even if you look at the code say 6 months down the line, you will know what the code does. Indentation also helps you catch errors which occur as it happened in the code above

    Here is an example

    Sub Sample()
        For i = 1 to 5
        For j = 1 to 10
        For k = 1 to 7
        If a = 10 then
        End If
        Next
        Next
        Next
    End Sub
    

    The same code can be written as

    Sub Sample()
        For i = 1 to 5
            For j = 1 to 10
                For k = 1 to 7
                    If a = 10 then
    
                    End If
                Next
            Next
        Next
    End Sub
    

    Another suggestion (it is not mandatory though) For a better understanding where does a For loop ends, it is advisable to write Next as say Next i.

    So the above code can be further improved to

    Sub Sample()
        For i = 1 to 5
            For j = 1 to 10
                For k = 1 to 7
                    If a = 10 then
    
                    End If
                Next k
            Next j
        Next i
    End Sub
    

    If you implement the above suggestion, you will notice that this section of your code

          With rstTest
           .CursorLocation = adUseClient
           .Open "select TOP 1 sap_code, depot, size, entry_date from openstock where " + "sap_code='" + br + "' and depot='" + de + "' and size='" + si + "' and entry_date='" + da + "' ORDER BY id DESC", con, adOpenStatic, adLockBatchOptimistic, adCmdText
      MsgBox "SAP_CODE" & br & "Depot" & de & "Size" & si & "entry_date" & da & "Duplicate Entry Not Entered into Database"
             If br = rstTest.Fields("sap_code").Value And _
                de = rstTest.Fields("depot").Value And _
                si = rstTest.Fields("size").Value And _
                da = rstTest.Fields("entry_date").Value Then
    
    
                Else
    
          End With  **NOte:  Error showing here as End With with out With**
                .AddNew
                For col = 1 To exportFieldsCount
                    val = arr(row, rangeFields(col))
                    If IsEmpty(val) Then
                    Else
                        .Fields(tableFields(col)) = val
                    End If
                Next
                End If
            Next   **NOte: Problem showing here as Next with out FOR**
    

    Solution: Above code can be re-written as

    For row = 2 To rowCount
        '
        '
        '
        With rstTest
            .CursorLocation = adUseClient
            .Open "select TOP 1 sap_code, depot, size, entry_date from openstock where " + _
            "sap_code='" + br + "' and depot='" + de + "' and size='" + si + _
            "' and entry_date='" + da + "' ORDER BY id DESC", con, adOpenStatic, _
            adLockBatchOptimistic, adCmdText
    
            MsgBox "SAP_CODE" & br & "Depot" & de & "Size" & si & "entry_date" & da & _
            "Duplicate Entry Not Entered into Database"
    
            If br = rstTest.Fields("sap_code").Value And _
                    de = rstTest.Fields("depot").Value And _
                    si = rstTest.Fields("size").Value And _
                    da = rstTest.Fields("entry_date").Value Then
            Else
               '~~> Removed End With from here
               'End With  **NOte:  Error showing here as End With with out With**
                .AddNew
                For col = 1 To exportFieldsCount
                    val = arr(row, rangeFields(col))
                    If IsEmpty(val) Then
                    Else
                        .Fields(tableFields(col)) = val
                    End If
                Next col
            End If
        End With '<~~ Pasted it here
    Next row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A groups of friends are working on a little game that would listen to
I'm working on a site that is using Facebook authentication and friends, but I'm
I'm working on an app that's using System.Net.Mail.MailAddress and friends for sending emails. Does
I'm working on developing a Facebook application that accesses the user's friends list. Since
I'm working with an intermediate join table in Core Data similar to the friends
Hi Friends I am working with a Full Calender application, in that,while i click
I'm working on a friends list type function that will display a user's friends
Hello my smart friends i have this contact form that i am working on.
I'm working with some friends on a small private mini MMORPG game server, and
I have a working WAMP environment (Apache Friends). I decided to try Subversion and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.