Error on closing a ADODB.Recordset VBA

Anonymous
2022-12-10T08:33:15+00:00

Dear all,

I have built this VBA function but I'm getting the below error (see the next screenshot) in the bold portion of the code:

Public Sub UserLogoutDetail(UserEmail As String) 

    Dim MySQL As String 

    Dim RstUserAccess As New ADODB.Recordset 

    Dim CmdUserAccess  As New ADODB.Command 

    Dim i As Integer 

    'Setup the command 

    Set CmdUserAccess.ActiveConnection = fGetConn 

    CmdUserAccess.CommandType = adCmdText 

    MySQL = "UPDATE User_Access_TABLE SET EXIT_DATA = #" & Now & "# WHERE (((ID)=" & GetID_ToUpdate(UserEmail) & "))" 

    CmdUserAccess.CommandText = MySQL 

    RstUserAccess.CursorLocation = adUseClient 

    RstUserAccess.CursorType = adOpenDynamic 

    RstUserAccess.LockType = adLockOptimistic 

    'Open the connection 

    RstUserAccess.Open CmdUserAccess 

Set RstUserAccess.ActiveConnection = Nothing 

RstUserAccess.Close

    'Cleanup 

    If CBool(CmdUserAccess.State And adStateOpen) = True Then 

        Set CmdUserAccess = Nothing 

    End If 

    If CBool(fGetConn.State And adStateOpen) = True Then 

        CloseConn 

    End If 

 End Sub

Do you have any suggestion?

Thanks

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2022-12-10T10:25:34+00:00

    You should close the recordset before setting the connection to nothing. But why use a recordset at all? Since your SQL string is an Update statement, you can use

    CmdUserAccess.Execute

    to execute the SQL.

    0 comments No comments
  2. Anonymous
    2022-12-10T14:40:00+00:00

    ...still issue on below RstUserAccess.Close coding:

    Thanks

    0 comments No comments
  3. HansV 462.4K Reputation points MVP Volunteer Moderator
    2022-12-10T14:53:49+00:00
    1. There is no need to set the ActiveConnection to Nothing if the recordset has already been closed, so you can simple delete that line.
    2. I repeat: why use a recordset at all?
    0 comments No comments
  4. Anonymous
    2022-12-10T15:16:29+00:00
    1. There is no need to set the ActiveConnection to Nothing if the recordset has already been closed, so you can simple delete that line.
    2. I repeat: why use a recordset at all?
    1. Issue again:

    1. These are routine done in the past linked to an MSAccess database.

    The only thing I have to do is changing few parameters on my query except the line which is giving me the error :(

    Regards

    0 comments No comments
  5. HansV 462.4K Reputation points MVP Volunteer Moderator
    2022-12-10T15:18:12+00:00

    Remove the line that sets the ActiveConnection to Nothing!!!!!!!!

    0 comments No comments