LastDLLError Property Example

When pasted into a UserForm module, the following code causes an attempt to call a DLL function. The call fails because the argument that is passed in (a null pointer) generates an error, and in any event, SQL canÆt be cancelled if it isnÆt running. The code following the call checks the return of the call, and then prints at the LastDLLError property of the Err object to reveal the error code. On systems without DLLs, LastDLLError always returns zero.

 Private Declare Function SQLCancel Lib "ODBC32.dll" _
 (ByVal hstmt As Long) As Integer

Private Sub UserForm_Click()
    Dim RetVal
    ' Call with invalid argument.
    RetVal = SQLCancel(myhandle&)
    ' Check for SQL error code.    
    If RetVal = -2 Then
        'Display the information code.
        MsgBox "Error code is :" & Err.LastDllError 
    End If
End Sub