Solved

Check to see if database object is open

Posted on 2004-10-11
6
256 Views
Last Modified: 2006-11-17
With the following how do I check and see if the database object has opened?  I can check to be if it is set to something, but how do I check to see if it has been opened or not?


Global dbsNew      As Database

    Set dbsNew = wrkDefault.CreateDatabase(DBFilename, _
        dbLangGeneral, dbEncrypt)
....
    Set dbsNew = wrkDefault.OpenDatabase(DBFilename)

***************
I tried the following, but this only shows if the database has been set.  If the code fails between the first statement
above and the next statement, how do I know the correct way to clean up my connections?  If the database is open, I want to run the SQL statement below, otherwise nothing really happened, so I don't.
If the first statement runs, and the second does not, the following code executes the else, but dbsNew is not open and the execute does not work.
***************

    If Not dbsNew Is Nothing Then
        dbsNew.Close
    Else
        strSQL = "INSERT INTO RunInfo (Program_Started, SQL_Finished, DB_Created, Register_Loaded_Count, Values_Updated_Count) "
        strSQL = strSQL & "VALUES (#" & Format(dtStarted, "YYYY-MM-DD HH:MM:SS") & "#, #"
        strSQL = strSQL & Format(dtSQLProcessed, "YYYY-MM-DD HH:MM:SS") & "#, #"
        strSQL = strSQL & Format(dtDBCreated, "YYYY-MM-DD HH:MM:SS") & "#,"
        strSQL = strSQL & intRecordCounter1 & ", " & intRecordCounter2 & ");"
        Debug.Print strSQL
        dbsNew.Execute strSQL
        dbsNew.Close
        Set dbsNew = Nothing
    End If
0
Comment
Question by:tdfreeman
  • 3
  • 2
6 Comments
 
LVL 41

Expert Comment

by:shanesuebsahakarn
Comment Utility
Can you not just assume that if the variable is not Nothing then it is open? Whenever you close it, you should set it to Nothing in any case.
0
 
LVL 1

Author Comment

by:tdfreeman
Comment Utility
<Can you not just assume that if the variable is not Nothing then it is open?>

From my errors I would have to say no, to the question.

But if I have set it but haven't opened it, it is no longer nothing, but it isn't open.  How do I check for that?  I set it when I create a database.  Later in the process I open the database to work with it.
0
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 250 total points
Comment Utility
This code:

    Set dbsNew = wrkDefault.CreateDatabase(DBFilename, _
        dbLangGeneral, dbEncrypt)

returns an *open* database object as if you have opened it with OpenDatabase. You do not have to explicitly open it with OpenDatabase.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 39

Expert Comment

by:stevbe
Comment Utility
CreateDatabase returns an open database object ... so If you do not error out it is already open and there is no need for the second line of code.

Steve
0
 
LVL 39

Expert Comment

by:stevbe
Comment Utility
oops ... that is what shane said :-)
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
Comment Utility
So I did, fancy that :-) :-)
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

728 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

10 Experts available now in Live!

Get 1:1 Help Now