Results 1 to 10 of 10
  1. #1
    smbrush is offline Experienced Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2018
    Posts
    30

    Add fields to a DAO recordset with VBA

    I have a DAO recordset with one field. I want to add other fields with VBA code. There is a method called recordset.fields.append but this requires a field object of some sort. Can anyone provide me with the code to add new fields to a DAO recordset with VBA code?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  3. #3
    smbrush is offline Experienced Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2018
    Posts
    30
    Your link doesn't help at all. There is nothing in all those code examples that answers my question. Is there a way to add fields to a DAO recordset with VBA or not? This is not a tabledef or querydef.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    If recordset is based on a table==
    You could use code as below:
    Code:
    Sub addFieldtoTable()
        'This adds a COLUMN named anotherfieldBB of datatype Long
        ' to an existing table called Patient9
     '** NOTE: With Access SQL you can only add 1 field at a time/
     '               To add multiple fields, run the query multiple times
    
        Dim x As String: x = "ALTER TABLE Patient9 " _
        & "ADD column   anotherfieldBB  long);"
        Debug.Print x
        CurrentProject.Connection.Execute x, dbFailOnError
    End Sub
    What is the source of your recordset?

    See if these help
    https://social.msdn.microsoft.com/Forums/office/en-US/0b4b58d4-dfe5-4a91-b99d-abe885c180b8/adding-fields-to-a-recordset?forum=accessdev

    https://docs.microsoft.com/en-us/off...-dao-recordset
    Last edited by orange; 08-17-2018 at 12:56 PM. Reason: more info

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Exactly what do you mean by 'add new fields to a DAO recordset with VBA code'? Yes, a field can be constructed with a calculation same as in a query object.

    If you want to modify a table object, then use SQL ALTER TABLE or TableDefs code.

    What exactly are you trying to accomplish?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    smbrush is offline Experienced Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2018
    Posts
    30
    This also doesn't help me. Please read my original post. I need to create a DAO recordset programmatically using values from a field in a table as field names. These values can change over time so I need to iterate over all the records in that table using another recordset.

    I start by creating my recordset from a SQL query such as: "select fldNames from tbl1 order by fldNames" and use "set rs.recordset = db.openrecordset(sql query)". I would then like to iterate through all records from an existing table and set them up as field names in my recordset. Ideally, I would like to use the following code or some version of it:

    set rsFldName = db.OpenRecordset("select fldNames from tbl1 order by fldNames")
    set rsMyRecordSet = db.OpenRecordset("select indexnum from tbl2")

    rsFldName.MoveFirst
    Do while not rsFldName.EOF
    rsMyRecordSet.Fields.Append rsFldName!fldNames, char(50)
    fsFldName.MoveNext
    loop

    This code won't work because .Fields.Append needs an object variable and not a string of the desired field name. I can't figure out how to set this field object variable.

    Do you or anyone else know how best to programmatically add fields to a DAO recordset object?

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I would then like to iterate through all records from an existing table and set them up as field names in my recordset.
    Suppose your existing table has this design:

    Code:
    table_name field_name data_type length
    patient9 SSN Text 9
    patient9 PID Text 8
    patient9 Address Text 60
    patient9 BirthDate Date 8
    patient9 Gender Text 1
    patient9 xName Text 20
    patient9 BloodType Text 2
    patient9 anotherfield Text 25
    patient9 anotherfieldA Text 6
    patient9 anotherfieldBB Long 4
    You can get all the field names in a recordset with some vba

    Code:
    Sub test_smbrush()
    Dim rs As DAO.Recordset
    Dim fld As DAO.Field
    Set rs = CurrentDb.OpenRecordset("Patient9")
    For Each fld In rs.Fields
     Debug.Print fld.name
    Next
    End Sub
    Output:

    SSN
    PID
    Address
    BirthDate
    Gender
    xName
    BloodType
    anotherfield
    anotherfieldA
    anotherfieldBB


    If this isn't what you need/want, then please be specific. Your understanding of DAO recordset in your context is not clear.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Well, your original post was very uninformative. Post 6 finally gave substantial enough information to begin understanding situation.

    You want the value in each record to be used as field name. Just build a string.

    Code:
    Set rsFldName = db.OpenRecordset("select fldNames from tbl1 order by fldNames")
    While not rsFldName.EOF
        strSQL = strSQL & "[" & rsFldName![fldNames] & "],"
        rsFldName.MoveNext
    Wend
    Set rsMyRecordSet = db.OpenRecordset("SELECT indexnum," & Left(strSQL, Len(strSQL)-1) & " FROM tbl2;")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Still not clear to me.
    In the table containing the names that you want to use for field names in your output file:
    If you have a field named STATUS and the contents of that field is "FINAL", do you want the output field name to be STATUS or FINAL?
    Then, in the next record the field STATUS contains the value "OPEN". What do you want for the output file field name?

    If you are using the contents, what happens when the field content is a date or a number?

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    After seeing the comments by June and Davegri, I am offering the following.

    I have a table called data_dictionary which I rebuild and populate with a DocumentTables routine.
    The table has this design
    Click image for larger version. 

