Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Get the Name of the Currently Open Database

Status
Not open for further replies.

tbaguio

Programmer
Sep 25, 2000
41
CA
Came across two nifty functions that enable you to find out the name and path of the currently open database for people using Access 97. Thought it would be of use. I mostly use it in the footers of my reports combined with the CurrentObjectName function to help me figure out which report came from which app.

Code:
Public Function GetDbPath() As String
    Dim myDB As Database
    
    Set myDB = CurrentDb()
    GetDbPath = myDB.Name
    
End Function

Public Function GetDBName()

    Dim myDB As Database
    Dim myDBPath As String
    
    Dim intPosition1 As Integer
    Dim intPosition2 As Integer
            
    Set myDB = CurrentDb()
    myDBPath = myDB.Name
    
    intPosition1 = Len(myDBPath)
    intPosition2 = InStr(1, myDBPath, "\")
    
    If intPosition2 = 0 Then
        MsgBox "Error", vbCritical
    
    Else
        intPosition2 = InStr(intPosition1, myDBPath, "\")
        
        Do While (intPosition2 < 1)
        
            intPosition1 = intPosition1 - 1
            intPosition2 = InStr(intPosition1, myDBPath, &quot;\&quot;)
            
        Loop
        
        GetDBName = Right(myDBPath, Len(myDBPath) - intPosition2)
        
    End If
            
End Function

If you're using Access 2000/2002 you can utilize this instead for the database filename:
Code:
CurrentProject.Name

Or this for the database path:
Code:
CurrentProject.Path

Hope you found this as helpful as I did!

Theresa


&quot;Sleep is the best meditation.&quot; - Dalai Lama
 
Just my 2ps worth!
If you want just the db name, then

Function GetDbName as string
DBName=dir(currentdb.name)
end function

if you want just the Path to the Db then

Function GetDbLocation As String
GetDbLocation=left(Currentdb.name,len(Currentdb.name)-len(Currentdb.name))
End Function


Ben ----------------------------------
Ben O'Hara
bo104@westyorkshire.pnn.police.uk
----------------------------------
 
Thanks Ben. It's always helpful to have shorter ways of doing things!

Theresa &quot;Sleep is the best meditation.&quot; - Dalai Lama
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top