Results 1 to 9 of 9
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479

    For each item in array


    Is item read only?
    Code:
    For Each item In Dat1
        If InStr(item, ". ") > 0 Then
        Else
            item = "DELETE"
        End If
    Next
    item does not change. Is this correct ?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I've never used "For Each" with array so just tested. It works for me. Array element value is changed.

    How do you know it did not change?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    I added a watch to Dat1 and also printed it to the immediate window.
    Yes, 'item' changed in the loop but the array did not.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Okay, I expanded my test. Confirmed item object variable is changed but array element is not.

    AFAIK, only way to change content of array element is to address it with index references. As in: Dat1(x) = "DELETE".
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    That's what I did June, but was surprised it didn't change the array.
    Thanks for confirming.

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Are you watching the wrong item? Remember that arrays are zero based, unless you explicitly alter the base number.
    I would not use item as a variable because it is a property name (e.g. Item(3) ).
    This is for Excel vba but I doubt that matters much. More than you'd want to know about arrays.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    How are Dat1 dimmed? Item? Can you post the code in broader context?

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    I don't know exactly what the purpose of your post is, whether to satisfy curiosity or to get results, but here's how to get results:
    Code:
    Public Function ta()
        Dim dat1(3) As String, i As Integer
        dat1(0) = "dog. "
        dat1(1) = "cat"
        dat1(2) = "pig."
        dat1(3) = "Mon. ster"
        Dim itm As Variant
        For i = 0 To UBound(dat1)
            If InStr(dat1(i), ". ") > 0 Then
            Else
                dat1(i) = "Delete"
            End If
        Next
        For Each itm In dat1
            Debug.Print itm
        Next
    End Function
    and the Immediate window:
    call ta
    dog.
    Delete
    Delete
    Mon. ster

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Middle, your code does not change array because it does not reference array element by index. So don't know what you mean by "That's what I did"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 6
    Last Post: 05-28-2023, 08:51 PM
  2. Replies: 12
    Last Post: 06-08-2021, 09:46 AM
  3. Replies: 3
    Last Post: 02-26-2019, 07:27 PM
  4. Replies: 7
    Last Post: 11-29-2015, 07:24 AM
  5. Replies: 2
    Last Post: 05-14-2015, 12:24 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