Name:	Datadictionary.png 
Views:	11 
Size:	14.3 KB 
ID:	35157

    Here is a routine to query this table, and using fields in the table to create a recordset RS_SMBRUSH.
    This recordset is created based on a table_name within data_dictionary and the field_names that are associated with the table_name.

    For this demo I used/selected a table named Inventory
    I have used some debug.prints to show some of the interim results.
    The sqlFinal shows the SQL for the recordset.
    The names of the fields in the recordset are listed at the bottom.


    Code:
    Sub test_smbrush_1()
    10  On Error GoTo test_smbrush_1_Error
        Dim sTableName As String
        Dim SQLFlds As String
        Dim SQLFinal As String
        Dim i As Integer
        Dim rs As DAO.Recordset
        Dim RS_SMBRUSH As DAO.Recordset
        Dim fld As DAO.Field
    20  Set rs = CurrentDb.OpenRecordset("Data_Dictionary")
        'show the table structure of Data_Dictionary
    30  Debug.Print "Field names in Recordset (rs)"
    40  For Each fld In rs.Fields
    50      Debug.Print fld.name
    60  Next
    70  Debug.Print
        'show the Table names in the data_dictionary
    80  Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT table_name from Data_Dictionary")
    90  Do While Not rs.EOF
            'Debug.Print rs!table_name 'Commented because of volume
    
    100     rs.MoveNext
    110 Loop
    
        'Pick a table from the list (I picked 'Inventory' for this demo
        'Show the fields available in the chosen Table
    120 Set RS_SMBRUSH = CurrentDb.OpenRecordset("Select field_name from data_dictionary where table_name ='Inventory'")
    130 Do While Not RS_SMBRUSH.EOF
    
    140     SQLFlds = SQLFlds & RS_SMBRUSH!field_name & ","
    150     RS_SMBRUSH.MoveNext
    160 Loop
        'Show the raw fields list before trimming and forming the Select
    170 Debug.Print SQLFlds
    
    180 SQLFinal = "SELECT " & Mid(SQLFlds, 1, Len(SQLFlds) - 1) & " From Inventory"
    190 Debug.Print
    200 Debug.Print SQLFinal
    210 Set RS_SMBRUSH = CurrentDb.OpenRecordset(SQLFinal)
    220 Debug.Print
    230 For Each fld In RS_SMBRUSH.Fields
    240     Debug.Print vbTab & fld.name
    250 Next
    
    
    260 On Error GoTo 0
    270 Exit Sub
    
    test_smbrush_1_Error:
    
    280 MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure test_smbrush_1, line " & Erl & "."
    
    End Sub
    This is the debug.print from the routine.

    Field names in Recordset (rs)
    EntryID
    table_name
    table_description
    field_name
    field_description
    ordinal_position
    data_type
    length
    default

    Cabinet,issdate,MasterCopies,Media,Revision,RowId, units,WorkCopies,

    SELECT Cabinet,issdate,MasterCopies,Media,Revision,RowId, units,WorkCopies From Inventory

    Cabinet
    issdate
    MasterCopies
    Media
    Revision
    RowId
    units
    WorkCopies

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

Similar Threads

  1. Print a report from current recordset with no fields
    By themushroomking in forum Access
    Replies: 8
    Last Post: 07-07-2017, 01:54 PM
  2. Add Fields To Form Based on Recordset
    By Relwal in forum Forms
    Replies: 1
    Last Post: 01-05-2017, 04:25 AM
  3. loop recordset and update fields
    By gammaman in forum Programming
    Replies: 9
    Last Post: 06-30-2016, 04:20 AM
  4. Replies: 8
    Last Post: 10-24-2013, 02:37 PM
  5. Wrong reference with .fields in DAO recordset
    By ddd in forum Programming
    Replies: 1
    Last Post: 12-08-2009, 05:34 PM

Tags for this Thread

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