Results 1 to 7 of 7
  1. #1
    mcucino is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Jul 2017
    Location
    Providence, RI
    Posts
    74

    Dynamic field and table names


    Hi All, I am sure there is a way to do this but I can't figure it out. I need to refer to a variable instead of (A) a field name in recordsets and (B) a table name tabledefs. Help on either (A) or (B) would be great. Here's the code:

    Code:
    Private Sub UpdateFields()
    Dim rs As DAO.Recordset
    Dim fld As DAO.Field
    Dim db As DAO.Database
    dim TableB as String 'or dao.table or something - I have no idea
    dim FieldName as string 'or dao.field or something
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("Select * from TableA")
    TableB = "LineItemsT"
        For Each fld In db.TableDefs!TableB.Fields  'TableB needs to be a variable
            rs.MoveFirst
            Do While Not rs.EOF
                rs.Edit
                FieldName = fld.name
                rs!FieldName = 12345 'FieldName needs to be variable that points to a field in the rs with the same field name as fld.name from the TableB
                rs.Update
                rs.MoveNext
            Loop
        Next
    
    
    rs.Close
    Set rs = Nothing
    
    
    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    So what are you trying to do???? Copy the values from TableA (rs) into a single field in TableB??

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    To refer to a TableDef object in a collection by its ordinal number or by its Name property setting, use any of the following syntax forms:
    TableDefs(0)
    TableDefs("name")
    TableDefs![name]
    https://docs.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/tabledef-object-dao

    To refer to a Field object in a collection by its ordinal number or by its Name property setting, use any of the following syntax forms:
    Fields(0)
    Fields("name")
    Fields![name]
    https://docs.microsoft.com/en-us/off...eld-object-dao

  4. #4
    mcucino is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Jul 2017
    Location
    Providence, RI
    Posts
    74
    I was hoping no one would ask that... ha ha. I'm really setting rs!fieldname = somecustomfunction(with,lots,of,parameters) that would take way too long in an action query and I didn't want that to further confuse the question

  5. #5
    mcucino is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Jul 2017
    Location
    Providence, RI
    Posts
    74
    kd2017 - thank you! the tabledefs worked great, but it's not working for the field name in the context of updating a recordset. I tried this:

    Code:
                fn = "[" & fld.Name & "]"
                rs!Fields(fn) = 1
    to no avail

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Tried it without the square brackets? Also, use a dot instead of an exclamation point: rs.fields(fn)

  7. #7
    mcucino is offline Advanced Beginner
    Windows 8 Access 2016
    Join Date
    Jul 2017
    Location
    Providence, RI
    Posts
    74
    YASSSS thanks kd2017!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 03-21-2018, 04:58 AM
  2. dynamic field names (sort of)
    By mountainclimber in forum Access
    Replies: 12
    Last Post: 08-13-2015, 08:05 AM
  3. Need Input on Form Design - Dynamic Field Names?
    By Madmartigan in forum Forms
    Replies: 8
    Last Post: 03-07-2014, 01:07 PM
  4. Replies: 5
    Last Post: 12-22-2012, 01:36 PM
  5. Dynamic field names per record
    By snofrandy in forum Queries
    Replies: 1
    Last Post: 05-30-2012, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums