Results 1 to 14 of 14
  1. #1
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82

    Error returning Items from Collections


    HI all,

    I am populating a collection like so:

    Code:
    'Standard Module
    Dim Ctl As Control                                          ' Declare Ctl as a Control
    Dim clText As Cls_Text                                      ' Declare clText for use by the Class
        
        Set ColTxtBox = New Collection                          ' Note that the collection is declared in module declarations (Mod_Example)
        For Each Ctl In Me.Controls
            If TypeName(Ctl) = "TextBox" Then
                Set clText = New Cls_Text                       ' Create instance of the class
                Set clText.CTxt = Ctl                           ' Assign the control to the class instance
                clText.CTxt.OnGotFocus = "[Event Procedure]"    ' Assign an event procedure to the class control
                clText.CTxt.OnClick = "[Event Procedure]"
                clText.CTxt.AfterUpdate = "[Event Procedure]"
                ColTxtBox.Add clText, Ctl.Name                  ' Add the class instance to the collection
            End If
        Next Ctl
    The purpose here is to assign a class to a series of textboxes.
    Code:
    'Class Module
    Public WithEvents CTxt As Access.TextBox
    Private Sub CTxt_Click()
        CTxt.BackColor = RGB(255, 0, 0)
    End Sub
    All of this is working fine, however, ColTxtBox.item(number) throws an error and does not work.

    Code:
    Error: 438: Object doesn't support this property or method
    I would like to be able to loop through each item in the collection and modify it, however, this .item error seems not to allow me to see the collection object. Why not?

    Note that ColTxtBox.count does work just fine, so I know that the collection contains items.

    Thanks for helps!

    Cheers

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Do you have Option Explicit at top of your module??
    Is ColTxtBox defined/dimmed?
    Set ColTxtBox = New Collection

  3. #3
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    Yes. as mentioned in code ' Note that the collection is declare in module declarations (Mod_Example)

  4. #4
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Where are you using this
    ColTxtBox.item(number)
    and how???

  5. #5
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    Something like:

    Code:
    For i = 1 to ColTxtBox.Count
          Debug.Print ColTxtBox.item(i)
    next i


    Strangely, the .Count works fine. So the declaration is functional. it's just the .item(#) seems not to work.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  7. #7
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    ... Guys. Seriously. I posted the code in this very thread.

    All I need is to know why the collection item (which should work as documented) is not working.

  8. #8
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Quote Originally Posted by journeyman View Post
    Something like:

    Code:
    For i = 1 to ColTxtBox.Count
          Debug.Print ColTxtBox.item(i)
    next i


    Strangely, the .Count works fine. So the declaration is functional. it's just the .item(#) seems not to work.
    You can use ColTxtBox(i) as well, both versions should work. The problem seems to be the action you want to do, you want to debug.print an object of type clText. The immediate window is for things that can be printed, like text or numbers, not objects.

    When you did this:
    Code:
    ColTxtBox.Add clText, Ctl.Name
    You specified that each item in the collection will be of type clText, and it will have an index of type string. Let's say for a moment that one of your textboxes is called "txtSomeTextbox". If you do this: ColTxtBox("txtSomeTextbox") It will return the clText class assigned to that textbox. What does that class returns? Not a string or number, maybe, so it throws an error.

    If you want to know the content of the collection, just inspect your collection.
    Code:
    For Each Ctl In Me.Controls        If TypeName(Ctl) = "TextBox" Then
                Set clText = New Cls_Text                       ' Create instance of the class
                Set clText.CTxt = Ctl                           ' Assign the control to the class instance
                clText.CTxt.OnGotFocus = "[Event Procedure]"    ' Assign an event procedure to the class control
                clText.CTxt.OnClick = "[Event Procedure]"
                clText.CTxt.AfterUpdate = "[Event Procedure]"
                ColTxtBox.Add clText, Ctl.Name                  ' Add the class instance to the collection
            End If
        Next Ctl
        Stop
    That Stop is going to interrupt your code at that line. You can then view what items ColTxtBox is holding in your Locals window.
    So, that's why I wanted to know how you're using it and where. I suppose you don't want to just debug.print the objects, you must want to do something with them, right? that's why they're in the collection.

    TL;DR
    You are debug.printing a clText, which is an object, not a string or number. See what it outputs if you do this:
    Code:
    For i = 1 to ColTxtBox.Count
          Debug.Print TypeName(ColTxtBox.item(i))
    next i


  9. #9
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    Thank Edgar for your explanation - very helpful and I appreciate the time taken to offer it.

    I suppose you don't want to just debug.print the objects, you must want to do something with them, right? that's why they're in the collection.
    This is correct. I actually want to be able to assign a backcolor to all the items in the collection (among other things), and was attempting to figure out the minutia on my own.

    Hence why I was asking about the debug to test that I've hit the right control.

    But ...
    For i = 1 to ColTxtBox.Count
    Debug.Print TypeName(ColTxtBox.item(i))
    ColTxtBox(i).backcolor = RGB(0,0,0)
    next i

    ... is not doing it for me. Neither the debug nor the control backcolor will work. Why? I'm looping each item in the collection

    Note: using your suggestion outputs 'Cls_Text'

    I don't want to know the name of the class object. I want to get the name of the item, its contents, and it's properties.

    the item property should get me there. it doesn't. hence the frustration.

    Cheers

  10. #10
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    If my suggestion outputs Cls_Text, then we are doing it right. It was just meant to remove the error and let you see that you have items in your collection. I suppose that text appeared as many times as items in your collection count. Let's now forget about this, I don't want to confuse you.

    I don't want to know the name of the class object. I want to get the name of the item, its contents, and it's properties.
    I think you'd be more lucky using a dictionary instead of a collection, given that you are adding the name of the control in the key of the item of the collection. You see, keys can not be retrieved from a collection, you have to know them before hand, the only way to get the keys of your collection is doing low level memory management. But a dictionary will let you access the key and the value. I will take a better look in a few hours.

    Peace



  11. #11
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    Awesome. Thanks for taking a run at it!

  12. #12
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    After a closer look, you're doing this:
    Set clText.CTxt = Ctl

    So, you should do this:
    Code:
        For i = 1 To ColTxtBox.count
            ColTxtBox(i).CTxt.BackColor = RGB(0, 0, 0)
        Next i
    Inspecting your initialized variables using the Stop keyword I mentioned would help you accelerate your development greatly.

  13. #13
    journeyman is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Dec 2022
    Posts
    82
    Thank you Edgar. This works exactly as I had hoped it would.

    It wasn't obvious to me before, but your explanation makes it very understandable.

    I feel like that if this is the solution then you could add other controls to the collection (i.e. Not CTxt)....

    E.g.
    For i = 1 To ColTxtBox.count
    ColTxtBox(i).CTxt.BackColor = RGB(0, 0, 0)
    ColTxtBox(i).ABox.Backcolor = RGB(255,255,255) ' Other added control to the collection
    Next i


    If so, I can see how this would be very useful.

    Thanks again.

  14. #14
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    If previously assigned, then yes.

    You'd probably want to add LostFocus events to your class as well, but maybe you've already done that. I would probably add a bunch of properties to that class as well, but anyway, good luck, it does seem like it will be able to handle an augmented user interaction. Don't forget about the inspections, it will be very helpful for you.

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

Similar Threads

  1. Dictionaries versus Collections
    By neuk in forum Programming
    Replies: 3
    Last Post: 07-22-2020, 09:32 AM
  2. Returning wrong items
    By cbende2 in forum Access
    Replies: 5
    Last Post: 08-01-2014, 07:43 AM
  3. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM
  4. query returning extra items
    By cbrsix in forum Queries
    Replies: 6
    Last Post: 07-05-2011, 02:22 PM
  5. Budget and Collections(Actuals)
    By dref in forum Forms
    Replies: 0
    Last Post: 08-19-2010, 03:39 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