×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Get field name and add as a value

Get field name and add as a value

Get field name and add as a value

(OP)
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
            rs2!Fld_Name = rs1.Fields.Name
            rs2!ValueNo = rs1("Value" & i)
            rs2.Update
        End If
    Next i
    rs1.MoveNext
Loop 

RE: Get field name and add as a value

The "Fields" collection contains multiple fields (7) and you aren't specifying which field.
Try:

CODE --> vba

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

RE: Get field name and add as a value

Since rs1 has a Fields collection, you can also do:

CODE

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


---- Andy

There is a great need for a sarcasm font.

RE: Get field name and add as a value

Keep in mind the fields.count begins with 0 not 1.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: Get field name and add as a value

(OP)
Thank you Dhookum.
That did exactly what I needed.

air1access

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close