Solved

How to unlock the database Which is being accessed ?

Posted on 1998-07-16
5
135 Views
Last Modified: 2010-05-03
My question is how can i unlock the database (data.mdb) which is being accesses by the my vb program.
The reason i want to do that is because i want to replace the current updated  database
with the original one (which is the backup .mdb) when the user press close button.

netcool
0
Comment
Question by:netcool
  • 3
5 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 1466057
You need to close the database and set the object to nothing I think

db.close
set db = nothing


0
 
LVL 5

Author Comment

by:netcool
ID: 1466058
deighton,

i tried but not working. Let me explain what i encouter and what ineed to be done.
I open the database and many recordset during my program running, so now i want to unlock/close  the database in order to delete the database and replace it with new one
(same name) .I can't delete the mdb file because it atatched with .ldb file unless i exist my program.
Another question, do i have to always set db = opendatabase("c:\data.mdb") whenever
i need to open recordset set tb = db.openrecordset("Employee") ????

Netcool

0
 
LVL 18

Expert Comment

by:deighton
ID: 1466059
The database needs to be open for you to open a recordset.  But you can open a database and then open and close as many recordsets as you like on that database, before finally closing the database.

You must always make sure that your databases and recordsets have been closed after use.  Avoid the following

function misuse() as integer

dim db as database
dim rs as recordset

set db = workspaces(0).opendatabase("x.mdb")
set rs = db.openrecordset("x",dbopendynaset)





end function

You always have to have rs.close then db.close at the bottom of the function

e.g

function ok() as integer

dim db as database
dim rs as recordset

set db = workspaces(0).opendatabase("x.mdb")
set rs = db.openrecordset("x",dbopendynaset)



rs.close
db.close



end function


You can  make the db a global or form level variable then do the following sort of thing

in declarations

dim db as database

the in form load or form initialse you open the database e.g. set db = workspaces..  etc

You then open and close recordsets in your form whenever you need them.  Finally you must do db.close in the form unload or  terminate event.

I think you might be leaving databases or recordsets open by accident.  See if this helps

0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1466060
Right.  And because of bugs in the MS code in various versions of VB and .dlls, it's always adviseable to set your objects = Nothing after closing them, in order to clean out all references to them in the OS.  Then you should be able to operate on your .mdbs as just another file, assuming nobody else in another app is using them.
0
 
LVL 18

Accepted Solution

by:
deighton earned 30 total points
ID: 1466061
You need to close the database and set the object to nothing I think

      db.close
      set db = nothing


      then open and close as many recordsets as you like on that database, before finally closing the
      database.

      You must always make sure that your databases and recordsets have been closed after
      use.  Avoid the following

      function misuse() as integer

      dim db as database
      dim rs as recordset

      set db = workspaces(0).opendatabase("x.mdb")
      set rs = db.openrecordset("x",dbopendynaset)





      end function

      You always have to have rs.close then db.close at the bottom of the function

      e.g

      function ok() as integer

      dim db as database
      dim rs as recordset

      set db = workspaces(0).opendatabase("x.mdb")
      set rs = db.openrecordset("x",dbopendynaset)



      rs.close
      db.close



      end function


      You can  make the db a global or form level variable then do the following sort of thing

      in declarations

      dim db as database

      the in form load or form initialse you open the database e.g. set db = workspaces..  etc

      You then open and close recordsets in your form whenever you need them.  Finally you must do
      db.close in the form unload or  terminate event.

      I think you might be leaving databases or recordsets open by accident.  See if this helps
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question