|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Function to return path of Currentdb
Is there a function that returns the path or directory of the CurrentDb?
Last edited by iluvstrats : February 5th, 2004 at 05:12 PM. |
|
#2
|
|||
|
|||
|
Dear iluvstrats,
Here is one I wrote awhile ago, I've added some comments and instructiuons for use at the top. I've never encountered any errors using this. *NOTE* You will need a reference set for Microsoft DAO Object Library Code:
Function ThisDB(Optional DesiredData As String = "fullpath") As String
'================================================= ============================
'Copyright by Heather L. Floyd - Floyd Innovations - URL
' Updated 2-6-2004
'-----------------------------------------------------------------------------
' To return desired data about the current database file
'-----------------------------------------------------------------------------
' Parameters:
' ARGUEMENT : DESCRIPTION
'-----------------------------------------------------------------------------
' DesiredData (Optional) (String) : String indicating what data you want
' returned -
' "fullpath" or "path" or "filename"
' Default is "fullpath"
'-----------------------------------------------------------------------------
' Returns:
' String of data requested based on 'DesiredData' arguement
'-----------------------------------------------------------------------------
' Example:
'-----------------------------------------------------------------------------
' ThisDB("fullpath") or ThisDB()= "C:\My Documents\My Database.mdb"
' ThisDB("path") = "C:\My Documents\"
' ThisDB("filename") = "My Database.mdb"
'================================================= ============================
On Error GoTo ErrorCode
'Variables & values
Dim db As DAO.Database
Set db = CurrentDb()
Dim strDB As String
strDB = db.Name
Dim strFullpath, strPath, strFileName As String
strFullpath = strDB
Dim iLenSDB As Integer
Dim iDotLoc As Integer
Dim iSlashLoc As Integer
Dim strModSDB As String
Dim iLenModSDB As Integer
iLenSDB = Len(strDB)
iDotLoc = InStr(strDB, ".")
strModSDB = strDB
iLenModSDB = Len(strModSDB)
iSlashLoc = InStr(strDB, "\")
'Parse
Do Until iSlashLoc = 0
iLenModSDB = Len(strModSDB)
strModSDB = Right(strModSDB, iLenModSDB - iSlashLoc)
iSlashLoc = InStr(strModSDB, "\")
Loop
strFileName = strModSDB
iLenModSDB = Len(strModSDB)
strPath = Left(strDB, iLenSDB - iLenModSDB)
'Return value
If DesiredData = "" Then
ThisDB = strFullpath
ElseIf DesiredData = "fullpath" Then
ThisDB = strFullpath
ElseIf DesiredData = "path" Then
ThisDB = strPath
ElseIf DesiredData = "filename" Then
ThisDB = strFileName
End If
ExitCode:
Exit Function
ErrorCode:
Select Case Err.number
Case Else
MsgBox "Error " & Err.number & ": " & Err.Description, vbCritical, "HLFFileUtils.ThisDB"
Resume ExitCode
End Select
End Function
Hope this helps. Heather |
|
#3
|
|||
|
|||
|
Function or command to return the path of an attached table?
Thanks. That worked great.
One more thing. Is there a function or command to return the path of of an attached table? |
|
#4
|
|||
|
|||
|
OK, so I'm a few years late, but here is an updated version of the code posted by Heather.
Fixed: -Removed irrevelent variables -Removed loop to find last backslash -Added Enum to make return options more clear -Added SELECT structure to make code a little more clear Thanks for this, Heather! Code:
Public Enum PathFormat
FullPath = 0
Path = 1
FileName = 2
End Enum
Function ThisDB(Optional DesiredData As PathFormat = FullPath) As String
'================================================= ============================
'Copyright by Heather L. Floyd - Floyd Innovations - URL
' Updated 2-6-2004
' Updated 10-30-2009 by Loopback (loopback@gregs-domain.net)
'-----------------------------------------------------------------------------
' To return desired data about the current database file
'-----------------------------------------------------------------------------
' Parameters:
' ARGUEMENT : DESCRIPTION
'-----------------------------------------------------------------------------
' DesiredData (Optional) (String) : String indicating what data you want
' returned -
' "fullpath" or "path" or "filename"
' Default is "fullpath"
'-----------------------------------------------------------------------------
' Returns:
' String of data requested based on 'DesiredData' arguement
'-----------------------------------------------------------------------------
' Example:
'-----------------------------------------------------------------------------
' ThisDB(FullPath) or ThisDB()= "C:\My Documents\My Database.mdb"
' ThisDB(Path) = "C:\My Documents\"
' ThisDB(FileName) = "My Database.mdb"
'================================================= ============================
On Error GoTo ErrorCode
'Variables & values
Dim strFullPath As String
Dim strPath As String
Dim strFileName As String
Dim intSlashPosition As Integer
strFullPath = CurrentDb().Name
intSlashPosition = InStrRev(strFullPath, "\")
strFileName = Mid(strFullPath, intSlashPosition + 1)
strPath = Left(strFullPath, intSlashPosition)
'Return value
Select Case DesiredData
Case 0:
ThisDB = strFullPath
Case 1:
ThisDB = strPath
Case 2:
ThisDB = strFileName
Case Else
ThisDB = strFullPath
End Select
ExitCode:
Exit Function
ErrorCode:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "HLFFileUtils.ThisDB"
Resume ExitCode
End Select
End Function
|
![]() |
| Viewing: ASP Free Forums > Database > Microsoft Access Help > Function to return path of Currentdb |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|