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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to return if opened exclusively?

Status
Not open for further replies.

DRH192

Programmer
Apr 25, 2005
96
GB
I want to run a procedure that transfers a large number of spreadsheets from the database to a location on the LAN. Everything works fine but I would like to improve the procedure by only allowing the data to be exported if the user has exclusive access. I want to do this for two reasons.

1 I can be sure that there are no dirty records
2 It will not slow the database down while the output queries are run.

many thanks
 
I got this from Microsoft and have not used it, it may help:
Code:
Function IsDbOpenedExclusively(strDbPath As String) As Boolean
'If this function generates error 3045 or error 3356, 
'then it returns True because these errors indicate 
'that the database is already opened exclusively.

    On Error Resume Next
    Dim dbe As PrivDBEngine
    Dim wrk As Workspace
    Dim dbs As Database
    Const conFileInUse = 3045
    Const conDBOpenedExclusively = 3356

    ' Return reference to private DBEngine object.
    Set dbe = New PrivDBEngine
    ' Return reference to default workspace.
    Set wrk = dbe.Workspaces(0)
    ' Attempt to open database.
    Set dbs = wrk.OpenDatabase(strDbPath)
    ' If reference to database isn't returned, check error.
    If dbs Is Nothing Then
        ' If error indicates database is open exclusively, return True.
        If (Err = conFileInUse Or Err = conDBOpenedExclusively) Then
            IsDbOpenedExclusively = True
        ' If unanticipated error occurs, display message.
        Else
            MsgBox "Error: " & Err & ": " & vbCrLf & Err.Description
        End If
    ' If reference to database is returned, it must not be opened
    ' exclusively by any other user.
    Else
        IsDbOpenedExclusively = False
        dbs.Close
        Set dbs = Nothing
    End If
End Function

Or do you wish to just open the tables exclusively?
 
Remou

I've also been interested in something like this. I have a networked database that I need to work exclusively on from time to time, would love for a message to appear when attempting to open by other users that said something to the effect,

"Database Currently Down For Maintenance, try back in 15 minutes"

Do you think the code you listed above would work or could be modified for that purpose??
 
Knicks
I think there may be a better way, though I can think of one at the moment (and may never :) ). If you have a front-end back-end set up, the above may be feasible. Hmm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top