Deleting file and path

L

Lars Brownies

I have a form that is bound to a hyperlink table. I have a delete button
with which you can delete the particular hyperlink record including the
underlying file.

The function that executes the code, first tries to delete the file. If
that's successful, the hyperlink record is deleted. There could be
situations where the hyperlink record can't be deleted for instance because
it's locked. In that case the function would already have deleted the file,
while the hyperlink record remains there.

Is there a way I could perhaps 'pre-lock' the hyperlink record, so I'm 100%
sure it can be deleted after the file has been deleted? Any other hints?

Thanks,

Lars
 
H

Hans Up

Lars said:
I have a form that is bound to a hyperlink table. I have a delete button
with which you can delete the particular hyperlink record including the
underlying file.

The function that executes the code, first tries to delete the file. If
that's successful, the hyperlink record is deleted. There could be
situations where the hyperlink record can't be deleted for instance
because it's locked. In that case the function would already have
deleted the file, while the hyperlink record remains there.

Is there a way I could perhaps 'pre-lock' the hyperlink record, so I'm
100% sure it can be deleted after the file has been deleted? Any other
hints?

On the Data tab of your form's property sheet, you can set Record Locks
to Edited Record. Then you can try to acquire a lock on the current
record with the form's RecordsetClone before deleting it. That lock
attempt will fail if another user has an edit in progress on the same
record in another copy of your form.

Private Sub cmdDelete_Click()
Dim rst As DAO.Recordset
Dim strMsg As String

On Error GoTo ErrorHandler

Set rst = Me.RecordsetClone
rst.Bookmark = Me.Bookmark

'try to lock current record
rst.Edit 'error 3218 if unable to lock

'If <file delete successful> Then
' rst.Delete
'End If

ExitHere:
Set rst = Nothing
On Error GoTo 0
Exit Sub

ErrorHandler:
Select Case Err.Number
Case 3218
strMsg = "Record locked -> unable to delete."
Case Else
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure cmdDelete_Click"
End Select
MsgBox strMsg
GoTo ExitHere

End Sub

I don't know what you have in mind for the "<file delete successful>"
condition. But the part that I supplied seems to work in Access 2003.
 
L

Lars Brownies

Excellent! thank you.

Lars

Hans Up said:
On the Data tab of your form's property sheet, you can set Record Locks to
Edited Record. Then you can try to acquire a lock on the current record
with the form's RecordsetClone before deleting it. That lock attempt will
fail if another user has an edit in progress on the same record in another
copy of your form.

Private Sub cmdDelete_Click()
Dim rst As DAO.Recordset
Dim strMsg As String

On Error GoTo ErrorHandler

Set rst = Me.RecordsetClone
rst.Bookmark = Me.Bookmark

'try to lock current record
rst.Edit 'error 3218 if unable to lock

'If <file delete successful> Then
' rst.Delete
'End If

ExitHere:
Set rst = Nothing
On Error GoTo 0
Exit Sub

ErrorHandler:
Select Case Err.Number
Case 3218
strMsg = "Record locked -> unable to delete."
Case Else
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure cmdDelete_Click"
End Select
MsgBox strMsg
GoTo ExitHere

End Sub

I don't know what you have in mind for the "<file delete successful>"
condition. But the part that I supplied seems to work in Access 2003.
 
H

Hans Up

Lars said:
Excellent! thank you.

You're welcome. Glad it worked.

Does your application need to account for users adding, renaming, or
deleting files on disk without performing those operations through your
app? It seems to me the situation could become more challenging than I
first considered.
 
L

Lars Brownies

Nope. All those actions are done within the application. Users have a
private app import folder in which they put the file. With a click this file
is transfered to a specific app folder where all app files reside.

Lars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top