I have a sheet that contains a list of zip codes, I need to update a flag for those zip codes in the database. Now the zipcodes are in 3 columns so I am able to select all the zip codes & store them into Variant. Instead of counting the zip codes & running SQL statements for each one, I want to pass the Variant directly to SQL & execute a stored procedure that could do the job. I don't want to loop the SQL command 9 times for this dataset just want to pass the variant in one go.
Here's how my zipcode sheet looks like:
A B C
1 99999 88888 77777
2 55555 66666 44444
3 22222 11111 33333
I have created a macro that selects all the zip codes I'm storing them in Variant name zipArr.
Module1
Sub UpdateZip
Dim zipArr as Variant
Dim oCon as DBConnection
zipArr = Range("A1:C3").Select
oCon.UpdateToSQL(zipArr)
End Sub
Public Module
Public Sub DBConnection(arr as Variant)
Dim con As New ADODB.Connection
Dim cmd as New String
con.ConnectionString = "Provider=SQLOLEDB.1;" _
& "Server=(local);" _
& "Database=Demo;" _
& "Integrated Security=SSPI;"
con.Open
cmd.CommandText = "Exec upd_ZipCodes" & arr
cmd.Execute
con.close
set con = nothing
End Sub
Any suggestions are appreciated. I tested running update query but passed a value of 1 zip code & the change happened on SQL. Would be great if that is possible in bulk. I do have a query from SQL Side but I'm trying to resolve this first.