I have a Db with (for the moment) tables in my FE database together with linked Excel files.
In the relationship window, I can establish a relationship between an "Access" table and the linked "Excel" table, but obviously can't establish referential integrity. I can live with that.
Using the following code - Public Function CreateRelationship(vPKTableName As String, vPKFieldName As String, vFKTableName As String, vFKFieldName As String, Optional RelName As String) As Boolean
-
'?CreateRelationship("TblPSysJoinExcelProjectsProject", "ProjectID", "TblExcelProject", "ONB Project", "APS"
-
Dim MyDb As DAO.Database
-
Dim newRelation As DAO.Relation
-
Dim relatingField As DAO.Field
-
-
On Error GoTo CreateRelationship_Err
-
-
If IsMissing(RelName) Or RelName = "" Then
-
RelName = vPKTableName + "_" + vPKFieldName + "__" + vFKTableName + "_" + vFKFieldName
-
End If
-
-
Set MyDb = CurrentDb()
-
-
'Arguments for CreateRelation(): any unique name,
-
'primary table, related table, attributes.
-
Set newRelation = MyDb.CreateRelation(RelName, vPKTableName, vFKTableName)
-
'The field from the primary table.
-
Set relatingField = newRelation.CreateField(vPKFieldName)
-
'Matching field from the related table.
-
relatingField.ForeignName = vFKFieldName
-
'Add the field to the relation's Fields collection.
-
newRelation.Fields.Append relatingField
-
'Add the relation to the database.
-
MyDb.Relations.Append newRelation
-
-
Set MyDb = Nothing
-
-
CreateRelationship = True
-
-
Exit Function
-
-
CreateRelationship_Err:
-
Debug.Print "Error: " & Err & " " & Err.Description + " (" + RelName + ")"
-
-
End Function
I get Error: 3057 Operation not supported on linked tables. (APS)
on the line starting "Set newRelation = ...."
If I can set the relationship manually, why not in code? How do I get round this?
Thanks
Phil
- It took a little patience, but I do believe that I have arrived at a solution, namely: Creating a Relationship between an Access Table and a Linked Excel Spreadsheet (RI not enforced):
- Revised Function Definition:
- Public Function CreateRelationship(vPKTableName As String, vPKFieldName As String, vFKTableName As String, vFKFieldName As String, Optional RelName As String) As Boolean
-
Dim MyDb As DAO.Database
-
Dim newRelation As DAO.Relation
-
Dim relatingField As DAO.Field
-
-
On Error GoTo CreateRelationship_Err
-
-
If IsMissing(RelName) Or RelName = "" Then
-
RelName = vPKTableName + "_" + vPKFieldName + "__" + vFKTableName + "_" + vFKFieldName
-
End If
-
-
Set MyDb = CurrentDb()
-
-
'Arguments for CreateRelation(): any unique name,
-
'primary table, related table, attributes.
-
Set newRelation = MyDb.CreateRelation(RelName, vPKTableName, vFKTableName, dbRelationDontEnforce)
-
-
'The field from the primary table.
-
Set relatingField = newRelation.CreateField(vPKFieldName)
-
-
'Matching field from the related table.
-
relatingField.ForeignName = vFKFieldName
-
-
'Add the field to the relation's Fields collection.
-
newRelation.Fields.Append relatingField
-
-
'Add the relation to the database.
-
MyDb.Relations.Append newRelation
-
-
Set MyDb = Nothing
-
-
CreateRelationship = True
-
Exit Function
-
-
CreateRelationship_Err:
-
If Err.Number = 3012 Then 'Relation already exists, DELETE it and start over
-
MyDb.Relations.Delete newRelation.Name
-
Resume
-
Else
-
Debug.Print "Error: " & Err.Number & " " & Err.Description & " (" & RelName & ")"
-
End If
-
End Function
- Sample Function Call:
- If CreateRelationship("Employees", "ID", "Employees_Excel", "ID", "MyRelation") = True Then
-
MsgBox "Success"
-
Else
-
MsgBox "Failure"
-
End If
-
- Kindly let me know if this works out for you.
13 2470
I am not exactly sure why this Error is occurring, but there is a work-a-round. How viable this solution is will depend on your specific circumstances. - Create a Table that is exactly the same structurally as your Linked Excel Table. This will be your Append Table (let's call it tblAppend).
- DELETE all Records in tblAppend before attempting to create any Relation, (ref Line# 15), as in:
- CurrentDB.Execute "DELETE * FROM tblAppend", dbFailOnError
- Create an Append Query that will Append all Data in the Linked Excel Table to the empty tblAppend (SET/RESET WARNINGS).
- Refer to Lines# 17-21.
- You can now execute Code that will create a Relation between FieldX in the Access Table and FieldY in tblAppend.
- Also, you can now enforce Referential Integrity, Cascade Deletes, etc. between these two Tables.
- Key Logic in this approach is to check if the Relation already exists, and if it does, DELETE then recreate it (ref Line# 44).
- Revised Function Definition:
-
Public Function CreateRelationshipDemo(vPKTableName As String, vPKFieldName As String, vFKTableName As String, vFKFieldName As String, Optional RelName As String) As Boolean
-
'?CreateRelationship("TblPSysJoinExcelProjectsProject", "ProjectID", "TblExcelProject", "ONB Project", "APS"
-
Dim MyDb As DAO.Database
-
Dim newRelation As DAO.Relation
-
Dim relatingField As DAO.Field
-
-
On Error GoTo CreateRelationship_Err
-
-
If IsMissing(RelName) Or RelName = "" Then
-
RelName = vPKTableName + "_" + vPKFieldName + "__" + vFKTableName + "_" + vFKFieldName
-
End If
-
-
Set MyDb = CurrentDb()
-
-
CurrentDb.Execute "DELETE * FROM Employees2", dbFailOnError
-
-
With DoCmd
-
.SetWarnings False
-
.OpenQuery "qryAppendDataFromLinkedExcelTable", acViewNormal
-
.SetWarnings True
-
End With
-
-
Set newRelation = MyDb.CreateRelation(RelName, vPKTableName, vFKTableName)
-
newRelation.Attributes = dbRelationDeleteCascade 'Cascade DELETES
-
-
'The field from the primary table.
-
Set relatingField = newRelation.CreateField(vPKFieldName)
-
-
'Matching field from the related table.
-
relatingField.ForeignName = vFKFieldName
-
-
'Add the field to the relation's Fields collection.
-
newRelation.Fields.Append relatingField
-
-
'Add the relation to the database.
-
MyDb.Relations.Append newRelation
-
-
Set MyDb = Nothing
-
-
CreateRelationshipDemo = True
-
-
Exit Function
-
-
CreateRelationship_Err:
-
If Err.Number = 3012 Then 'Relation already exists, DELETE it and start over
-
MyDb.Relations.Delete newRelation.Name
-
Resume
-
Else
-
Debug.Print "Error: " & Err & " " & Err.Description + " (" + RelName + ")"
-
End If
-
End Function
- Sample Function Call:
- If CreateRelationshipDemo("Employees", "ID", "Employees2", "ID", "MyRelation") = True Then
-
MsgBox "Success"
-
Else
-
MsgBox "Failure"
-
End If
-
P.S. - If you are interested, I have a functional Demo that you can reference. Just let me know.
NeoPa 32,556
Expert Mod 16PB
Hi ADezii.
Just to be clear, can I confirm that you're proposing to create a Jet/ACE copy of the Excel table and then creating a Relationship using that instead?
NeoPa 32,556
Expert Mod 16PB
Hi Phil.
Not really sure why that wouldn't work if I'm honest. I'd expect the code and the interface to work similarly and consistently. It seems you've found they don't :-(
Just to be clear, can I confirm that you're proposing to create a Jet/ACE copy of the Excel table and then creating a Relationship using that instead?
First and foremost, always a pleasure to see you alive and well and as sharp as ever! Regarding your statement, you are correct. Depending on Phil's unique situation, it may/may not be a suitable solution. Granted, it is a little clumsy.
NeoPa 32,556
Expert Mod 16PB
I can't deny I also get a little thrill when I see you post my friend.
Thanks guys for your input.
I am reluctant to go down ADezii's route because, after setting up the relationship manually, the access portion should stay in "sync" with the Excel files.
Certainly MsysRelationships shows the relationship after it has been set up manually, and using VBA the relationship can be deleted, so that indicates there is not a problem with write permissions to MSysRelationships.
Is this an Access bug?
Phil
It's not a bug... It's a "feature"!
:-)
after setting up the relationship manually, the access portion should stay in "sync" with the Excel files.
You are correct in stating that my solution would be useless in this case because you would lose the synchronization with the Excel File.
- It took a little patience, but I do believe that I have arrived at a solution, namely: Creating a Relationship between an Access Table and a Linked Excel Spreadsheet (RI not enforced):
- Revised Function Definition:
- Public Function CreateRelationship(vPKTableName As String, vPKFieldName As String, vFKTableName As String, vFKFieldName As String, Optional RelName As String) As Boolean
-
Dim MyDb As DAO.Database
-
Dim newRelation As DAO.Relation
-
Dim relatingField As DAO.Field
-
-
On Error GoTo CreateRelationship_Err
-
-
If IsMissing(RelName) Or RelName = "" Then
-
RelName = vPKTableName + "_" + vPKFieldName + "__" + vFKTableName + "_" + vFKFieldName
-
End If
-
-
Set MyDb = CurrentDb()
-
-
'Arguments for CreateRelation(): any unique name,
-
'primary table, related table, attributes.
-
Set newRelation = MyDb.CreateRelation(RelName, vPKTableName, vFKTableName, dbRelationDontEnforce)
-
-
'The field from the primary table.
-
Set relatingField = newRelation.CreateField(vPKFieldName)
-
-
'Matching field from the related table.
-
relatingField.ForeignName = vFKFieldName
-
-
'Add the field to the relation's Fields collection.
-
newRelation.Fields.Append relatingField
-
-
'Add the relation to the database.
-
MyDb.Relations.Append newRelation
-
-
Set MyDb = Nothing
-
-
CreateRelationship = True
-
Exit Function
-
-
CreateRelationship_Err:
-
If Err.Number = 3012 Then 'Relation already exists, DELETE it and start over
-
MyDb.Relations.Delete newRelation.Name
-
Resume
-
Else
-
Debug.Print "Error: " & Err.Number & " " & Err.Description & " (" & RelName & ")"
-
End If
-
End Function
- Sample Function Call:
- If CreateRelationship("Employees", "ID", "Employees_Excel", "ID", "MyRelation") = True Then
-
MsgBox "Success"
-
Else
-
MsgBox "Failure"
-
End If
-
- Kindly let me know if this works out for you.
Great, ADezii, works a treat.
It looks as if missing out the dbRelationDontEnforce was the main culprit.
Thanks again
Phil
After some thought I guess it makes sense: - You can create the Relationship manually, but the option to enforce RI is disabled since you cannot set Referential Integrity on an External, Linked Object.
- It appears that the CreateRelation() Method enforces RI by Default. The generated Error is not in creating the Relationship, but enforcing RI on it.
- By setting the dbRelationDontEnforce, the Method is explicitly told not to enforce RI thus avoiding the Error.
- In any event, good luck with your Project!
ADezii,
That was a great explanation! That makes a lot of sense and gives me another tool in my kit. Thanks to you, too, Phil, for raising the issue--always pushing the envelope!
Thanks twinnyfo for the compliment. I actually thought that my explanation was as clear as mud.(LOL).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: pj |
last post by:
I tried to create a relationship in EM's diagram pane in
SQL Server 2000 (I'd list the version of EM, but About gives me
the MMC version, which is probably not relevant.)
(The database itself is...
|
by: js |
last post by:
I need to create a relationship between a local table and tables on a
linked server. I used the design table wizard and selected the
relationship property wizard. In the reslationship property...
|
by: Mindy |
last post by:
Hey,
I want to create links between my two tables. The primary keys of these
two tables are character variables, with length =11. I followed exactly
the instruction of Dummuy Book for ACCESS...
|
by: Raji16 |
last post by:
Hi,
I am a new member. i am designing a simple judicial database system. however after creating tables i am a bit confused on setting the relationships between tables :confused: here is the link...
|
by: max |
last post by:
Hello,
I am a newbye, and I'm trying to write a simple application.
I have five tables with three columns; all tables are identical; I need to
change some data in the first table and let VB...
|
by: raylopez99 |
last post by:
Keep in mind this is my first compiled SQL program Stored Procedure
(SP), copied from a book by Frasier Visual C++.NET in Visual Studio
2005 (Chap12). So far, so theory, except for one bug...
|
by: pradyuthal |
last post by:
is it possible to make relationship between one or two fields created not based on a table or query and the same types of fields in table/tables so that I may be able to feed data through the form? ...
|
by: prakashbpl1 |
last post by:
Hi All,
Hi query is regarding ASP.NET and C# developement ,how to create relationship between two IList collection classes.By this collection relationship I want to create a nested GridView and...
|
by: ADils |
last post by:
I have a database which is defined in a MIL standard. This consists of 104 tables and has a key that consists every time of three fields at minimum. I do not want to have all relations in the...
|
by: Mustafa Sherazi |
last post by:
Hello,
i have three tables in my database. i m trying to create one to many relationship among those three tables. relationship created between two tables, but when i try to create relationship...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |