简体   繁体   中英

export entire excel sheet to sql table with VBA

having issue sending entire sheet to sql table. I have tried 2 different ways as shown below. both work in different ways. Ultimately I am struggling with exporting multiple rows of data to sql DB

this 1st way I can import multiple rows to sql table successfully with one click of the button, only problem is that the StartTime and FinishTime come to sql table as 00:00:00 even if I enter a normal time in excel

Sub Button1_Click()
  Dim conn As New ADODB.Connection
  Dim iRowNo As Integer
  Dim sEventDate, sID, sDeptCode, sOpCode, sStartTime, sFinishTime, sUnits  As String

  With Sheets("Sheet1")
    'Open a connection to SQL Server
    conn.Open "Provider=SQLOLEDB;Data Source=db\db1;Initial Catalog=PTW;Integrated Security=SSPI;"
    'Skip the header row
    iRowNo = 2
    'Loop until empty cell in FirstName
    Do Until .Cells(iRowNo, 1) = ""
    sEventDate = .Cells(iRowNo, 1)
    sID = .Cells(iRowNo, 2)
    sDeptCode = .Cells(iRowNo, 3)
    sOpCode = .Cells(iRowNo, 4)
    sStartTime = .Cells(iRowNo, 5).Text
    sFinishTime = .Cells(iRowNo, 6).Text
    sUnits = .Cells(iRowNo, 7)
    'Generate and execute sql statement to import the excel rows to SQL Server table
    conn.Execute "insert into dbo.TimeLog (EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) values ('" & sEventDate & "', '" & sID & "', '" & sDeptCode & "', '" & sOpCode & "', cast('" & dStartTime & "' as datetime), cast('" & dFinishTime & "' as datetime), '" & sUnits & "')"

    iRowNo = iRowNo + 1

    Loop

    MsgBox "Data Successfully Exported."
    conn.Close
    Set conn = Nothing
  End With
End Sub

this 2nd way works and sends the StartTime and FinishTime exactly as it is in excel table, but it only allows me to send one row to sql table at a time

Sub Button1_Click()
  Dim conn As ADODB.Connection
  Dim cmd As ADODB.Command
  Dim strSQL As String

  strSQL = "INSERT INTO dbo.TimeLog" & _
     "(EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) " & _
     "VALUES (?,?,?,?,?,?,?)"

Set conn = New ADODB.Connection
  conn.Open "Provider=SQLOLEDB;Data Source=db\db1;Initial Catalog=PTW;Integrated Security=SSPI;"

iRowNo = 1

Set cmd = New ADODB.Command

  cmd.ActiveConnection = conn
  cmd.CommandType = adCmdText
  cmd.CommandText = strSQL



With Sheets("Sheet1")
  'Loop until empty cell in FirstName
   Do Until .Cells(iRowNo, 1) = ""

  cmd.Parameters.Append _
    cmd.CreateParameter("pEventDate", adVarChar, adParamInput, 8, .Cells(iRowNo, 1))
  cmd.Parameters.Append _
    cmd.CreateParameter("pID", adInteger, adParamInput, , .Cells(iRowNo, 2))
  cmd.Parameters.Append _
    cmd.CreateParameter("pDeptCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 3))
  cmd.Parameters.Append _
    cmd.CreateParameter("pOpCode", adVarChar, adParamInput, 2, .Cells(iRowNo, 4))
  cmd.Parameters.Append _
    cmd.CreateParameter("pStartTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 5))
  cmd.Parameters.Append _
    cmd.CreateParameter("pFinishTime", adDBTime, adParamInput, 0, .Cells(iRowNo, 6))
  cmd.Parameters.Append _
    cmd.CreateParameter("pUnits", adInteger, adParamInput, , .Cells(iRowNo, 7))

  cmd.Execute


  iRowNo = iRowNo + 1
  Loop

  MsgBox "Data Successfully Exported"

End With

  conn.Close
  Set conn = Nothing
End Sub

you must cast the value. In mssql, yourt time value is recognized as string.

cast('8:00:00' as datetime)

conn.Execute "insert into dbo.TimeLog (EventDate, ID, DeptCode, Opcode, StartTime, FinishTime, Units) values ('" & sEventDate & "', '" & sID & "', '" & sDeptCode & "', '" & sOpCode & "', cast('" & dStartTime & "' as datetime), cast('" & dFinishTime & "' as datetime), '" & sUnits & "')"

Adding this inside the loop of the first script was the result I was looking for...

    Set cmd = New ADODB.Command
    cmd.ActiveConnection = conn
    cmd.CommandType = adCmdText
    cmd.CommandText = strSQL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM