atuljadhavnetafim
Active Member
- Joined
- Apr 7, 2012
- Messages
- 341
- Office Version
- 365
- Platform
- Windows
Dear Expert,
I am using below code to uploade data in SQL table, and it is working fine,
i want to add one condition in this code, which is
before uploade data in SQL Table it will check whether customer id already exist or not in SQL Table (Customers), if customer id is already there in SQL Table then it remove and uploade rest the data,
I am using below code to uploade data in SQL table, and it is working fine,
i want to add one condition in this code, which is
before uploade data in SQL Table it will check whether customer id already exist or not in SQL Table (Customers), if customer id is already there in SQL Table then it remove and uploade rest the data,
Code:
Sub Button1_Click()
Dim conn As New ADODB.Connection
Dim iRowNo As Integer
Dim sCustomerId, sFirstName, sLastName As String
With Sheets("Sheet1")
'Open a connection to SQL Server
conn.Open "Provider=SQLOLEDB;Data Source=vcsql;Initial Catalog=CreditControl;Integrated Security=SSPI;"
'Skip the header row
iRowNo = 2
'Loop until empty cell in CustomerId
Do Until .Cells(iRowNo, 1) = ""
sCustomerId = .Cells(iRowNo, 1)
sFirstName = .Cells(iRowNo, 2)
sLastName = .Cells(iRowNo, 3)
'Generate and execute sql statement to import the excel rows to SQL Server table
conn.Execute "insert into dbo.Customers (CustomerId, FirstName, LastName) values ('" & sCustomerId & "', '" & sFirstName & "', '" & sLastName & "')"
iRowNo = iRowNo + 1
Loop
MsgBox "Congratulation Customers imported. Thanks Atul"
conn.Close
Set conn = Nothing
End With
End Sub
[\code]