I have put together some code in VBA which exports data from an excel sheet to a Data Base, this code (below) currently just inserts the data, but I would like to insert it based on a key (the date). So if the date already exists then in the DB the record associated with that date is replaced, if the date does not already exist, then it is inserted as usual.
Current code:
Sub SendData(AB As String, CD As String, EF As String, GH As String, IJ As String, KL As Double, MN As Double, PQ As Double, RS As Double)
Dim Date As String, Entity As String, area As String, unit As String, name As String, surname As String
Dim day As Double, weekly As Double, month As Double, year As Double
Dim objConn As ADODB.Connection
Set objConn = New ADODB.Connection
objConn.ConnectionString = "Provider=SQLOLEDB;Data Source=source;Initial Catalog=Title;Integrated Security=SSPI"
objConn.Open
Set objRec = New ADODB.Recordset
Date = Format(Range("date").Value, "YYYY-MM-DD")
Entity = AB
area = CD
unit = EF
name = GH
surname = IJ
daily = KL
weekly = MN
month = PQ
year = RS
StrSQL = "insert into table_name values ('" & Date & "', '" & Entity & _
"','" & area & "','" & unit & "','" & name & "','" & surname & "'," & daily & "," & weekly & "," & month & "," & year & ")"
Set objRec = objConn.Execute(StrSQL)
objConn.Close
Set objConn = Nothing
end sub
I am a bit confused where to place the key... Thanks
sql joinsyou can do a join for the update and then another for the insert, if you are doing an entire range, if not use something likeselect count(ID) from [table_name] where [date_value]='your date'and use an if statement based on the resultant recordsetsrecordcountor do aselect [date_value] from [table_name]of the destination table at the start of the proc in a separate rst and use.findto see if it's there..executeand.openrecordsetand what you are actually doing in your codeADODB.Recordset. In recordset you could search for date key, then add new or update existing one, depending on the result of search.