我有一个excel应用程序,它使用ODBC连接到HANA数据库,并在按钮单击下获取下一页中的表数据。我使用下面的代码从数据库中获取数据: -
Function importRoutine(sheet As String, provider As String, databaseschema As String, server As String, app As String, row As Long, column As Long, commandText As String, displayname As String, errorFlag As Boolean) As Integer
Debug.Print commandText
Dim l As QueryTable
Dim rs As Object
Dim cnt As ADODB.Connection
Dim cmd As ADODB.Command
Dim iCols As Integer
Set cnt = New ADODB.Connection
Set cmd = New ADODB.Command
Set rcd = New ADODB.Recordset
Dim rangesheet As String
rangesheet = Sheets(sheet).Cells(row + 1, column).Address
Dim connectionsheet As String
connectionsheet = "Driver=" + provider + ";SERVERNODE=" + server + ";" + getAuthentication + "; CS=" + databaseschema + ""
cnt.ConnectionString = connectionsheet
cnt.Open
Set cmd.ActiveConnection = cnt
cmd.commandText = commandText
cmd.CommandType = adCmdText
rcd.Open commandText, cnt '', adOpenStatic
Set rs = CreateObject("ADODB.RECORDSET")
rs.ActiveConnection = cnt
rs.Open commandText, cnt
With Sheets(sheet).ListObjects.Add(SourceType:=3, Source:=rs, Destination:=Range(Sheets(sheet).Cells(row, column).Address)).QueryTable
'.commandText = commandText
'.CommandType = xlCmdSql
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCell
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.ListObject.Name = displayname
.PreserveColumnInfo = False
On Error Resume Next
.Refresh BackgroundQuery:=False
End With
Sheets(sheet).ListObjects(displayname).TableStyle = "TableStyleMedium10"
importRoutine = 0
Call deleteConnection
CloseRecordset:
rs.Close
Set rs = Nothing
CloseConnection:
cnt.Close
Set cnt = Nothing
On Error GoTo 0
End Function
如果表包含英文字符,则代码可以正常工作。但是如果表中有一些特殊字符,比如一些中文字符,则无法将数据放入下一页并抛出错误。
VBA是否有任何限制无法获取/查看包含中文字符的数据?
答案 0 :(得分:0)
我能够解决该错误。问题出在我表中的数据类型列中。 varchar是创建unicode错误的。当我将相同的内容更改为NVARCHAR时,数据库中的数据提取工作在所有其他语言字符的工作表上正常工作。