Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get field name and add as a value 1

Status
Not open for further replies.

air1access

Technical User
Jan 27, 2008
123
US
I have a piece of code I'm messing with to normalize a recordset.
I'm trying to get the field name of rs1 and add it as a value for rs2.
I have a tried a few things, but can quite get it....

Any suggestions?
Thanks in advance..!!

Code:
Dim db As Database
Dim rs1 As Recordset
Dim rs2 As Recordset
Dim FieldCount As Integer, i As Integer
Dim fld As Field

FieldCount = 7
Set db = CurrentDb
Set rs1 = db.OpenRecordset("Table2") 'table with denormalized format
Set rs2 = db.OpenRecordset("table1") 'table with normalized format

rs1.MoveFirst

Do While Not rs1.EOF
    For i = 1 To FieldCount - 1
        If Not IsNull(rs1("Value" & i)) Then
            rs2.AddNew
            rs2!Assigned_Resource = rs1!Assigned_Resource
            [highlight #F57900]rs2!Fld_Name = rs1.Fields.Name[/highlight]
            rs2!ValueNo = rs1("Value" & i)
            rs2.Update
        End If
    Next i
    rs1.MoveNext
Loop
 
The "Fields" collection contains multiple fields (7) and you aren't specifying which field.
Try:
Code:
 Debug.Print rs1.Fields(i).Name
 rs2!Fld_Name = rs1.Fields(i).Name
 rs2!ValueNo = rs1.Fields(i).Value


Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Since rs1 has a [tt]Fields[/tt] collection, you can also do:

Code:
...
For i = 1 To rs1.Fields.Count 
...


---- Andy

There is a great need for a sarcasm font.
 
Thank you Dhookum.
That did exactly what I needed.

air1access
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top