我是VBA的新手 - SQL Server交互。目前,我尝试从Excel读取数据并将其添加到SQL Server中的表中。我不明白为什么我得到对象关闭的错误。如果有任何其他错误或事情可以更容易或更好地编码,请随时分享!
Sub UpdateBG_Results()
Dim dbConnectStr As String
Dim connect As ADODB.Connection
Dim results() As Variant
Dim rec As ADODB.Recordset
Dim fieldsArray() As String
Sheets("Operational KPI's ROUND").Select
fieldsArray() = GetStringArray(ActiveSheet.Range("Y12:AO12"))
results = ActiveSheet.Range("Y14:AO14").Value
dbConnectStr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=HOME27C4K12\SQLEXPRESS"
Set connect = New ADODB.Connection
connect.Open dbConnectStr
Set rec = New ADODB.Recordset
rec.Open "USE Current_state1 SELECT * FROM BG_Results", connect, adOpenKeyset, adLockOptimistic
rec.AddNew fieldsArray, results
rec.Update
rec.Close
End Sub