本文介绍了ADODB.Recordset上的“类型不匹配”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应从SQL数据库读取数据并向Excel报告的程序。它可以在32位计算机上按预期工作,但是由于我移至64位工作环境,因此程序无法运行。这是我的代码示例(返回的第一个错误):

I have a program that is supposed to read data from an SQL database and report back to Excel. It works as expected on a 32-bit machine, but since I moved over to a 64-bit work environment, the program has failed to run. Here is a sample of my code (the first error returned):

Private Sub SearchBox_Change()
ResultBox.Clear

Call CompileQuery

'If the query is empty
If SearchBox.Value = "" Then
    NumShowingLabel = "Showing 0 of 0 Results"
    ResultBox.Clear
    GoTo noSearch
End If

'Open a new query with varQuery
With varRecordset
    .ActiveConnection = varConnection
    .Open varQuery
End With

'Set NumShowingLabel
If varRecordset.RecordCount > varMaxResults Then
    NumShowingLabel = "Showing 60 of " & varRecordset.RecordCount & " Results"
Else
    NumShowingLabel = "Showing " & varRecordset.RecordCount & " of " & varRecordset.RecordCount & " Results"
End If

'As long as there is a record, move to the first one
If Not varRecordset.RecordCount = 0 Then varRecordset.MoveFirst

'Add each record to ResultBox
If varRecordset.RecordCount > varMaxResults Then
    For varTempInt = 1 To varMaxResults
        ResultBox.AddItem varRecordset.Fields("FileName").Value
        varRecordset.MoveNext
    Next
Else
    For varTempInt = 1 To varRecordset.RecordCount
        ResultBox.AddItem varRecordset.Fields("FileName").Value
        varRecordset.MoveNext
    Next
End If

'Release varRecordSet
varRecordset.Close

noSearch:

End Sub

运行时,Excel返回错误类型不匹配并突出显示 .RecordCount 。对于varTempInt = 1 varRecordset.RecordCount (示例中的最后一个for循环)。我已安装Windows支持文章983246建议的修补程序,至少据我所知。我将其安装到C:目录并重新启动计算机,但仍然无法正常工作。

When run, Excel returns an error "Type Mismatch" and highlights .RecordCount of For varTempInt = 1 To varRecordset.RecordCount (the last for loop in the sample). I have installed the hotfix recommended by the Windows Support Article 983246, at least to the best of my understanding. I installed it to the C: directory and restarted my machine, but it still does not work.

编辑1:只是想澄清一下我以前使用的是ADO 2.5 NOT ADO 6.1

Edit 1: Just wanted to clarify that I was previously using ADO 2.5 NOT ADO 6.1

TL; DR:如何修复64上的 RecordSet.RecordCount 类型不匹配错误位计算机运行Excel 2010?

TL;DR: How can I fix a RecordSet.RecordCount "Type Mismatch" error on a 64-bit machine running Excel 2010?

推荐答案

我没有这个确切的问题,但是我发现<$ ADODB记录集上的c $ c> recordcount 属性是命中还是未命中。最好的选择是重写循环,例如:

I haven't had this exact problem, but I've found that the recordcount property on an ADODB recordset is hit or miss. Your best bet is to rewrite the loops like:

recordset.movefirst
While Not recordset.eof
    <your stuff with your record>
    recordset.movenext
Loop

还要测试是否有记录您的记录集可以使用:

Also, to test that there are records in your recordset you can use:

If recordset.BOF and recordset.EOF THEN
     <Something is wrong there are no records>
End If

我的猜测是ADODB recordcount属性可能用64位废话了几乎每个ODBC驱动程序中使用的ODBC驱动程序版本。

My guess is that the ADODB recordcount property is probably crap with the 64 bit version of whatever ODBC driver you are using as it is in nearly every ODBC driver.

这篇关于ADODB.Recordset上的“类型不匹配”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 19:04
查看更多