Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2013, 09:02 PM
Registered User
 
Join Date: Feb 2013
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default createrelation error 3609

I'm trying to learn how to create a relation via vba and am receiving error 3609 "No unique index found for the reference field of the primary table."

Here is the Function I am testing (to create a new table, new fields, new index, new relation):
Public Function VBATestUpdateProgram() As Boolean
' This function will test the new table-building, including index (ID) assignment and relations.
On Error GoTo Error_Handler
Dim dbsTestVBA As DAO.Database
Dim tdfTestVBA As DAO.TableDef
Dim idxID As DAO.Index
Dim fldID As DAO.Field
Dim fldStateLookUp As DAO.Field
Dim fld As DAO.Field
Dim relState As DAO.Relation
Set dbsTestVBA = CurrentDb
Set tdfTestVBA = dbsTestVBA.CreateTableDef("TestVBATableBuilding")
Set fldID = tdfTestVBA.CreateField("ID", dbLong)
Set fldStateLookUp = tdfTestVBA.CreateField("StateLookUp", dbLong)
fldID.Attributes = dbAutoIncrField
tdfTestVBA.Fields.Append fldID
tdfTestVBA.Fields.Append fldStateLookUp
Set idxID = tdfTestVBA.CreateIndex("TestVBATableBuilding_ID")
idxID.Primary = True
idxID.Unique = True
Set fldID = idxID.CreateField("ID")
idxID.Fields.Append fldID
tdfTestVBA.Indexes.Append idxID
dbsTestVBA.TableDefs.Append tdfTestVBA
dbsTestVBA.TableDefs.Refresh
Application.RefreshDatabaseWindow
Set relState = dbsTestVBA.CreateRelation("TestVBATableBuildingSta teNames", "TestVBATableBuilding", "StateNames")
relState.Attributes = dbRelationUpdateCascade Or dbRelationDeleteCascade
Set fld = relState.CreateField("StateLookUp")
fld.ForeignName = "StateName"
relState.Fields.Append fld
dbsTestVBA.Relations.Append relState
dbsTestVBA.Relations.Refresh
Application.RefreshDatabaseWindow
Set fldID = Nothing
Set fldStateLookUp = Nothing
Set fld = Nothing
Set idxID = Nothing
Set tdfTestVBA = Nothing
Set relState = Nothing
VBATestUpdateProgram = True
Set dbsTestVBA = Nothing
Exit_Procedure:
Exit Function
Error_Handler:
MsgBox "An error has occurred in this application. Please contact your technical" _
& " support and tell them the following information:" & vbCrLf & vbCrLf _
& "Error Number " & Err.Number & ", " & Err.Description, Buttons:=vbCritical
Resume Exit_Procedure
End Function

"StateNames" is an existing table with the following fields:
ID (primary key and auto-numbered)
StateName (text, 25)
StateAbbrev (text, 2)

My intent, here, is to have the newly-created "StateLookUp" field be a "LookUp", or at least be related to, the "StateName" field in the "StateNames" table, where each "StateName" is unique (i.e., "StateLookUp" is 'many'; "StateName" is 'one'). After receiveing the error, the "TestVBATableBuilding" table is built, as well as both the "ID" primary key and "StateLookUp" fields.

Help, Mr. Wizard!
 
Old February 4th, 2013, 03:10 PM
Registered User
 
Join Date: Feb 2013
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I answered my own question after more research and some experimentation. Instead of hard-coding the relationship-related VBA, I found some code from "sakazir" that I modified:

Public Function CreateRelation(primaryTableName As String, primaryFieldName As String, foreignTableName As String, foreignFieldName As String) As Boolean
' This function will create relations. Primary Table = Parent table (e.g., "StateNames")
' Primary Field Name = Primary Key (e.g., "ID" in "StateNames"); Foreign Table = Child Table (e.g., "CityState")
' Foreign Field Name = Foreign Key (e.g., "StateAbr" in "CityState")
On Error GoTo Error_Handler
Dim dbsCreRel As DAO.Database
Dim newRelation As DAO.Relation
Dim relatingField As DAO.Field
Dim relationUniqueName As String
relationUniqueName = primaryTableName + "_" + primaryFieldName + "__" + foreignTableName + "_" + foreignFieldName
Set dbsCreRel = CurrentDb()
Set newRelation = dbsCreRel.CreateRelation(relationUniqueName, primaryTableName, foreignTableName)
Set relatingField = newRelation.CreateField(primaryFieldName)
relatingField.ForeignName = foreignFieldName
newRelation.Fields.Append relatingField
dbsCreRel.Relations.Append newRelation
Set dbsCreRel = Nothing
CreateRelation = True
Exit_Procedure:
Exit Function
Error_Handler:
MsgBox "An error has occurred in this application. Please contact your technical" _
& " support and tell them the following information:" & vbCrLf & vbCrLf _
& "Error Number " & Err.Number & ", " & Err.Description, Buttons:=vbCritical
CreateRelation = False
Resume Exit_Procedure
End Function

Use this function and it should work. I have a table named "StateNames" with fields of "ID" = Primary Key; "StateName" (Text, 25); and "StateAbbrev" (Text, 2). For this example, I have another table named "CityState" with fields of "ID" = Primary Key (for the table); "City" (text, 25); "StateAbr" (long); "Zip" (text, 10). Enjoy!





Similar Threads
Thread Thread Starter Forum Replies Last Post
file system object error. automation error. library not registered shaamir BOOK: Beginning ASP.NET 4 : in C# and VB 1 September 25th, 2012 09:27 AM
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING ngchpg BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 July 16th, 2012 03:28 AM
SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified soufya BOOK: Beginning ASP.NET 4 : in C# and VB 7 September 14th, 2011 08:08 AM
HTTP error 500: Internal Server Error in Load Testing sherin Visual Studio 2008 0 May 19th, 2010 09:02 AM
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.