"Application Defined or Object Defined" VBA-SQL 连接中的错误
"Application Defined or Object Defined" error in VBA-SQL connection
我正在尝试为 SQL 连接编写一个 Exce-Vba 代码。代码首先会打开与服务器的连接,然后它将 table (Range("C22:G81")
) 的 4 列从我的 Excel-sheet 复制到 SQL-server(我现在只是想发送数字 table 作为测试,我不发送任何列名)
我已经尝试解决 "Application Defined or Object Defined" 错误很长时间了。我收到连接字符串错误 strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strName & ";Extended Properties=""Excel 12.0;"
我什至尝试过另一个带有密码选项的版本,例如 strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strName & ";Extended Properties=""Excel 12.0; Jet OLEDB:Database Password='passwd';"
但是我得到了同样的错误。我是 SQL 编码方面的新手。我想知道我是否遗漏了一些重要的东西。
最后,我不知道它是否与此错误有关,但我在 SQL 服务器中为我在 Excel 中的 4 列手动创建了 4 列。我是否需要写一些特定的东西,让 Excel-sheet 中的那 4 列能在 SQL-server 中找到正确的列?
提前致谢...
代码:
Private Sub inlasning()
Dim MyWorkBook As Workbook
Dim rs As New ADODB.Recordset
Dim conn As New ADODB.Connection
Dim ServerName As String, DataBaseName As String, strSQL As String
Set conn = New ADODB.Connection
ServerName = "E45c7642"
DataBaseName = "Tables"
' Specify the OLE DB provider
conn.Provider = "sqloledb"
' Set SQLOLEDB connection properties
conn.Properties("Data Source").Value = ServerName
conn.Properties("Initial Catalog").Value = DataBaseName
' Windows NT authentication.
conn.Properties("Integrated Security").Value = "SSPI"
conn.Open
Dim ValidSheet As Worksheet
Dim HeaderRange As Range
Dim DataRange As Range
Dim ColRange As Range
Dim LastRange As Range
Dim strName As String, strCon As String
strName = ThisWorkbook.FullName
Application.ScreenUpdating = False
Set ValidSheet = ThisWorkbook.Sheets("Sheet2") '
Set HeaderRange = ValidSheet.Range("C20:G21")
Set ColRange = HeaderRange.Find(TheHeader, , , xlWhole)
Set LastRange = ColRange.End(xlDown)
Set DataRange = ValidSheet.Range("C22:G81") ' This is what I am trying to transfer, only numeric values without column names
strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strName _
& ";Extended Properties=""Excel 12.0;"
conn.Open strCon
strSQL = "SELECT * FROM [" & ValidSheet.Name & "$" & Replace(DataRange, "$", "") & "];"
rs.Open strSQL, dbclass, adOpenStatic, adLockReadOnly
arrData = rs.GetRows
rs.Close
conn.Close
Set rs = Nothing
Set conn= Nothing
Set ValidSheet = Nothing
End Sub
在 "connection string" 出现相同的错误后,我改变了策略,使用 dbclass 过程打开连接。所以新代码如下所示。 (我从一个人那里找到了这个代码,但他现在正在度假,所以我不能问他)。
它自动获取连接 (dbclass) 属性,这些属性保存在主 ThisWorkbook 中。此代码根本没有给出任何错误,但它不会将列从 Excel 复制到数据库。我为 sql-query 尝试了不同的版本,比如 SQL = .... VALUES('result')
或 SQL = .... VALUES(result)
,但是没有结果,没有错误。
Private Sub Testing_Click()
Dim FindColValues() As Double
Dim ValidBook As Workbook
Dim ValidSheet As Worksheet
Dim DataRange As Range
Dim dataa As Range
Application.ScreenUpdating = False
TheSheet = "Sheet2"
Set ValidSheet = Worksheets(TheSheet)
Set DataRange = ValidSheet.Range("C21:C81")
' Below creating an array "result(it)" from the seleced range.
For Each dataa In DataRange
ReDim Preserve result(it)
result(it) = dataa.Value
it = it + 1
Next
' Below is just an alternative array for "in case"
arrData = ValidSheet.Range("C22:G81").Value
SQL = "INSERT INTO Table_test (Column1) VALUES ('result()');"
dbclass.ExecuteSQL SQL
End Sub
下面是其他函数自动读取的 dbclass 连接属性:
Private Sub Workbook_Open()
Dim connOk As Boolean
Dim rs As New ADODB.Recordset
Dim MyWorkBook As Workbook
Dim CurSheet As Worksheet
Set dbclass = New clsDB
dbclass.Database = "Tables"
dbclass.ConnectionType = SqlServer
dbclass.DataSource = "E45c7642"
dbclass.UserId = Application.UserName
connOk = dbclass.OpenConnection(False, True)
If connOk = False Then
MsgBox "Cannot connect"
Else
MsgBox "The server is connected"
End If
End Sub
我终于找到了第二个代码的问题。正如我之前所写,在我的替代代码(第二个代码)中,我在 VBA 中完全没有收到任何错误,但它没有将我的 table 保存到服务器中。
现在我知道原因了,因为我的真实值是"comma"格式,但是服务器保存的值是"dot"格式。所以我添加了 Str(value)
以将 "comma" 值转换为 "dot" 值,现在它起作用了:
....
SQL = "INSERT INTO Table_test (Column1) VALUES (" & Str(result(1)) & ")"
dbclass.ExecuteSQL SQL
End Sub
我正在尝试为 SQL 连接编写一个 Exce-Vba 代码。代码首先会打开与服务器的连接,然后它将 table (Range("C22:G81")
) 的 4 列从我的 Excel-sheet 复制到 SQL-server(我现在只是想发送数字 table 作为测试,我不发送任何列名)
我已经尝试解决 "Application Defined or Object Defined" 错误很长时间了。我收到连接字符串错误 strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strName & ";Extended Properties=""Excel 12.0;"
我什至尝试过另一个带有密码选项的版本,例如 strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strName & ";Extended Properties=""Excel 12.0; Jet OLEDB:Database Password='passwd';"
但是我得到了同样的错误。我是 SQL 编码方面的新手。我想知道我是否遗漏了一些重要的东西。
最后,我不知道它是否与此错误有关,但我在 SQL 服务器中为我在 Excel 中的 4 列手动创建了 4 列。我是否需要写一些特定的东西,让 Excel-sheet 中的那 4 列能在 SQL-server 中找到正确的列?
提前致谢...
代码:
Private Sub inlasning()
Dim MyWorkBook As Workbook
Dim rs As New ADODB.Recordset
Dim conn As New ADODB.Connection
Dim ServerName As String, DataBaseName As String, strSQL As String
Set conn = New ADODB.Connection
ServerName = "E45c7642"
DataBaseName = "Tables"
' Specify the OLE DB provider
conn.Provider = "sqloledb"
' Set SQLOLEDB connection properties
conn.Properties("Data Source").Value = ServerName
conn.Properties("Initial Catalog").Value = DataBaseName
' Windows NT authentication.
conn.Properties("Integrated Security").Value = "SSPI"
conn.Open
Dim ValidSheet As Worksheet
Dim HeaderRange As Range
Dim DataRange As Range
Dim ColRange As Range
Dim LastRange As Range
Dim strName As String, strCon As String
strName = ThisWorkbook.FullName
Application.ScreenUpdating = False
Set ValidSheet = ThisWorkbook.Sheets("Sheet2") '
Set HeaderRange = ValidSheet.Range("C20:G21")
Set ColRange = HeaderRange.Find(TheHeader, , , xlWhole)
Set LastRange = ColRange.End(xlDown)
Set DataRange = ValidSheet.Range("C22:G81") ' This is what I am trying to transfer, only numeric values without column names
strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strName _
& ";Extended Properties=""Excel 12.0;"
conn.Open strCon
strSQL = "SELECT * FROM [" & ValidSheet.Name & "$" & Replace(DataRange, "$", "") & "];"
rs.Open strSQL, dbclass, adOpenStatic, adLockReadOnly
arrData = rs.GetRows
rs.Close
conn.Close
Set rs = Nothing
Set conn= Nothing
Set ValidSheet = Nothing
End Sub
在 "connection string" 出现相同的错误后,我改变了策略,使用 dbclass 过程打开连接。所以新代码如下所示。 (我从一个人那里找到了这个代码,但他现在正在度假,所以我不能问他)。
它自动获取连接 (dbclass) 属性,这些属性保存在主 ThisWorkbook 中。此代码根本没有给出任何错误,但它不会将列从 Excel 复制到数据库。我为 sql-query 尝试了不同的版本,比如 SQL = .... VALUES('result')
或 SQL = .... VALUES(result)
,但是没有结果,没有错误。
Private Sub Testing_Click()
Dim FindColValues() As Double
Dim ValidBook As Workbook
Dim ValidSheet As Worksheet
Dim DataRange As Range
Dim dataa As Range
Application.ScreenUpdating = False
TheSheet = "Sheet2"
Set ValidSheet = Worksheets(TheSheet)
Set DataRange = ValidSheet.Range("C21:C81")
' Below creating an array "result(it)" from the seleced range.
For Each dataa In DataRange
ReDim Preserve result(it)
result(it) = dataa.Value
it = it + 1
Next
' Below is just an alternative array for "in case"
arrData = ValidSheet.Range("C22:G81").Value
SQL = "INSERT INTO Table_test (Column1) VALUES ('result()');"
dbclass.ExecuteSQL SQL
End Sub
下面是其他函数自动读取的 dbclass 连接属性:
Private Sub Workbook_Open()
Dim connOk As Boolean
Dim rs As New ADODB.Recordset
Dim MyWorkBook As Workbook
Dim CurSheet As Worksheet
Set dbclass = New clsDB
dbclass.Database = "Tables"
dbclass.ConnectionType = SqlServer
dbclass.DataSource = "E45c7642"
dbclass.UserId = Application.UserName
connOk = dbclass.OpenConnection(False, True)
If connOk = False Then
MsgBox "Cannot connect"
Else
MsgBox "The server is connected"
End If
End Sub
我终于找到了第二个代码的问题。正如我之前所写,在我的替代代码(第二个代码)中,我在 VBA 中完全没有收到任何错误,但它没有将我的 table 保存到服务器中。
现在我知道原因了,因为我的真实值是"comma"格式,但是服务器保存的值是"dot"格式。所以我添加了 Str(value)
以将 "comma" 值转换为 "dot" 值,现在它起作用了:
....
SQL = "INSERT INTO Table_test (Column1) VALUES (" & Str(result(1)) & ")"
dbclass.ExecuteSQL SQL
End Sub