我正在将来自外部数据库的数据调用到ListObject表中。该脚本在Sub()中运行时创建ListObject表不会出现问题,但是在Function内部调用时,该脚本仅在ListObject行中停止,它报告Application-defined or object-defined error
。以下脚本:
Function get_value(input_id As String, input_date As String)
On Error GoTo xerr
Dim sqlstring As String
Dim connstring As String
Dim sLogin As String
sLogin = "DATABASE=DB;UID=UID;PWD=PWD"
sqlstring = "SELECT data_date, data_value FROM tb_data_values WHERE series_id='" & input_id & "' AND data_date<='" & input_date & _
"' ORDER BY data_date DESC"
connstring = "ODBC;DSN=myodbc;" & sLogin
Dim qtTable As QueryTable
Set qtTable = Worksheets("hs").ListObjects.Add(SourceType:=xlSrcExternal, Source:=connstring, Destination:=Worksheets("hs").Range("A1")).QueryTable
With qtTable
.ListObject.ShowHeaders = False
.ListObject.ShowTableStyleRowStripes = False
.ListObject.ShowTableStyleColumnStripes = False
.ListObject.ShowTableStyleFirstColumn = False
.CommandText = sqlstring
.CommandType = xlCmdSql
.RefreshStyle = xlOverwriteCells
.BackgroundQuery = False
.Refresh
End With
get_value=Worksheets("hs").ListObjects(1).Cells(1,2)
Exit Function
xerr:
Debug.Print err.Description
End Function
答案 0 :(得分:0)
将记录带入Excel表时,我使用 select distinct t.name as tablename, p.rows as cnt
from sys.columns c
inner join sys.tables t on c.object_id = t.object_id
INNER JOIN
sys.schemas s ON t.schema_id = s.schema_id
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
WHERE
t.is_ms_shipped = 0
AND
t.name in (select t1.name from sys.columns c1 inner join sys.tables t1 on c1.object_id = t1.object_id
where c1.name = 'person_id')
order by 2
这是一个例子:
Connections