I have a table that has the names of tables I want to create, I can get the tables made but when I try to create a relationship from that table to another I am having a problem. I'm sure it's just that I don't understand how the create relationship part of the code works.
Can someone please check my code and assist. Thanks in advance.
And this is where I get an error:
rel.Fields(tblName).ForeignName = "Stand"
error 3265, item not found in this collection.
Can someone please check my code and assist. Thanks in advance.
Code:
Private Sub CreateTable_Click()
Dim dbs As Database, tdf As TableDef, fld As Field, fld1 As Field, fld2 As Field, fld3 As Field, fld4 As Field, rel As Relation
Dim rst As Recordset, TblCnt As Integer, idx As Index, fldIndex As Field, frm As Form, ctl As Control
Dim tblName As String
Dim CU
CU = fOSUserName()
Set frm = Forms!CreateTables
Set ctl = frm!ProgressBar
ctl = 0
' Return reference to current database.
Set dbs = CurrentDb
' Return the number of records
Set rst = dbs.OpenRecordset("rProcesses")
TblCnt = rst.RecordCount
' Open the table and Go to the first record
DoCmd.OpenForm "Processes", acNormal, acReadOnly, , , acHidden
DoCmd.GoToRecord acDataForm, "Processes", acFirst
If IsNull(Forms!Processes.ProcessName) Then
MsgBox "You must enter the processes first!", vbOKOnly, "Processes required"
Exit Sub
End If
' Begin a loop to check if the table already exists
Do Until rst.EOF
tblName = Forms!Processes.ProcessName
frm.Caption = "Creating " & tblName
ctl.Max = rst.RecordCount
If fnObjExists("Table", tblName) Then
DoCmd.GoToRecord acDataForm, "Processes", acNext
Else
' Return TableDef object variable that points to new table.
Set tdf = dbs.CreateTableDef(tblName)
' Define new field in table.
Set fld1 = tdf.CreateField("Stand", dbText, 10)
Set fld2 = tdf.CreateField("TeamID", dbInteger)
Set fld3 = tdf.CreateField("WageAmount", dbCurrency)
Set fld4 = tdf.CreateField("WageDate", dbDate)
' Append Field objects to Fields collection of TableDef object.
tdf.Fields.Append fld1
tdf.Fields.Append fld2
tdf.Fields.Append fld3
tdf.Fields.Append fld4
tdf.Fields.Refresh
' Create the index
Set idx = tdf.CreateIndex(tblName)
Set fldIndex = idx.CreateField("Stand", dbText, 10)
' Append index fields.
idx.Fields.Append fldIndex
' Set Primary property.
idx.Primary = True
' Append index.
tdf.Indexes.Append idx
' Append TableDef object to TableDefs collection of database.
dbs.TableDefs.Append tdf
dbs.TableDefs.Refresh
Set rel = dbs.CreateRelation(, "Stands", tblName)
rel.Fields.Append rel.CreateField("Stand")
rel.Fields(tblName).ForeignName = "Stand"
dbs.Relations.Append rel
DoCmd.GoToRecord acDataForm, "Processes", acNext
End If
rst.MoveNext
ctl = ctl + 1
Loop
Set dbs = Nothing
DoCmd.Close acForm, "Processes"
ctl = 0
frm.Caption = "Table Creation Complete!"
MsgBox "All Tables successfully created!", vbOKOnly, "Table Creation Successful"
End Sub
rel.Fields(tblName).ForeignName = "Stand"
error 3265, item not found in this collection.