# Close database

**URL:** <https://forum.xojo.com/t/close-database/16034>\
**Category:** Databases\
**Created:** [April 26, 2014, 2:57pm UTC](https://forum.xojo.com/t/close-database/16034 "2014-04-26T14:57:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre-Guy\_Lecompte](https://forum.xojo.com/user_avatar/forum.xojo.com/pierre-guy_lecompte/32/21567_2.png) [@Pierre-Guy\_Lecompte](https://forum.xojo.com/u/Pierre-Guy_Lecompte)\
**Post date:** [April 26, 2014, 2:57pm UTC](https://forum.xojo.com/t/close-database/16034/1 "2014-04-26T14:57:08Z")

</div>

Hi everybody,  
I try to close my db (database)  
I have 2 methods (linked with 2 buttons) :

- DB\_Ouvrir
- DB\_Fermer

  
// DB\_Ouvrir  
// 26/04/14 - Méthode pour ouvrir la base de donnée.  
// Modifiée :  
//

Dim dialog As OpenDialog  
Dim file As FolderItem

dialog = New OpenDialog // create the dialog (does not actually show it)

// The filter is what type of file you are looking for  
// The filters are defined in the File Types dialog  
// inside the edit menu  
dialog.Filter = “sqlite” // “text/plain”

dialog.PromptText = “Sélectionner la base de donnée.”// The OpenDialog class supports custom prompts  
dialog.Title = “Ouvrir fichier”// and custom title’s

// Also, you can display the dialogs as sheets  
// by calling dlg.showModalWithin(self)  
//  
// For regular modal dialogs, just call dlg.showModal  
file = dialog.ShowModal

//then test for nil to see if the user clicked cancel  
If file \<\> Nil Then  
SelectedFolderItemLabel.Text = file.AbsolutePath

```
Dim dbFile As FolderItem
Dim db As New SQLiteDatabase
'dbFile = GetFolderItem("database_005.sqlite")
dbFile = GetFolderItem(SelectedFolderItemLabel.Text)

db.DatabaseFile = dbFile
If db.Connect Then
  
  'mDB=db
  'mDb.DatabaseFile = dbFile
  
  OpenStatusLabel.Text = "SQLite database est ouverte"
  Toolbar_11.Tool_Ouvrir.Enabled = False
  Toolbar_11.Tool_Fermer.Enabled = True
  
  //proceed with database operations here..
Else
  MsgBox("Erreur d'ouverture de la Database : " + db.ErrorMessage)
  Toolbar_11.Tool_Ouvrir.Enabled = True
  Toolbar_11.Tool_Fermer.Enabled = False
  
End If

```

Else  
SelectedFolderItemLabel.Text = “User clicked Cancel.”  
End If  
  
// DB\_Fermer  
// 26/04/14 - Méthode pour fermer la base de donnée.  
// Modifiée :  
// 26/04/14 - Problème avec db.Close

if db.Connect then  
 If db.Close Then // Base fermée \<- Line 7  
Toolbar\_11.Tool\_Ouvrir.Enabled = True  
Toolbar\_11.Tool\_Fermer.Enabled = False  
Else // base ouverte  
Toolbar\_11.Tool\_Ouvrir.Enabled = False  
Toolbar\_11.Tool\_Fermer.Enabled = True  
End If  
msgbox(“base fermée”)  
else  
msgbox(“Basse non connectée”)  
end if  


I have a error :  
Window1\_DB\_Fermer, line 7 - This method doesn’t return a value  
if db.Close Then // Base fermée

I don’t understand why ?  
When I trace on this method, db is nil. Why, normally is declare like SQLiteDatabase

I have on Properties :  
Name : db  
Type : SQLiteDatabase  
Scope : Public

What is false ?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 26, 2014, 2:59pm UTC](https://forum.xojo.com/t/close-database/16034/2 "2014-04-26T14:59:54Z")

</div>

```auto
if db.Connect then
  db.Close() // Base ferme <- Line 7
  Toolbar_11.Tool_Ouvrir.Enabled = True
  Toolbar_11.Tool_Fermer.Enabled = False

  msgbox("base ferme")
else
  msgbox("Basse non connecte")
end if
```

Also, be aware that, unless there is a problem, db.Connect will establish a connection to your database even if was already closed.

---

<div class="post-metadata">

**Author:** ![Pierre-Guy\_Lecompte](https://forum.xojo.com/user_avatar/forum.xojo.com/pierre-guy_lecompte/32/21567_2.png) [@Pierre-Guy\_Lecompte](https://forum.xojo.com/u/Pierre-Guy_Lecompte)\
**Post date:** [April 26, 2014, 3:10pm UTC](https://forum.xojo.com/t/close-database/16034/4 "2014-04-26T15:10:00Z")

</div>

Kem, I don’t understand.  
My base is correctly open.  
But why db is not identify on this method ?

I Work on Mac, can somebody help me with ‘Messages’ on Sharescreen, or with ‘TeamViewer’ ?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 26, 2014, 3:26pm UTC](https://forum.xojo.com/t/close-database/16034/5 "2014-04-26T15:26:57Z")

</div>

The db.Close method does not return a value. You simply call it and it closes. See

[http://documentation.xojo.com/index.php/Database.Close](http://documentation.xojo.com/index.php/Database.Close)  
If the database had not been connected, it does nothing, but you can’t test that.

db.Connect does return a value, but will connect the database even if it wasn’t connected before. In the code you posted, the Else block should never execute.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [April 26, 2014, 8:06pm UTC](https://forum.xojo.com/t/close-database/16034/6 "2014-04-26T20:06:56Z")

</div>

> [@82900:@Pierre-Guy Lecompte](#):
>
> Dim db As New SQLiteDatabase

You are creating a local variable in DB\_Ouvrir. The variable is not available in DB\_Fermer. Use a property instead.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:11pm UTC](https://forum.xojo.com/t/close-database/16034/7 "2020-10-29T17:11:57Z")

</div>


