PSS ID Number: Q173463
Article Last Modified on 01-11-2001
If the WillExecute event is used to set the CursorType property, the resultset for the cursor may contain nothing if the CursorType has not been set before the WillExecute event fires. Setting a Remote Data Control resultset equal to the query resultset will cause the error:
"runtime error" 40516 Invalid Object"
The CursorType property is set but will only affect subsequent queries. The CursorType property is not set for the query that caused the WillExecute event to fire.
Set the CursorType and LockType properties before running the query.
This behavior is by design.
The purpose of the WillExecute event is to allow modifications of a query's SQL or to allow a query to be cancelled. All query properties can be modified here, but changing some properties will not affect the current query that caused the event to fire. CursorType and LockType are two properties that cannot be changed for the current query. Changing other properties, such as SQL and maxrow, will affect the resultset returned by the current query that caused the event to fire.
Option Explicit
Dim rs As rdoResultset
Dim WithEvents cn As rdoConnection
Dim SQL As String
Private Sub cn_WillExecute(ByVal Query As RDO.rdoQuery, _
Cancel As Boolean)
MsgBox Query.CursorType
Query.CursorType = rdOpenStatic
MsgBox Query.CursorType 'the cursortype has been changed
End Sub
Private Sub Form_Load()
Set cn = New rdoConnection
cn.Connect = "ucn.Connect = "uid=sa;pwd=;server=margerys;" _
& "driver={SQL & "driver={SQL Server};database=pubs;" _
& "DSN='';" & "DSN='';"
cn.CursorDriver = rdUseOdbc
cn.EstablishConnection rdDriverNoPrompt
SQL = "Select * From titles "
'
' Openresultset cause the WillExecute event to fire.
'
Set rs = cn.OpenResultset(SQL)
'
'The next command will fail because a remote data control
' cannot be assigned to nothing. rs is nothing because
' because the CursorType has not been set for the current query.
'
'Set rs = cn.OpenResultset(SQL,rdopenforwardonly)
'
'Uncomment the above line and comment the first 'set rs' and
' the problem will persist because a remote data control cannot
' be assigned to a forwardonly cursor.
'
Set MSRDC1.Resultset = rs
End Sub
Private Sub quit_Click()
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
Unload Me
End Sub
For information on WillExecute, please see the following:
Visual Basic Online Books, and the following article in the Microsoft
Knowledge Base:
Q172082 HOWTO: Handle WithEvents/RDO 2.0's in Asynchronous Processing(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Margery Simms, Microsoft Corporation
Additional query words: kbVBp500 kbVBp600 kbdse kbDSupport kbVBp KBRDO
Keywords: kbGrpDSVBDB
Issue Type: kbprb
Technology: kbVBSearch kbAudDeveloper kbZNotKeyword6 kbZNotKeyword2 kbVB500Search kbVB600Search kbVBA500 kbVBA600 kbVB500 kbVB600