Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    easyrider is offline Advanced Beginner
    Windows 11 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64

    How to obtain the value of a record in DCount criteria argument

    OK, so I know that this is not possible. Per Microsoft's documentation: "Use the DCount function to count the number of records in a domain when you don't need to know their particular values."



    But what if I do want to know what the value of those records are?

    Background:

    Existing DB with a junction table (JT) to handle a M:M relationship between Users & Items. Originally had a form with checkboxes for each Item and got help on this forum in this thread to enable the user's items on the form via DCount. The client then decided that they preferred a Listbox of the items instead of the checkboxes. I tweaked the code for a listbox and all was well in the universe until I added a new item to the Item table. The form's listbox is sorted by item name when the form loads. Now of course, the For loop incorrectly enables the listitems since the listbox items are sorted by name instead of Item_ID. If I change the rowsource to sort the listbox by ItemID, it works but the list is not alphabetized which looks bad.

    Since I cannot get the value of the Item using DCount, what could I use to iterate the JT, obtain the Item's value, and select the listbox items associated with said user based on that value instead of the loop iteration?

    Thanks in advance guys,
    -Bill

    ps: I cannot post the DB, as it contains existing employee information. Although my status is "novice", I am comfortable with VBA code and some query design.

  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,870
    I suggest you make a copy of the database and keep only enough records to highlight the issue.
    Change the names etc to anonymize the data and post.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,572
    Puzzled here.
    You were using Dcount() in the other thread with criteria.?
    Why can't you do the same now?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Could you please show us the code you use now to select the listbox items? You would probably need to use a DLookup on the ItemsTable to get the PK based on the name.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    I cannot post the database as it already contains proprietary company information in addition to almost 1800 employees - way to much cleanup/name changing

  6. #6
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by Welshgasman View Post
    Puzzled here.
    You were using Dcount() in the other thread with criteria.?
    Why can't you do the same now?
    Because I added a new item to the ListT, Item3.5 after a significant amount of data was already in the JT. The listbox is sorted by name, alphabetically. If I sort it by the Item's PK, it works, but is not alphabetized. I put together a little module to show you the difference.

    The user has items 1, 2 & 4 in the junction table.

    This list box is sorted by name:

    Click image for larger version. 

Name:	SortedByName.PNG 
Views:	20 
Size:	5.7 KB 
ID:	49781


    This one is sorted by Item_ID
    Click image for larger version. 

Name:	SortedByPK.PNG 
Views:	18 
Size:	5.7 KB 
ID:	49782

  7. #7
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by Gicu View Post
    Could you please show us the code you use now to select the listbox items? You would probably need to use a DLookup on the ItemsTable to get the PK based on the name.

    Cheers,
    Here's the code, Vlad. It's called on the Form_Load event.

    Private Sub ShowAllocatedItems()
    Dim i As Integer
    Dim y As Integer

    For i = 1 To DMax("Item_ID", "Item_T")
    y = DCount("*", "UserXItem_JT", "User_ID=" & Me.User_ID & " AND Item_ID=" & i)
    If y > 0 Then
    ItemList.Selected(i - 1) = True
    End If
    Next i
    End Sub

    Yeah, I thought about DLookup too. But I've read several places that DLookup shouldn't be used inside of a loop due to performance issues. Unless, I use a recordset object - which I'm not opposed to.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,572
    Quote Originally Posted by easyrider View Post
    I cannot post the database as it already contains proprietary company information in addition to almost 1800 employees - way to much cleanup/name changing
    Not really?
    https://www.accessforums.net/showthr...t=randomize#19
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,572
    Quote Originally Posted by easyrider View Post
    Here's the code, Vlad. It's called on the Form_Load event.

    Private Sub ShowAllocatedItems()
    Dim i As Integer
    Dim y As Integer

    For i = 1 To DMax("Item_ID", "Item_T")
    y = DCount("*", "UserXItem_JT", "User_ID=" & Me.User_ID & " AND Item_ID=" & i)
    If y > 0 Then
    ItemList.Selected(i - 1) = True
    End If
    Next i
    End Sub

    Yeah, I thought about DLookup too. But I've read several places that DLookup shouldn't be used inside of a loop due to performance issues. Unless, I use a recordset object - which I'm not opposed to.
    So you are showing items in the list, nothing to do with the user, just not selecting them?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by Welshgasman View Post
    Due to the nature of my employer's business, I am forbidden to post the database, randomized or not. I have provided answers to your initial question, with screenshots.

  11. #11
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by Welshgasman View Post
    So you are showing items in the list, nothing to do with the user, just not selecting them?
    The listbox shows all of the items in the table, what I want is the listbox to highlight/select the items associated with said user when the form loads. See screenshots I posted earlier. Thanks!

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I am not quite understanding what you want so I will focus on this element "get the value of the Item". Possibly include item value as a field in listbox RowSource. Reference that field by listbox column index to pull value.
    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.

  13. #13
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Bill,

    The dLookup use in loops applies when you have over thousands of items, with your limited list it shouldn't be a problem. Just so I get it right, the listbox is based on the tblItems, the bound column is IndexId and in its row source you sort by Item Name. And you want to select all items for the current user that have at least one record for them in the join table tblUserItem. Is that the gist of of it?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  14. #14
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by Gicu View Post
    Bill,

    The dLookup use in loops applies when you have over thousands of items, with your limited list it shouldn't be a problem. Just so I get it right, the listbox is based on the tblItems, the bound column is IndexId and in its row source you sort by Item Name. And you want to select all items for the current user that have at least one record for them in the join table tblUserItem. Is that the gist of of it?

    Cheers,
    Pretty much, yeah. Just to clarify though, the listbox is unbound, the row source is:

    "SELECT [Item_T].[Item_ID], [Item_T].[ItemName] FROM Item_T ORDER BY [Item_ID];"

    And the bound column = 1

  15. #15
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Can you please try this?
    Code:
    Private Sub ShowAllocatedItems()
    Dim i As Integer
    Dim y As Integer
    
    
    For i = 0 To Me.ItemList.ListCount - 1
        y = DCount("*", "UserXItem_JT", "User_ID=" & Me.User_ID & " AND Item_ID=" & Me.ItemList) 'listbox is bound to Item_ID
        Me.ItemList.Selected(i) = Iif(y>0,True,False)
    Next i
    
    
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. dcount with 2 criteria
    By Nobby2193 in forum Access
    Replies: 2
    Last Post: 02-04-2019, 03:46 AM
  2. Replies: 6
    Last Post: 08-23-2012, 05:06 AM
  3. Replies: 15
    Last Post: 05-24-2012, 02:36 AM
  4. obtain record from report
    By teebumble in forum Reports
    Replies: 3
    Last Post: 07-22-2011, 04:25 PM
  5. Obtain value in record
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 01-20-2011, 08:52 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