sql server - Error assigning pass through query to report recordsource in VBA code -
i have following code :
private sub report_open(cancel integer) on error goto errorhandler dim isptqueryexists integer isptqueryexists = isquery("mytestquery") if isptqueryexists = -1 set qdf = currentdb.querydefs("mytestquery") qdf.sql ="my sql query" me.recordsource = "mytestquery" end if else dim databasename string dim servername string servername = "xxxx" databasename = "xxxx" set qdf = currentdb.createquerydef("mytestquery") strconnectionstring = "xxxx" qdf.connect = strconnectionstring qdf.sql = "my sql query" me.recordsource = "mytestquery" end if errorhandler: msgbox err.description end sub ******************************************************** ' function: istablequery() ' ' purpose: determine if table or query exists. ' ' arguments: ' dbname: name of database. if database name ' "" current database used. ' tname: name of table or query. ' ' returns: true (it exists) or false (it not exist). ' '******************************************************** function isquery(qname string) integer dim found integer dim queryname string const name_not_in_collection = 3265 ' assume table or query not exist. found = false ' trap errors. on error resume next ' see if name in queries collection. queryname = currentdb.querydefs(qname$).name if err <> name_not_in_collection found = true isquery = found end function
i have report . i trying set record source of query dynamically vba code pass through query .
i dont have query def created @ design time
, not specifying record source report , design time
in code , checking if query def exists , if , changing sql property of , setting record source .
if query def not available , creating new 1 setting record source.
the problem here : whenever execute code , showing error:
"item not found in collection" . if click on ok button of message box continues run , displays report
.
but , not sure why error occuring ,and why code not failing error ?
any thoughts on this? . have put lot of efforts tounderstand in vain
updated :
if dont use error handler , not throwing exception
Comments
Post a Comment