Do I close the connection properly? (1 Viewer)

selvsagt

Registered User.
Local time
Today, 14:55
Joined
Jun 29, 2006
Messages
99
I am wondering if I am closing/cleaning up a connection properly.
I have found that over time this functions gets into trouble. First it gets slow, after I while I get "unrecognized database format" and other nasty messages. The database itself is not corrupted

If I close the application and start over again it still does not work. When everyone logges of for the night, it works again the next morning until it again gets into trouble. If think I "max out" the connections or something like that.

Is it sufficient to have .close or do I need to add a
"set something to = Nothing" somewhere?
How would I add a "nothing" line in the statement below?

This Is my code
Code:
Private Sub cmdOpenRGliste_Click()
On Error GoTo err_rgliste

With CurrentDb.QueryDefs("qry_conn_RGAccountListe_verdi")
  .SQL = "exec dbo.SP_rgListe @rgID = " & Forms!tblsplash.txtrgID & ";"
  .Close
End With

DoCmd.OpenForm "frmRGlisteMain"
Exit Sub

err_rgliste: MsgBox Error$
Exit Sub
End Sub

I use a pass-through query, that stores the connection string, and modify the sql with the code above.

The Stored Procedure is just a basic select statement:
Code:
SELECT * from dbo.tbl_account where dbo.tbl_account.rgID = @rgID
 

Ranman256

Well-known member
Local time
Today, 08:55
Joined
Apr 9, 2015
Messages
4,337
There's no need to make a connection in access. You're already in the db.(SO is the passthru query right?)
This code is not needed,
Just run the query:

Docmd.openquery "qry_conn_RGAccountListe_verdi"
Docmd.openform "frmRGlisteMain"
 

selvsagt

Registered User.
Local time
Today, 14:55
Joined
Jun 29, 2006
Messages
99
The pass-through query contains only the actual value that is sent to the server. So in order to change the content of the passTrough I change the sql so that the right rgID is present in the PT.

Code:
This is the content of the PT:
exec dbo.SP_rgListe @rgID = 1;

When I say ".close" I dont think I close the connection to the server. I just close the query. When I run the same PT again, I think I get another connection, and so on.
So, if that is true, how do I close the PT connection after I am finished fetching data?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 07:55
Joined
Feb 28, 2001
Messages
27,348
Don't explicitly close anything you didn't explicitly open.

When you close Access normally, the task exit code will search for everything you opened and will close it for you. This is called "process rundown" or "task rundown" and is part of every task, process, and service in Windows.

For application objects like Excel or PowerPoint or Word, if you had opened them to create or modify a file, I would explicitly close the file -just to be nice to the file. The app itself? The ONLY Office component that is touchy about this is Outlook, which will not allow you to have multiple copies of it open at the same time.

Note that there IS an exception to all of this. If you are shutting down because you trapped an error and need to abort, it wouldn't hurt to close anything you knew was still open - but if your error handler simply shunts the trap into a normal application exit, you don't need to do anything extra.
 

Users who are viewing this thread

Top Bottom