How do I move to the previous field in a recordset?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gnawoncents
    New Member
    • May 2010
    • 214

    How do I move to the previous field in a recordset?

    I am pretty inexperienced with VBA recordsets, and hope there is a simple fix to my problem. I have a recordset (rstQuestions) and am using a For Each...Next operation to go through the fields (fldQuestions) and pull data from relevant ones. I'm running into a problem though when I try to navigate to the previous field (not record).

    Here's what I'm trying to do... I need to find the relevant field in a recordset (done; no problems), move to the next field in that same record, retrieve and store the field's value in a string, then return to the previous field for further processing.

    I have tried fldQuestions.Mo vePrevious and fldQuestions.Mo veNext, but those seem reserved for records, not fields. (I get an "Error 438: Object doesn't support this property or method.")

    I even experimented with "Next fldQuestions" and "Previous fldQuestions," with equally poor results.

    Is what I'm attempting possible? If so, what is the correct syntax?

    I know I could get the same data with a DLookup function, but since my code runs through every field of every record, I'm pretty sure that would slow down the procedure significantly.

    Any ideas? Thank you in advance for your help.
  • Mariostg
    Contributor
    • Sep 2010
    • 332

    #2
    YourRecordSet!Y ourFieldName to select a specific field value at a given recordset row.

    Ex:
    rs.open sql, cn
    rs.MoveFirst 'Moves to first Record
    x=rs!YourFieldN ame 'Assign the value of YourFieldName value to x.

    Comment

    • gnawoncents
      New Member
      • May 2010
      • 214

      #3
      Thanks for the reply.

      Since the field name in question changes with each iteration of the procedure, I had previously tried a variation of your suggestion:

      strComments = Nz(rstQuestions !(strLOOKUP), "")

      ...where strLOOKUP is the field name I need.

      However, I got the compile error, "Type-declaration character does not match declared data type."

      What am I missing here?

      Comment

      • Mariostg
        Contributor
        • Sep 2010
        • 332

        #4
        It's rstQuestions!(s trLOOKUP) that creates the error. No brackets...rstQ uestions!strLOO KUP

        Comment

        • gnawoncents
          New Member
          • May 2010
          • 214

          #5
          When I remove the parentheses, then it just looks for a field named "strLOOKUP, " which doesn't exist. Is there a better way to indicate strLOOKUP is a string than parentheses?

          Comment

          • Mariostg
            Contributor
            • Sep 2010
            • 332

            #6
            I think I understand better what you want to do. This snippet should give you a good clue
            Code:
                Dim f As String
                f = "YourDesiredFieldName"
                rs.Open sql, cn
                strComments = Nz(rs.Fields(f), "")

            Comment

            • gnawoncents
              New Member
              • May 2010
              • 214

              #7
              Thanks! It seems to be working now. I was missing the ".Fields" portion of the code. Sorry for not explaining myself better earlier.

              Comment

              • Mariostg
                Contributor
                • Sep 2010
                • 332

                #8
                No problem. Glad it is working. You might want to mark your post as solved.

                Never Mind... you did mark it.
                Last edited by Mariostg; Sep 20 '10, 05:32 PM. Reason: Happy trigger.

                Comment

                Working...