Using a database within a tab panel

I’m just coding a simple database app that uses a tabpanel on the main window. I have the usual settings that I normally use which are:

App.module (dbModule)
dbModule property ‘UsersDB’ as ‘SQLiteDatabase’ ’ The one and only global declaration used

Window1 open event:

Dim UsersDB as New SQLiteDatabase
UsersDB.DatabaseFile = GetFolderItem("DatabaseFileName") ' Real database file name inserted here
If UsersDB.Connect Then
Msgbox("Database opened OK")
Else
Msgbox("Database failed to open")
End

All works as expected so far and the database opens correctly. I then have a tabpanel front & centre on Window1. When the user selects a tab, the ‘Change’ event for the tabpanel looks like this:

Select Case Me.Value
  Case 0
    Msgbox("Tab 0")
  Case 1
    Msgbox("Tab 1")
  Case 2
    Msgbox("Tab 2")
  Case 3
    Msgbox("Tab 3")
  Case 4
    Msgbox("Tab 4")
    UpdateTabPanel4
End

‘UpdateTabPanel4’ (Which is a method of Window1) then contains this code :

Dim rs As RecordSet
rs=UsersDB.SQLSelect(SQL Statement goes here)

I then get a nil object exception for ‘rs’ at line 2 for the SQLSelect, suggesting that the database connection is not visible or accessible at this point.

Shouldn’t this talk to the database connection I’ve already established ? Or do I need to repeat the code from the Window1 open event in each of the methods used when a tabpanel is selected ? I thought that since the tabpanel is a child of Window1 that the connection I have already established would be visible.

Have I missed something obvious ?

The problem is here:

Dim UsersDB as New SQLiteDatabase

This creates a local, private variable that has the same name as your global variable. Consequently, all subsequent operations in app.Open operate on the local variable, leaving the global variable nil. Change that one line to

UsersDB = New SQLiteDatabase

Thanks Tim, that was it.

I can stare at code for ages sometimes and think it looks right, even when it obviously isn’t.