Access VBA connection to test existence of SQL Server -
i have access application needs connect 1 of several possible sql servers (i.e., connect linked tables) , have list of possible sql server instance names. when application launches, needs go see of possible servers available. considering sluggishness of solutions using sqlbrowseconnect or netserverenum, i'm wondering if there clean , fast way 'ping' sql server based on name.
we use pass-through query, verifyconnection, opens small table.
the test alters connection , checks if can read table:
public function issqlserver( _ byval testnewconnection boolean, _ optional byval hostname string, _ optional byval database string, _ optional byval username string, _ optional byval password string, _ optional byref errnumber long) _ boolean const cstrquery string = "verifyconnection" dim dbs dao.database dim qdp dao.querydef dim rst dao.recordset dim booconnected boolean dim strconnect string dim strconnectold string dim boocheck boolean set dbs = currentdb set qdp = dbs.querydefs(cstrquery) if hostname & database & username & password = "" if testnewconnection = false ' verify current connection. boocheck = true else ' fail. no check needed. ' new connection cannot checked empty parameters. end if else strconnectold = qdp.connect strconnect = connectionstring(hostname, database, username, password) if strconnect <> strconnectold if testnewconnection = false ' fail. no check needed. ' tables connected database. else ' check new connection. qdp.connect = strconnect boocheck = true end if else ' check current connection. strconnectold = "" boocheck = true end if end if on error goto err_issqlserver ' perform check of new connection or verify current connection. if boocheck = true set rst = qdp.openrecordset() ' tried connect ... if errnumber = 0 if not (rst.eof or rst.bof) ' success. booconnected = true end if rst.close end if if strconnectold <> "" ' restore old connection parameters. qdp.connect = strconnectold end if end if set rst = nothing set qdp = nothing set dbs = nothing issqlserver = booconnected exit_issqlserver: exit function err_issqlserver: ' return error. errnumber = err.number errormox "tilslutning af database" ' resume able restore qdp.connect strconnectold. resume next end function
this way check complete route way single table.
Comments
Post a Comment