So, I frankensteined a bit of code together last week, to upload data from my excel sheet to my database. It worked perfectly. I've come back to it today, and now it's not working... I keep getting the error message:
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
I think the fact that it WAS working has left me a bit blind to find the problem... So I'd be really grateful if someone with fresh eyes could have a look over it, and see if they can see the problem!
Sub Button1_Click()
If MsgBox("Are you sure you want to upload this data?", vbYesNo) = vbNo Then Exit Sub
Dim conn As New ADODB.Connection
Dim iRowNo, sArtistID, sSaleMonth, sSaleYear As Integer
Dim sValue As Long
Dim sSaleroom As String
Dim sUploaded As Date
sUploaded = Now()
With Sheets("DataUpload")
'Open a connection to SQL Server
'Skip the header row
iRowNo = 2
'Loop until empty cell in CustomerId
Do Until .Cells(iRowNo, 1) = ""
sArtistID = .Cells(iRowNo, 1)
sSaleMonth = .Cells(iRowNo, 2)
sSaleYear = .Cells(iRowNo, 3)
sSaleroom = .Cells(iRowNo, 4)
sValue = .Cells(iRowNo, 5)
'Generate and execute sql statement to import the excel rows to SQL Server table
conn.Execute "insert into dbo.Data (ArtistID, SaleMonth, SaleYear, SaleRoom, Value, Uploaded) values ('" & sArtistID & "', '" & sSaleMonth & "', '" & sSaleYear & "', '" & sSaleroom & "', '" & sValue & "', '" & sUploaded & "')"
iRowNo = iRowNo + 1
Loop
MsgBox "Data imported."
conn.Close
Set conn = Nothing
End With
End Sub
DataID (int) has Identity Specification on, increment 1. ArtistID, salemonth/year and value are all int, saleroom is char and uploaded is datetime.
Thanks very much!
datetimeand the data you are currently uploading is not recognized as a datetime. So, we need to know which column is expecting a datetime. IsSalesMonthadatetimeor maybeSaleYear? Maybe some sample data from the tabledbo.Datacould be helpful. So, you might want to include some in your question.