Results 1 to 9 of 9
  1. #1
    caniread is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    86

    Looping through form fields


    I have 21 fields in a form named thick0 through thick120. The field name stays the same but goes up by six on the number up to 120. Below is the code I wrote for thick0 & thick120 on an after update event and everything works. I could write the same code for every field but is there a way to tell it to loop through every field doing the same?

    Thank you for the help,

    Isaac

    Dim Mil As Double
    If Me.Thick0 < 1 Then
    Mil = Me.Thick0
    Mil = Mil * 1000
    Me.Thick0 = Mil
    Else
    End If

    Dim Mil As Double
    If Me.Thick120 < 1 Then
    Mil = Me.Thick120
    Mil = Mil * 1000
    Me.Thick120 = Mil
    Else
    End If

  2. #2
    Beetle is offline Unrelatable
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    Camp Swampy (Denver, CO)
    Posts
    207
    Assuming that any other controls on your form would not have Thick in the name, then something like the following would work;

    Code:
    Dim ctl As Control
        
    For Each ctl In Me.Controls
        If ctl.Name Like "*Thick*" Then
            If ctl < 1 Then ctl = ctl * 1000
        End If
    Next ctl

  3. #3
    caniread is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    86
    I unfortunately do have others that start with thick. example "ThickAVG"

    Maybe some way to tell it to add 6 to the name every time till you get to 120?

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    So you gave yourself the answer:
    Code:
    Dim i As integer
        
    For i=0 to 120
    
    
       if me.controls("Thick"&i)<1 then me.controls("Thick"&i)*1000
           
    Next i
    Cheers,
    Vlad

  5. #5
    caniread is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    86
    It gave me a invalid use of property error. Also not sure it will work since I go up by 6. There is no field named thick1 to 5. Just multiples of 6.

    What if I tag field? Same tag name for all and loop through those tag names? If possible what would the code look like?

  6. #6
    Beetle is offline Unrelatable
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    Camp Swampy (Denver, CO)
    Posts
    207
    Code:
    Dim ctl As Control
        
    For Each ctl In Me.Controls
        If ctl.Tag = "Some Value" Then
            If ctl < 1 Then ctl = ctl * 1000
        End If
    Next ctl

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Sorry, forgot the STEP:
    Code:
    Dim i As integer
        
    For i=0 to 120 STEP 6
    
    
       if me.controls("Thick"&i)<1 then me.controls("Thick"&i)= me.controls("Thick"&i)*1000       
    Next i

  8. #8
    caniread is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    86
    Thank you Beetle. I ended up using your code.

  9. #9
    Beetle is offline Unrelatable
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    Camp Swampy (Denver, CO)
    Posts
    207
    Glad I could help.

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

Similar Threads

  1. Split form and looping within records
    By jaryszek in forum Access
    Replies: 1
    Last Post: 11-13-2017, 05:24 AM
  2. Looping through fields of a record (query or table)
    By PlamenGo in forum Programming
    Replies: 2
    Last Post: 04-02-2014, 05:44 AM
  3. looping through controls on a form
    By khumbo in forum Forms
    Replies: 8
    Last Post: 03-17-2013, 03:30 PM
  4. Looping thru fields.
    By Newby in forum Access
    Replies: 4
    Last Post: 01-29-2013, 03:42 PM
  5. Looping through records in a continous form
    By accessnihon in forum Forms
    Replies: 3
    Last Post: 01-04-2012, 01:04 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