Object variable or With block variable not set Access vba [duplicate]

核能气质少年 提交于 2019-12-13 22:24:17

问题


I'm working with a couple tables, CTOL and CTOL_Asbuilt in Access. I'm trying to run a query to join these two tables together using VBA code. I ran the query in Access and it works. I'm using DAO for the database library to retrieve data from the local Access database (code is in the same database project as the database), and I'm new to VBA Access scripting.

SELECT CTOL.ID, CTOL.BOM_PART_NAME, CTOL.CII, CTOL.[PART FIND NO], CTOL.CSN,
       CTOL.AFS, CTOL.EQP_POS_CD, CTOL.LCN, CTOL.POS_CT, CTOL.SERIAL_NO, 
       CTOL.PART_NO_LLP, [CTOL_Asbuilt].[PART-SN], [CTOL_Asbuilt].[PART-ATA-NO], 
       [CTOL_PW-E750207_Asbuilt].[PW-PART-NO]
FROM CTOL LEFT JOIN [CTOL_Asbuilt] ON CTOL.[PART FIND NO] = [CTOL_Asbuilt].[PART-ATA-NO];

This is the code below:

Option Compare Database
Option Explicit

'Const adOpenStatic = 3
'Const adLockOptimistic = 3

Function queryDatabase()

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsQuery As DAO.Recordset


Dim part_find_no() As String
Dim eqp_pos() As Integer
'Dim strSQL As String
Dim i As Integer
Dim j As Integer
'Set objConnection = CurrentDb.OpenRecordset("CTOL")

Set db = CurrentDb

Set rsQuery = db.OpenRecordset("SicrProcess", dbOpenDynaset)

rs.Close
db.Close

Set rs = Nothing
Set db = Nothing

End Function

I'm getting the following error when I run this code with a macro that calls the function:

Run time error '91':

Object variable or With block variable not set

I'm trying to use the code with the query to loop through two fields and increment the value of the EQP_POS_CD field when the PART FIND NO entry matches the last (else, it just moves to the next record until it reaches the end of the result set). I want to test-run this query to make sure that the code retrieves the result that is output by running the query manually in Access.

Can you help me in fixing this error so I can run my code to retrieve the data? Thanks!


回答1:


rs.Close

You cannot close something that is not open. Perhaps you meant it to be rsQuery.Close?




回答2:


Open a recordset and loop through records.

Sub queryDatabase()
    On Error GoTo ErrProc

    Dim db As DAO.Database
    Set db = CurrentDb

    Dim qdf As DAO.QueryDef
    Set qdf = db.QueryDefs("SicrProcess") 'set your query name here

    Dim rs As DAO.Recordset
    Set rs = qdf.OpenRecordset(dbOpenDynaset)

    Dim part_find_no() As String
    Dim eqp_pos() As Integer, i As Integer

    If rs.EOF Then GoTo Leave
    rs.MoveLast
    rs.MoveFirst

    For i = 1 To rs.RecordCount

        '...
        'Do work here
        '...

        rs.MoveNext
    Next i

Leave:
    On Error Resume Next
    rs.Close
    Set rs = Nothing
    qdf.Close
    Set qdf = Nothing
    Set db = Nothing
    On Error GoTo 0
    Exit Sub

ErrProc:
    MsgBox Err.Description, vbCritical
    Resume Leave
End Sub


来源:https://stackoverflow.com/questions/44439264/object-variable-or-with-block-variable-not-set-access-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!