How to create a ODBC connection to SQL Server? -
i try use access call stored procedure in sql server. have trouble build odbc connection, not know missing something? or need set in sql site?
i have screen this:
and code behind ok button this:
dim dbpubs dao.database dim tdfpubs dao.tabledef dim qdfpubs dao.querydef dim strmsg string dim strsql string ' check existence of server, database , user name. ' if missing, inform user , exit. if isnull(me!txtserver) strmsg = "enter name of company's server." & _ & "(see database administrator)" msgbox strmsg, vbinformation, "missing data" me!txtserver.setfocus elseif isnull(me!txtdatabase) strmsg = "enter name of database. (example: xxxx)" msgbox strmsg, vbinformation, "missing data" me!txtdatabase.setfocus elseif isnull(me!txtuid) strmsg = "enter user login. (example: xx)" = "" msgbox strmsg, vbinformation, "missing data" me!txtdatabase.setfocus else strserver = me!txtserver strdatabase = me!txtdatabase struid = me!txtuid ' password may null, provide possibility strpwd = nz(me!txtpwd, "") ' prepare connection string strconnect = "odbc;driver={sql server}" _ & ";server=" & strserver _ & ";database=" & strdatabase _ & ";uid=" & struid _ & ";pwd=" & strpwd & ";" end if private function validateconnectstring() boolean on error resume next err.clear docmd.hourglass true ' assume success validateconnectstring = true ' create test query , set properties set qdfpubs = dbpubs.createquerydef("") qdfpubs.connect = strconnect qdfpubs.returnsrecords = false qdfpubs.odbctimeout = 5 ' attempt delete record doesn't exist qdfpubs.sql = "delete authors au_lname = 'lesandrini'" ' test 1 pass through query see previous ' connect string still valid (server has not changed) qdfpubs.execute ' if there error, connection failed if err.number validateconnectstring = false set qdfpubs = nothing docmd.hourglass false end function
you should pay visit connectionstrings site details, however, wouldn't use odbc if you.
my connection (for sql server 2012):
private ocon adodb.connection
public sub initconnection(byref sdatasource string, byref sdbname string) dim sconstr string set ocon = new adodb.connection sconstr = "provider=msdatashape;data provider=sqlncli11;" & _ "integrated security=sspi;persist security info=false;data source=" & _ sdatasource & ";initial catalog=" & sdbname on error resume next call ocon.open(sconstr) if (err.number = 0) 'all ok else 'show error message / throw / sink / etc end if on error goto 0 end sub
where sdatasource
"[computername]\[sql server instance]" (same in e.g. ssms, it's "myhomepc\sqlexp") , sdbname
default catalog, ie default db open. you'll need add reference microsoft activex data objects
can use adodb
connection
, command
, recordset
objects (in access vb window: "tools" --> "references...").msdatashape
not mandatory comes handy hierarchical grids.
edit: btw, connstr. site: driver={sql server native client 11.0};server=myserveraddress;database=mydatabase; uid=myusername;pwd=mypassword;
(again, sql server 2012, 2008 it's "...client 10.")
Comments
Post a Comment