DAO and Table Locking

J

Jonathan Wood

Greetings,

I've written some code to create a relationship between table A and table B.
The code looks something like this:

Set rel = db.CreateRelation("A_B")
rel.Table = "A"
rel.ForeignTable = "B"
Set fld = rel.CreateField("AID")
fld.ForeignName = "BID"
rel.Fields.Append fld
db.Relations.Append rel

This code works just fine except that a few lines later, I use DLookup() to
find a row in table B, but I then get an error that table B is exclusively
locked and cannot be accessed.

Can anyone clarify why table B has been locked and how to unlock it?

Thanks.

Jon
 
T

Tom van Stiphout

On Thu, 4 Mar 2010 23:54:17 -0700, "Jonathan Wood"

You may need to add:
db.Tabledefs.Refresh

You know you can create relationships at design time as well, right?

-Tom.
Microsoft Access MVP
 
J

Jonathan Wood

Tom said:
You may need to add:
db.Tabledefs.Refresh

I tried that before the offending code but still get the error: Run-time
error '3008': The table 'B' is already opened exclusively by another user,
or it is already open through the user interface and cannot be manipulated
programmatically.

The table is not opened in Access.
You know you can create relationships at design time as well, right?

Yes, of course.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
 
T

Tom van Stiphout

On Fri, 5 Mar 2010 07:49:32 -0700, "Jonathan Wood"

OK, this peeks my interest. Can you email me a sample program,
stripped down to the bare essentials?
My spam trap should be easily avoidable by humans.

-Tom.
Microsoft Access MVP
 
P

Paul Shapiro

Jonathan Wood said:
I've written some code to create a relationship between table A and table
B. The code looks something like this:

Set rel = db.CreateRelation("A_B")
rel.Table = "A"
rel.ForeignTable = "B"
Set fld = rel.CreateField("AID")
fld.ForeignName = "BID"
rel.Fields.Append fld
db.Relations.Append rel

This code works just fine except that a few lines later, I use DLookup()
to find a row in table B, but I then get an error that table B is
exclusively locked and cannot be accessed.

Here is the function I use for creating new relationships. I've always been
able to continue working in the db after running this code.

Public Function pjsCreateRelationship( _
dbData As DAO.Database, _
strRelationName As String, _
strParentTable As String, _
strChildTable As String, _
astrFieldNamesPrimary() As String, _
astrFieldNamesForeign() As String, _
Optional fCascadeDelete As Boolean = False, _
Optional fCascadeUpdate As Boolean = False _
) As Boolean
On Error GoTo ErrorHandler
Dim fSuccess As Boolean, relTemp As DAO.Relation, _
lngLoop As Long, lngAttributes As Long

'Make sure we were passed compatible field name arrays
If LBound(astrFieldNamesPrimary) = 0 And LBound(astrFieldNamesForeign) =
0 _
And UBound(astrFieldNamesPrimary) = UBound(astrFieldNamesForeign) Then
fSuccess = True 'Assume we succeed
'See if relationship name already exists
Set relTemp = dbData.Relations(strRelationName)
Else 'Bad calling parameters
Err.Raise Number:=9999, Description:="Programmer Error: Invalid
calling parameters"
End If

If relTemp Is Nothing Then 'Return here from error handler
With dbData
lngAttributes = IIf(fCascadeDelete, dbRelationDeleteCascade, 0)
+ _
IIf(fCascadeUpdate, dbRelationUpdateCascade, 0)
Set relTemp = .CreateRelation(strRelationName, strParentTable,
strChildTable, lngAttributes)

With relTemp
For lngLoop = 0 To UBound(astrFieldNamesPrimary)
.Fields.Append
..CreateField(astrFieldNamesPrimary(lngLoop))
.Fields(lngLoop).ForeignName =
astrFieldNamesForeign(lngLoop)
Next lngLoop
End With
.Relations.Append relTemp
.Relations.Refresh
End With
CurrentDb.Relations.Refresh
Else 'Could verify the existing relationship has the same attributes
and fields
End If

ExitHandler:
On Error Resume Next
pjsCreateRelationship = fSuccess
Set relTemp = Nothing
Exit Function

ErrorHandler:
Select Case Err.Number
Case 3265 'Name doesn't exist in this collection
Resume Next
Case Else
'Your error handling code
fSuccess = False
End Select
Resume ExitHandler
Resume
End Function
 
J

Jonathan Wood

Tom said:
OK, this peeks my interest. Can you email me a sample program,
stripped down to the bare essentials?
My spam trap should be easily avoidable by humans.

Thanks, I think I may have resolved this (and I can't really send it to you
without retyping everything--including the data--because this work is being
done over the Internet using VPN and Remote Desktop)

Basically, it's the code I posted followed by the DLookup() call that causes
the error. However, there is one more element: I was creating a transaction
(calling BeginTrans). If I don't call BeginTrans, I do not get the error.

So it seems that Access won't let me access a table with pending edits
awaiting CommitTrans or a Rollback.

Does that seem right?

Thanks.

Jon
 
J

Jonathan Wood

Thanks, but my relationship seems to be created just fine. However, as
mentioned in my other post, I was also calling BeginTrans and I've found
that the error goes away if I don't call BeginTrans. It appears Access does
not allow me to modify a table that has changes pending a commit or
rollback.

Thanks.

Jon
 

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