auf SQL Hoffte, wenn jemand Skript helfen könnte dieses für michExport von Excel mit Visual Basic
ich eine Excel-Datei mit einem Makro einer Schaltfläche als Anlage erstellen bin versucht, die Daten in SQL exportieren. My SQL-Tabelle ist als
RecordedPeriod (datetime, not null)
EventDate (varchar(8), not null)
ID (int, not null)
DeptCode (varchar(2), not null)
OpCode (varchar(2), not null)
StartTime (time(0), not null)
FinishTime (time(0), not null)
Units (int, not null)
folgt Während das Makro in Visual Basic für Applikationen zu schaffen ist mein Code wie folgt
Sub Button1_Click()
Dim conn As New ADODB.Connection
Dim iRowNo As Integer
Dim sRecordedPeriod, sEventDate, sID, sDeptCode, sOpCode, sStartTime, sFinishTime, sUnits As String
With Sheets("Sheet1")
'Open a connection to SQL Server
conn.Open "Provider=SQLOLEDB;Data Source=db1\db1;Initial Catalog=ProdTrack;Integrated Security=SSPI;"
'Skip the header row
iRowNo = 2
'Loop until empty cell in FirstName
Do Until .Cells(iRowNo, 1) = ""
sRecordedPeriod = .Cells(iRowNo, 1)
sEventDate = .Cells(iRowNo, 2)
sID = .Cells(iRowNo, 3)
sDeptCode = .Cells(iRowNo, 4)
sOpCode = .Cells(iRowNo, 5)
sStartTime = .Cells(iRowNo, 6)
sFinishTime = .Cells(iRowNo, 7)
sUnits = .Cells(iRowNo, 8)
'Generate and execute sql statement to import the excel rows to SQL Server table
conn.Execute "insert into dbo.TimeLog (RecordedPeriod, EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) values ('" & sRecordedPeriod & "', '" & sEventDate & "', '" & sID & "', '" & sDeptCode & "', '" & sOpCode & "', '" & sStartTime & "', '" & sFinishTime & "', '" & sUnits & "')"
iRowNo = iRowNo + 1
Loop
MsgBox "Data Successfully Exported."
conn.Close
Set conn = Nothing
ich diese Fehlermeldung beim Export.
Run-time error '2147217913 (80040e07)':
Conversion failed when converting date and/or time from character string.
Hat jemand irgendwelche Vorschläge? Diese
ist, wie meine Excel-Tabelle aussieht. Ich versuche, das Zeitformat zu ändern, immer noch einen Fehler zu bekommen. Mein EventDate ist ein Varchar in SQL, daher sollte es dort keine Probleme geben.
RecordedPeriod EventDate ID DeptCode OpCode StartTime FinishTime Units
NULL 6/15/2017 45318 DS DS 8:00:00 9:00:00 500
NULL 6/15/2017 45318 DS DS 9:00:00 9:15:00 500
NULL 6/15/2017 45318 DS DS 9:15:00 9:20:00 500
Die Daten in meiner Excel-Datei gelöscht werden, nachdem wir auf SQL exportieren, und wir werden in täglich neue Daten fallen.
gut ist Ihr Datum im falschen Format ... – User632716
@tompreston dort Hallo, ja gerade nicht sicher, wo bei. Können Sie ein Beispiel geben? Ich will nicht das SQL-Tabelle – DRUIDRUID
Datumsformate yyyymmdd und yyyy-mm-dd arbeiten ändern. –