Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    Variable assignments to DAO field names

    I have a case where a sub needs to dynamically change the field name during an ADDNEW field assignment. I'm trying to use a Field object to make the name change then use that object in an assignment statement. This is my first time attempting such a case and obviously not understanding well enough.



    I started by trying assignments like, "rsPDF![Color & ColNum] = 1", but such syntax is not valid, so I've been trying to resolve using a field object. Is that the method to use in a situation like this?

    Also tried fldColor = "Color" & ColNum with an assignment like "rsPDF!fldColor = "but that fails as well.

    The code:
    Code:
    Private Sub LoadBAtbl(rsIN As Recordset, ColNum As Integer, Mth As String, Occasion As String)
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    '  PREPARING SPECIAL FORMATTING FOR TWO COLUMN REPORT RECORDSET:
    '  The input recordset rsIN contains all the data that is to be added to the table
    '  currently selected in the rsPDF recordset.  Each single record in rsIN has multiple
    '  fields.  Headers are added with dte values of 40 and 50 respectively, so that
    '  conditional formatting will respond as intended, as will the "Bs" and "As".
    '*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
    Dim intColr As Integer            'Color flag for conditional formatting
    Dim fldColor As Field               'Need a field object to make assignments
    Dim fldDte As Field               'Need a field object to make assignments
    Dim fldTXT As Field               'Need a field object to make assignments
    
    
    fldColor = "rsPDF!Color" & ColNum       <================ Debug stops here
    fldDte = "rsPDF!Dte" & ColNum
    fldTXT = "rsPDF!Txt" & ColNum
    
    
    rsPDF.AddNew                     'blank line before header
    rsPDF!fldTXT = " "
    rsPDF.Update
    
    
    Select Case Occasion
        Case "Birthdays"
            intColr = 1              'Non-Headers in blue
        
            rsPDF.AddNew
            rsPDF!fldColor = 1
            rsPDF!fldDte = 40       'Header underline and blue
            rsPDF!fldTXT = "      " & Mth & " BIRTHDAYS      "
            rsPDF.Update
    
    
        Case "Anniversaries"
    
    <Snip>
    The error: Click image for larger version. 

Name:	000.jpg 
Views:	17 
Size:	69.9 KB 
ID:	49014

  2. #2
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Changing the Dim to DAO.Field didn't change anything either: (Same error)
    Attachment 49015

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Why the quotation marks?
    Code:
    "rsPDF!Color"
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    No idea if you can make this work, but you don't enclose rs.Field references in quotes as a rule. You might be OK if you use rsPDF.Fields("Dte" & ColNum)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    rsPDF!Fields("Dte" & ColNum) = " " doesn't work, nor did rsPDF!Fields(fldTxT) = " ", where fldTxt is string set to "TxT2". Both failed "item not in collection". Even rsPDF!Fields("TxT2") = " " fails, and yes rsPDF!Txt2 = " " assigns correctly.

    As you say, maybe I can't get this to work. The reason I was doing this to begin with is that I need to populate the new recordset with the "longest" of the rsIN recordsets so that all subsequent assignments with ".Edit" > ".Update" looping won't overflow. I can think of a few other approaches that won't require duplication of a lot of code, so I'll think on that for a bit.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    your attachment failed, by the way, so can't look for you.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I don't know what happened there, but it was simply a screenshot of another failure so doesn't matter.

  8. #8
    darkwind is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2017
    Location
    Central U.S.
    Posts
    48
    I don't believe you can change the name of fields in a recordset object. I think you'd need a tabledef to be able to set field names (and dynamically change the number of fields present, which ? it sounds like you may need to do). Something like:

    Code:
       Dim dbs As DAO.Database, tdf As DAO.TableDef, fld As DAO.field
       Set dbs = CurrentDb
       Set tdf = dbs.CreateTableDef("tblNew")
       
       For Each fld In rsIn.fields
          tdf.fields.Append tdf.CreateField(fld.name & ColNum, fld.Type, fld.size)
       Next fld
       
       dbs.TableDefs.Append tdf
       
       Dim rsPDF As DAO.recordset
       Set rsPDF = dbs.OpenRecordset(tdf.name)
       rsPDF.AddNew
       rsPDF("fldTXT") = ""
       rsPDF.Update
       
       Select Case Occasion
          Case "Birthdays"
             intColr = 1
             rsPDF.AddNew
             rsPDF("fldColor") = intColr
             rsPDF("fldDte") = 40
             rsPDF("fldTXT") = "      " & Mth & " BIRTHDAYS      "
             
          Case "Anniversaries"
             
       End Select
    But there's a lot you're doing here that I don't quite understand the reason for, so maybe I'm missing something about your objectives/scenario.

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Are you sure it shouldn't be rsIn? I see no declared variable for rsPDF in what you posted.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    darkwind is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2017
    Location
    Central U.S.
    Posts
    48
    The syntax to reference the field name is rsPDF.Fields(0).name, but it's read-only. Trying to set that will get an invalid operation error.

  11. #11
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    The table already exists and is refreshed as multiple incidents occur. It is the object of the rsPDF Set.

  12. #12
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    rsPDF is global to the module and is Dim rsPDF as DAO.Recordset.

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Quote Originally Posted by GraeagleBill View Post
    The table already exists and is refreshed as multiple incidents occur. It is the object of the rsPDF Set.
    I have no idea to whom you're replying. The error is object variable or with block variable not set. I'm saying you don't show that you declared it anywhere.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Sorry, you had posted:
    Are you sure it shouldn't be rsIn? I see no declared variable for rsPDF in what you posted.
    I was intending to reply to you that rsPDF is global to the module and is both Dim'd DAO.Recordset and is "Set" to an existing table that is being populated by the code in the OP.

  15. #15
    darkwind is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2017
    Location
    Central U.S.
    Posts
    48
    Quote Originally Posted by GraeagleBill View Post
    The table already exists and is refreshed as multiple incidents occur. It is the object of the rsPDF Set.
    Right, but you can't just re-name recordset fields using a recordset object... If you want to do that, you need to directly refer to the table definition.

    If you want to read/set the value of a field, sure:

    Code:
    rs.Edit
    rs("fieldName").Value = "whatever"
    rs.Update
    If you want to find the name of a field, sure:

    Code:
    Debug.Print rs.Fields(index).Name
    But the Field.Name property is read-only if it's part of a collection. Fields in a table are always part of a collection (the tabledef.fields collection). See: https://learn.microsoft.com/en-us/of...e-property-dao

    The proper syntax to change a field's name would be:

    Code:
    rs.Fields(0).Name = "FieldName"


    But that won't work, it will throw the invalid operation error I mentioned before... because it's a read-only property in this context.

    Aside from all of that, if you already have a table with all of this data... then I don't at all understand why you need to dynamically change the column names and/or number of columns. Isn't the whole point of a table that the number of columns and the type of data that goes in each column is constant? If you've got variable amounts of data of a given type, that should go in related tables, not in the main table- no? I might be able to give better advice with a better understanding of what you're doing.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Set Label Names As A Variable
    By JMZLR in forum Programming
    Replies: 2
    Last Post: 01-22-2021, 11:34 AM
  2. Variable Label Form Names
    By DCR in forum Forms
    Replies: 2
    Last Post: 01-05-2021, 08:27 PM
  3. How can I refer to field names using a variable
    By Tony Franks in forum Programming
    Replies: 3
    Last Post: 06-06-2016, 10:05 AM
  4. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM
  5. Variable Table Names in a Module
    By Jessica in forum Modules
    Replies: 1
    Last Post: 04-19-2010, 07:38 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