Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close an ODBC SQL 2000 connection from Access 97?

Status
Not open for further replies.

ordendelfai

Technical User
Nov 8, 2002
187
US
I am using the below code to automatically connect to SQL 2000 server with Access 97, and it works great. However, my IS department is telling me that my I keep having multiple sessions open on the SQL server. I thought when I close Access, my SQL session closes too. They told me to deallocate the resources, and close the connection with VBA.

I am a novice with VBA, and the below code was lots of borrowing, tweaking, and trial and error. Unfortunately my IS department will not give me any code to accomplish what they are asking.

Is there some code I should add when the database is closed? Would really appreciate if you could help ;)

Thx!


*******CODE*******

Public Function LinkTables()

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim RST As DAO.Recordset
Dim strServer As String
Dim strDB As String
Dim strTable As String
Dim strConnect As String
Dim strMsg As String
Const acbSwitchboard = "frmSWITCHBOARD"

On Error GoTo HandleErr

'Build base Authentication strings.
strConnect = "ODBC;Driver={SQL Server};Server=WH-SQLDEVVS01;Database=SPContact;Trusted_Connection=Yes"

'Create a recordset to obtain server object names
Set db = CurrentDb()
Set RST = db.OpenRecordset("tblSQLTables", dbOpenSnapshot)
If RST.EOF Then
strMsg = "There are no tables listed to link to."
MsgBox strMsg, , "No tables"
GoTo ExitHere
End If

Forms.frmSPLASH.[LblFunction].Caption = "Relinking:"
Do Until RST.EOF
strServer = RST!SQLServer
strDB = RST!SQLDatabase
strTable = RST!SQLTable
Forms.frmSPLASH.[lblLoading].Caption = strTable
DoCmd.RepaintObject acForm, "frmSPLASH"
' Create a new TableDef object.
Set tdf = db.CreateTableDef(strTable)
' Set the Connect property to establish the link
tdf.Connect = strConnect & "Server=" & strServer & ";Database=" & strDB & ";"
tdf.SourceTableName = strTable
' Append to the database's TableDefs colelction.
db.TableDefs.Append tdf
RST.MoveNext
Loop

RST.Close
Set RST = Nothing
Set tdf = Nothing
Set db = Nothing

ExitHere:
Exit Function


HandleErr:
Select Case Err
Case Else
strMsg = Err & ": " & Err.Description
Exit Function
End Select

End Function
 
Hi,
Just add that:
Rst.close
Set RST = Nothing
tdf.Close
Set tdf = Nothing
Db.Close
Set db = Nothing


Jean-Paul
Montreal
To send me E-Mail, remove “USELESSCODE”.
jp@USELESSCODEsolutionsvba.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top