Solved

How to unlock the database Which is being accessed ?

Posted on 1998-07-16
5
132 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now