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

    I don't at all understand why you need to dynamically change the column names
    I'm not trying to "change" column names, I'm trying to use the same code module to populate an existing table with fields already defined. E.g., like fields Txt1, Txt2, TxT3.... etc The input to the module are recordsets rsI that contain the data required to populate those fields.

  2. #17
    darkwind is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2017
    Location
    Central U.S.
    Posts
    48
    Gotcha. Sorry, the language used in OP had me quite confused on this.

    So, to edit an existing record, simply:

    Code:
    rsPDF.Edit
    rsPDF("txt1").Value = rsIn("txt1").value
    rsPDF.Update
    To add a new record:

    Code:
    rsPDF.Addnew
    rsPDF("txt1").Value = rsIn("txt1").Value
    rsPDF.Update
    It sounds like you might be trying to dynamically match column names? I.e., update rsPDF so rsPDF("txt1") = rsIn("txt1"). If so, something like:

    Code:
        Dim fld As DAO.Field, fld2 As DAO.field
        rsPDF.AddNew
        For Each fld In rsIn.Fields
            For Each fld2 In rsPDF.Fields
                If fld2.Name = fld.Name Then
                    fld2.Value = fld.Value
                    Exit For
                End If
            Next fld2
        Next fld
        rsPDF.Update

  3. #18
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If it's declared you need to show or tell that in your first post.
    dynamically change the field name during an ADDNEW field assignment
    Before creating the rs, maybe. After - no - unless the field happens to be in the recordset to start with. In that case you're not arbitrarily trying to alter rs field names, you'd be dynamically choosing them, which is what I thought you were trying to do. You'd have to alter the query def sql or the table def field name and that just sounds crazy.

    I think this is a case where it would help if we knew the what/why rather than trying to fix something that will never work. Or you go back one or two steps and take a new tack.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #19
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    unless the field happens to be in the recordset to start with
    Yes, as I posted earlier, the recordset rsPDF is based on an existing table with fields already defined.

    I fear I've wasted everyone's time on this. In was trying to get a block of code to operate on the single record in the target recordset by modifying an assignment field name by appending an identifying value but none of the various expressions would work successfully.

    The target recordset is based on a table with fields suffixed with numerical values, e.g., Txt1, Txt2, Txt3,..... Txtn. As data is gathered in the input recordset rsIN, I wanted the same block of code to be able to populate the fields by appending the suffix values much like we would do with form controls, e.g., Me.Controls("tbMyTB" & number).

  5. #20
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Well I guess if we're going to figure out if that is possible, you'll have to post a sample db with instructions on what to run. I for one am watching World Series so I'm not going to be of any use tonight!
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #21
    darkwind is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Feb 2017
    Location
    Central U.S.
    Posts
    48
    I think I might finally be understanding what you want to do. You want to concatenate together a static field name with a dynamic column number to form the full name of the field in the recordset, correct? If so, this is the syntax you're after on the line of code that fails in your OP:

    Code:
    Set fldColor = rsPDF("txt" & ColNum)
    You could also build the string into a variable:

    Code:
    Dim sFieldName as String
    sFieldName = "txt" & ColNum
    Set fldColor = rsPDF(sFieldName)
    You can refer to fields in a recordset using !, but that won't let you concatenate strings together to form the field name the way you seem to want to.

    I maintain that this is, in all probability, not the best way to accomplish what you're likely trying to accomplish. You might want to review if there's a better way to name your fields or a better way to pair your recordsets up. Shouldn't the two recordsets be related in some way - either having the same field names, having a primary key/foreign key relationship, or having static field relationships (e.g., rsIn("color") always pairs to rsPDF("hdrColor"), or similar)? In any of those cases there are very likely cleaner, more readable, and potentially faster ways of transferring the data between the two recordsets.

  7. #22
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    If it's about syntax or concatenation I'll be surprised. The object variable may have been declared but it is not Set and if it was, then it's another piece of the puzzle not shown. That's what the message means to me.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #23
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Set fldColor = rsPDF("txt" & ColNum)
    Notice in the OP code and screenshot that trying to assign a concatenated string to a field object is missing the "Set".

    Normally, table recordset situations such as the current one are best accomplished using the known relationships together with a union query. Were it not for the somewhat convoluted requirement of a multi-column report for which the current rsPDF becomes the reports RecordSource plus the added conditional formatting rules being applied, we might be having a totally different discussion. I'm just wanting to make the best of a somewhat difficult requirement.

    I might come back and try to repair the OP code, but time dictates an alternative approach where I've duplicated some of the code.

    Thanks to you both for your thoughts and help,
    Bill

  9. #24
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I'm referring to rsPDF not being Set, not fldColor. Anyway, since you don't want to go that route anymore, you might as well mark this one as solved so that others don't spend time reading through it all only to find out that you're done with it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #25
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I have gone back to try out DarkWind's suggestion in post #21. Works like a champ.
    Code:
    (Snip)
    Dim fldColor As DAO.Field
    Dim fldDte As DAO.Field
    Dim fldTxT As DAO.Field
    
    
    Set fldColor = rsPDF("Color" & ColNum)
    Set fldDte = rsPDF("Dte" & ColNum)
    Set fldTxT = rsPDF("TxT" & ColNum)
    
    
    Select Case Occasion
        Case "Birthdays"
            rsPDF.MoveFirst
           
            intColr = 1              'Non-Headers in blue
        
            rsPDF.Edit
            fldColor = 1
            fldDte = 40       'Header underline and blue
            fldTxT = "      " & UCase(Mth) & " BIRTHDAYS      "
            rsPDF.Update
            rsPDF.MoveNext
    
    
    (Snip)

Page 2 of 2 FirstFirst 12
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