This code is intended to pull the data from T_LinkedTableInfoCFC (and ultimately other tables of the same design) and compile its contents into T_MasterLinkedTableFields. To process all the fields the code loops through the "Master" table and extracts the name of each field. Everything works as expected until the highlighted line. Note that the line directly above it where the strFldName variable is set directly works and does not produce an error. Also note the information from the Immediate Window included at the bottom in which the contents of the two variables as displayed appear to be equivalent but.. ?strFieldName = strFldNm evaluates as False. Based on search results the "RstMasterT.Fields(strVariable).Value" syntax seems correct but I can't understand why it doesn't work when the string variable is set to the field name as pulled from the recordset but does when set directly to ?equivalent? text.
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim RstMasterT As DAO.Recordset
Dim RstSoloT As DAO.Recordset
Dim strFieldName As String
Dim strFldNm As String
Dim strDocName As String
Dim StrDocName2 As String
Dim n As Long
DoCmd.SetWarnings False
strDocName = "T_LinkedTableInfoCFC" 'test table
StrDocName2 = "T_MasterLinkedTableFields" 'The table used to collect all the info from the above tables
DoCmd.RunSQL "Delete * from " & StrDocName2 'Empty the master table
Set dbs = CurrentDb
Set RstSoloT = dbs.OpenRecordset(strDocName, dbOpenDynaset)
Set RstMasterT = dbs.OpenRecordset(StrDocName2, dbOpenDynaset)
If Not (RstSoloT.EOF And RstSoloT.BOF) Then 'RstSoloT is not empty
RstSoloT.MoveLast
RstSoloT.MoveFirst
With RstMasterT
For n = 0 To .Fields.Count - 1 'Cycle through the field names in rstLinked recordset
strFieldName = .Fields.Name & vbCrLf
strFldNm = "FE_DbPath"
RstMasterT.AddNew
RstMasterT.Fields(strFldNm).Value = RstSoloT.Fields(strFldNm).Value 'Works!
[highlight #FCE94F]RstMasterT.Fields(strFieldName).Value = RstSoloT.Fields(strFieldName).Value 'Item not found in this collection[/highlight]
Next n
End With
RstSoloT.MoveNext
End If
IMMEDIATE WINDOW
?strFieldName
FE_DbPath
?strFldNm
FE_DbPath
?strFieldName = strFldNm
False
?strFldNm = Cstr(strFieldName)
False
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim RstMasterT As DAO.Recordset
Dim RstSoloT As DAO.Recordset
Dim strFieldName As String
Dim strFldNm As String
Dim strDocName As String
Dim StrDocName2 As String
Dim n As Long
DoCmd.SetWarnings False
strDocName = "T_LinkedTableInfoCFC" 'test table
StrDocName2 = "T_MasterLinkedTableFields" 'The table used to collect all the info from the above tables
DoCmd.RunSQL "Delete * from " & StrDocName2 'Empty the master table
Set dbs = CurrentDb
Set RstSoloT = dbs.OpenRecordset(strDocName, dbOpenDynaset)
Set RstMasterT = dbs.OpenRecordset(StrDocName2, dbOpenDynaset)
If Not (RstSoloT.EOF And RstSoloT.BOF) Then 'RstSoloT is not empty
RstSoloT.MoveLast
RstSoloT.MoveFirst
With RstMasterT
For n = 0 To .Fields.Count - 1 'Cycle through the field names in rstLinked recordset
strFieldName = .Fields.Name & vbCrLf
strFldNm = "FE_DbPath"
RstMasterT.AddNew
RstMasterT.Fields(strFldNm).Value = RstSoloT.Fields(strFldNm).Value 'Works!
[highlight #FCE94F]RstMasterT.Fields(strFieldName).Value = RstSoloT.Fields(strFieldName).Value 'Item not found in this collection[/highlight]
Next n
End With
RstSoloT.MoveNext
End If
IMMEDIATE WINDOW
?strFieldName
FE_DbPath
?strFldNm
FE_DbPath
?strFieldName = strFldNm
False
?strFldNm = Cstr(strFieldName)
False