sql - Terminating Query If Not Connection -
i have code uploads data sql server database, trying add in if line of code saying if there connection continue, if there not connection end. having difficult time figuring out wording , placement though. beginning of code connects is:
public function update() dim cdb dao.database, qdf dao.querydef dim rs dao.recordset dim err dao.error const connectionstring = _ "odbc;" & _ "driver={sql server native client 10.0};" & _ "server=serv;" & _ "database=db;" & _ "uid=id;" & _ "pwd=pwd;" set cdb = currentdb set qdf = cdb.createquerydef("") set rs = currentdb.openrecordset("cddata", dbopentable) qdf.connect = connectionstring while not rs.eof
while 1 can simple attempt execute query or command, “time out” , delay test active connection can result in long delay. result wonderful trick exists uses different connection mechanism in access , “reduces” potential long delay when attempting use saved query based on linked tables. (that "different" connection system occurs when create querydef opposed linked table or query based on linked table sql server)
the follow code return true or false if have working sql connection:
function testlogin(strcon string) boolean on error goto testerror dim dbs dao.database dim qdf dao.querydef set dbs = currentdb() set qdf = dbs.createquerydef("") qdf.connect = strcon qdf.returnsrecords = false 'any valid sql statement runs on server work below. qdf.sql = "select 1 test" qdf.execute testlogin = true exit function testerror: testlogin = false exit function
end function
so in code connection string code go:
if testlogin(strconn) = false msgbox "no connection or logon invalid" exit sub end if ' record processing code goes here successful logon/connection.
Comments
Post a Comment