函数返回记录集后编译错误

时间:2018-08-20 10:05:08

标签: ms-access adodb recordset

我想我有一个很简单的问题,因为我只是在学习Access,但是在论坛中找不到任何解决方案。在我的代码中,有一个返回记录集的函数,它的作用很好,但是当我设置指向该记录集的指针时,它没有指向。我文件中的相关代码如下:

Private Sub Form_Open(Cancel As Integer)

EntryId = Forms.Item("fCustlist").Recordset.Fields("Custlist_Entry_Id")
setCustomersRecordset
setFormElements

End Sub

Private Sub setCustomersRecordset()
Dim rsCustomersDb As ADODB.Recordset
Dim i As Integer

Set rsCustomersDb = getRelatedCustomers(EntryId)

For i = 1 To rsCustomersDb.RecordCount '<--COMPILE ERROR: Type mismatch
'.....
Next i

End Sub

Public Function getRelatedCustomers(EntryId As Long) As ADODB.Recordset
Dim sql As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

sql = "SELECT blah blah blah"

Set cn = CurrentProject.AccessConnection
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open sql, cn, , adLockReadOnly

Set getRelatedCustomers = rs
End Function

我正在使用getRelatedCustomers返回记录集,我可以做到

Debug.Print getRelatedCustomers(entryId).RecordCount

确实返回正确的数字,但是在设置RsCustomersDb = getRelatedCustomers(entryId)之后,它不会编译。我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

ADODB recordcount属性很长。您应该使用Dim i As Long,而不是Dim i As Integer

通常,除非真正需要用于旧版API的整数,否则不应在VBA中使用整数。一个long可以存储更多,更快,并且大小相等的内存。了解更多here