View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Rachel[_2_] Rachel[_2_] is offline
external usenet poster
 
Posts: 6
Default SQL update statement in excel vba

I am pulling my hair out with this one, any help would be great!

I have an excel spreadsheet that contains a user form, which updates a
sql server table. The sql server table has 4
fields that are the tables primary key (don't ask - my boss insists on
this much to my annoyance) The userform has 2 pages,
one for inserting records which works fine and the other page that
updates these records which again works most
of the time. My problem is when the user does not use the correct
primary key the sql code still runs but does not
say that the record has not been updated. How can I capture this in my
vba code? My excel vba update code is below:

Sub UpdateSQLDatabase()

Dim MyConnection As ADODB.Connection
Dim MySQL As String

Sheets("Upload Appointments").Visible = True
Sheets("Upload Appointments").Select

Set MyConnection = New ADODB.Connection

EmptyRows

MyConnection.Open "Provider=sqloledb;Data Source=test;Initial
Catalog=test;Integrated Security=SSPI;"

For i = 2 To Range("a400").End(xlUp).Row
MySQL = "UPDATE tbl_SourceData " & _
"SET AppDate ='" & Range("C" & i).Value & "', Outcome = '" &
Range("D" & i).Value & "', AppUpdated = '" & Range("Date").Value _
& "' WHERE Patient_ID =" & "'" & Range("A" & i).Value &
"'" _
& " AND Clinical_GP =" & "'" & Range("ClinGp").Value & "'"
_
& " AND Location = " & "'" & Range("Loc").Value & "'" _
& " AND RefDate = " & "'" & Range("B" & i).Value & "'"

MyConnection.Execute MySQL
Next i

MyConnection.Close

Set MyConnection = Nothing

End Sub


The 4 pk's are patient_ID, Clinical_Gp, Location and RefDate, the
problem arises when the user inputs an incorrect
refdate and I have no way of capturing that no rows were affected.

Any help would be greatly appreciated

Thank you
Rachel