Good Day All,
I'm banging my head against the table a bit with this one. I can update an SQL table via Excel VBA except the date will not pass through correctly. The value always comes through as 1900-01-01 or in some cases where I have played with the format it's shown as 1900-01-28.
It's a simple setup just for testing. 1 Table with 2 columns CellText and CellDate, both getting their values from a cell range.
The value expected for CellText is 'Some Text' The value expected for CellDate is 24/03/2015
This is my code:
Sub UpdateTable()
Dim cnn As ADODB.Connection
Dim uSQL As String
Dim strText As String
Dim strDate As Date
strText = ActiveSheet.Range("b4").Value
strDate = Format(ActiveSheet.Range("c4").Value, "dd/mm/yyyy")
Set cnn = New Connection
cnnstr = "Provider=SQLOLEDB; " & _
"Data Source=ServerName; " & _
"Initial Catalog=DbName;" & _
"User ID=UserName;" & _
"Trusted_Connection=Yes;"
cnn.Open cnnstr
uSQL = "INSERT INTO tbl_ExcelUpdate (CellText,CellDate) VALUES ('" & strText & "', " & strDate & ")"
Debug.Print uSQL
cnn.Execute uSQL
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub
My Debug value is this INSERT INTO tbl_ExcelUpdate (CellText,CellDate) VALUES ('Some Text ', 24/03/2015)
My CellDate format in table is datetime.
Can anyone shed any light please?
Kind Regards Dino
Aucun commentaire:
Enregistrer un commentaire