none
In VBA how do I now when a Database variable has not been initialised? RRS feed

  • Question

  • My idea was this, I declare the object, and set it once...

    Private dbLeghe As Database Public Sub OpenLegaDb()

    ' Only open the database if it has not already been done If dbLeghe Is Nothing Then Set dbLeghe = OpenDatabase("C:\PrevColl\Solder.mdb", , True) End If End Sub

    ...but I don't understand how to check if it dbLeghe has been set or not. Is Nothing does not seem to work...


    http://www.ransen.com Cad and Graphics software

    Wednesday, September 26, 2018 2:36 PM

Answers

  • Looking at your code, I imagine the If statement is always True, correct? If so, are you saying dbLeghe still does not get set even after the Set line? If you say yes, then perhaps the issue is in the use of the Private scope for the variable.

    Just my 2 cents...

    No, the if statement is not always true, because after the first time that I call OpenLegDb it dbLeghe has been set to something, i.e. not Nothing.

    But the error was mine, I ported from a locally opened db to a globally opened one, but forgot to remove the .Close in one of the ported functions. Arrrg!

    Sorry to have wasted your time everyone!



    http://www.ransen.com Cad and Graphics software

    • Marked as answer by Owen Ransen Thursday, September 27, 2018 7:16 AM
    Thursday, September 27, 2018 7:16 AM

All replies

  • Hi,

    Is Nothing should work. Why do you say it does not seem to work? How exactly are you verifying if it's working or not?

    Looking at your code, I imagine the If statement is always True, correct? If so, are you saying dbLeghe still does not get set even after the Set line? If you say yes, then perhaps the issue is in the use of the Private scope for the variable.

    Just my 2 cents...

    Wednesday, September 26, 2018 3:06 PM
  • the "standard" version of this supports .DBguys analysis. But the question is how to access that database after it's initialized?

    This should work

    Option Compare Database
    Option Explicit
    
    
    Private m_db As DAO.Database
    
    Public Property Get CurrentDbC() As DAO.Database
    
        If (m_db Is Nothing) Then
            Set m_db = CurrentDb
        End If
        
        Set CurrentDbC = m_db
    
    End Property
    
    which I attribute to Dirk Goldgar


    peter n roth - http://PNR1.com, Maybe some useful stuff

    Wednesday, September 26, 2018 4:27 PM
  • Looking at your code, I imagine the If statement is always True, correct? If so, are you saying dbLeghe still does not get set even after the Set line? If you say yes, then perhaps the issue is in the use of the Private scope for the variable.

    Just my 2 cents...

    No, the if statement is not always true, because after the first time that I call OpenLegDb it dbLeghe has been set to something, i.e. not Nothing.

    But the error was mine, I ported from a locally opened db to a globally opened one, but forgot to remove the .Close in one of the ported functions. Arrrg!

    Sorry to have wasted your time everyone!



    http://www.ransen.com Cad and Graphics software

    • Marked as answer by Owen Ransen Thursday, September 27, 2018 7:16 AM
    Thursday, September 27, 2018 7:16 AM
  • Hi Owen,

    Glad to hear you got it sorted out. Good luck with your project.

    Thursday, September 27, 2018 2:55 PM