Results 1 to 3 of 3
  1. #1
    PlamenGo is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jun 2011
    Location
    Bulgaria
    Posts
    22

    Looping through fields of a record (query or table)


    Hi, I have a problem in my application ...
    Need to loop throug the records in a Qry, which has 20 fields (columns), named Names, f1 ....f19 ...
    It has to find fields with data and put it in a separate table ...

    My code is:
    Code:
    Private Sub MakeApps()
    Dim fldName As String
    Set db = CurrentDb
    Set Rec2 = db.TableDefs("newTable").OpenRecordset
    Set Rec1 = db.QueryDefs("qryTest").OpenRecordset
      Rec1.MoveFirst
      Do Until Rec1.EOF
           For I = 1 To 19
             fldName = "Rec1!F" & Trim$(Str(I))
           '  (or fldName = "F" &  Trim$(Str(I))
           ' If Not IsNull(Rec1!fldName) Then
            If Not IsNull(fldName) Then
              Rec2.AddNew
              ' (code goes here ...)
              Rec2.Update
            End If
            Next
          Rec1.MoveNext
          Loop
        Rec1.Close
        Rec2.Close
        db.Close
    End Sub
    I am using a query as a rst (Rec1) because the I have to combine data from 2 tables (at the moment).
    Obviously, I cannot address the fields by name as described above. Can someone suggest another method of looping? of course I can write a lot of code usinf Ifs, and ElseIfs ... but it I am sure it can be done better.

  2. #2
    PlamenGo is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jun 2011
    Location
    Bulgaria
    Posts
    22
    Sorry for bordering, but almost immediately I figured out how to resolve (and I have been blocked with this for the last 2 days!
    I used the recordset Fields property ... For those who would be interested:
    Code:
      
      Do Until Rec1.EOF
           For I = 1 To Rec1.Fields.Count - 1
            If Not IsNull(Rec1.Fields(I)) Then
              Rec2.AddNew
    
              Rec2.Update
            End If
       
            Next
          Rec1.MoveNext
          Loop
        Rec1.Close
        Rec2.Close

  3. #3
    hardin is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2014
    Posts
    1
    sorry but can't understand it but try to resolve..

    iaf-ifa.org
    Last edited by hardin; 04-03-2014 at 01:02 AM.

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

Similar Threads

  1. Replies: 9
    Last Post: 01-31-2014, 12:09 PM
  2. Looping thru fields.
    By Newby in forum Access
    Replies: 4
    Last Post: 01-29-2013, 03:42 PM
  3. Replies: 2
    Last Post: 06-13-2012, 06:00 PM
  4. Looping query
    By jaykappy in forum Queries
    Replies: 13
    Last Post: 02-24-2012, 03:05 PM
  5. Replies: 7
    Last Post: 11-13-2010, 08:08 AM

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