使用两个字段的条件在记录集上执行查找

时间:2019-07-15 10:53:11

标签: sql sql-server excel vba

我正在尝试对具有两个条件的记录集执行Find,每个条件一个。但是,我遇到以下错误:

  

参数类型错误,超出可接受范围或彼此冲突

Set rs = getdata("Select Distinct astAssetTypes.Code AT, astComponents.Code Comp FROM dbo.astComponents JOIN dbo.astAssetTypes ON astAssetTypes.Id = astComponents.AssetTypeId WHERE astAssetTypes.Code IN(" & ComponentCode & ")")
If Not rs.EOF Then
    rs.Find rs.Fields(1).Name & "='" & tempArr(b, 1) & "' AND " & rs.Fields(0).Name & "='" & tempArr(b, 9) & "'"
End If

打印查找条件的结果为:
Comp='0738.D100.L00.55' AND AT='9211.D108.D07.01_02.02'

我为它为什么不起作用感到困惑。从在线资源说明这种“查找”功能的可能性来看,我不明白为什么这种方法无法处理。

查询有效,getdata是一个自定义函数:

Public Function getdata(query As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim connstring As String
Set cnn = New ADODB.Connection

connstring = "Provider=SQLOLEDB;Data Source=omitted;Connect Timeout=180"
cnn.Open connstring

Set getdata = New ADODB.Recordset
    getdata.CursorLocation = adUseClient
getdata.Open query, connstring, adOpenStatic, adLockBatchOptimistic

cnn.Close
End Function

0 个答案:

没有答案