简体   繁体   English

Excel VBA SQL Server数据导入“类型不匹配错误”

[英]excel vba sql server data import “type mismatch error”

I am attempting to create a macro that will pull in some data into Excel 2003 from SQL Server 2005. 我正在尝试创建一个宏,该宏将从SQL Server 2005将一些数据提取到Excel 2003中。

The SQL code is in this macro, and part of the code for this comes from values in a cell in Excel. SQL代码在此宏中,并且此代码的​​一部分来自Excel中单元格中的值。 This works, to a degree, but when the data in the Excel cell that i'm using for the SQL code exceeds a line in the formula preview (or approx 170 characters), i get the "Type mismatch" error. 这在一定程度上可行,但是当我用于SQL代码的Excel单元格中的数据超过公式预览中的一行(或大约170个字符)时,出现“类型不匹配”错误。 Otherwise it works correctly. 否则它将正常工作。

With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data       Source=anglobisql;Use Procedure for Prepare=1;Auto " _
    , _
    "Translate=True;Packet Size=4096;Workstation ID=MyIDHere;Use Encryption for Data=False;Tag with column collation when possible=F" _
    , "alse;Initial Catalog=DATABASENAME"), Destination:=Range("A1"))
    .CommandType = xlCmdSql
    .CommandText = Array("SELECT COLUMN1, COLUMN2 FROM TABLENAME (nolock) WHERE COLUMN1 IN (" & ActiveWorkbook.Sheets("Sheet1").Range("e607").Value & ")")
    .Name = "DATABASE TABLENAME"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .SourceConnectionFile = _
    "H:\My Data Sources\DATABASE TABLENAME.odc"
    .Refresh BackgroundQuery:=False

Obviously all databasename and tablename are filled in correctly. 显然,所有数据库名称和表名称均正确填写。

Any idea why this is falling down (whether there's a character limit, or whether it fails because the cell value continues onto the next line in the formula preview bar in Excel? How could this be fixed? 知道为什么它会下降(是否存在字符数限制,或者是否由于单元格值继续到Excel中的公式预览栏中的下一行而失败),如何解决?

Much appreciated, 非常感激,

Alex 亚历克斯

There shouldn't be any problems caused by the Excel cell having more than 170 characters. Excel单元格超过170个字符不会引起任何问题。 I suggest splitting the line that errors out to help find exactly where the problem is. 我建议将出错的行分开,以帮助找到问题所在。

Replace this: 替换为:

    .CommandText = Array("SELECT COLUMN1, COLUMN2 FROM TABLENAME (nolock) WHERE COLUMN1 IN (" & ActiveWorkbook.Sheets("Sheet1").Range("e607").Value & ")")

With this: 有了这个:

    Dim rng As Range, sSql As String
    Set rng = ActiveWorkbook.Sheets("Sheet1").Range("e607")
    Debug.Print "Range.Value = |" & rng.Value & "|"
    sSql = "SELECT COLUMN1, COLUMN2 FROM TABLENAME (nolock) WHERE COLUMN1 IN (" & rng.Value & ")"
    Debug.Print "SQL = |" & sSql & "|"
    .CommandText = sSql

Debug.Print prints to the immediate window so you can double-check that the variables are holding what you expect them to. Debug.Print打印到立即窗口,因此您可以再次检查变量是否保存了期望的值。 I like using | 我喜欢使用| instead of " when debugging strings because I almost never have a | inside a string. 调试字符串时使用“ | "代替" ,因为我几乎在字符串中都没有|

Also, I wouldn't bother using Array() for the CommandText or the Connection . 另外,我不会为CommandTextConnection使用Array() I'd redo your first line to look something like this for readability. 为了方便阅读,我将重做第一行,使其看起来像这样。

With ActiveSheet.QueryTables.Add( _
        Connection:="OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI" _
                & ";Persist Security Info=True;Data Source=anglobisql" _
                & ";Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096" _
                & ";Workstation ID=MyIDHere;Use Encryption for Data=False" _
                & ";Tag with column collation when possible=False;Initial Catalog=DATABASENAME" _
      , Destination:=Range("A1"))

This is solved by removing the array text so 这是通过删除数组文本来解决的,因此

.CommandText = Array("SELECT COLUMN1, COLUMN2 FROM TABLENAME (nolock) WHERE ...

becomes 变成

.CommandText = ("SELECT COLUMN1, COLUMN2 FROM TABLENAME (nolock) WHERE ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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