Results 1 to 11 of 11
  1. #1
    JonathanT is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    118

    dCount referencing a control on another form

    I'm trying to use the DCount function to count the number or records in a table that have a specific value in a field. I want to get the value from a combo box on another form. The answer I'm getting is a count of all the records in the table, not just a count of the records of the ones matching the value in the combo box. If I hard code a value in my expression it returns the correct count, however when I try to get the value from a combo box the count is all the records in my table.



    Here is the syntax of what I'm using.

    =DCount("[ID]","[Spending]","[Forms]![Navigation]![NavVendor]")

    Any Ideas??

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    What is the name of the field, in "Spending", that will have the same value as "[Forms]![Navigation]![NavVendor]" and what is its' data type, Text or Numeric.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    JonathanT is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    118
    The field in Spending is "Vendor" and the data type is short text.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    Try:

    =DCount("[ID]","[Spending]","
    Vendor = '" & [Forms]![Navigation]![NavVendor] & "'")

    Note the use of single apostrophes which I have shown in red.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,970
    Are code and combobox located on forms used as TargetObjects on same navigation subform? If so, only one TargetObject at a time will be available and code will fail.
    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.

  6. #6
    JonathanT is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    118
    This code returned zero records as a count
    =DCount("[ID]","[Spending]","Vendor = '" & [Forms]![Navigation]![NavVendor] & "'")

  7. #7
    JonathanT is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    118

    Sample DB

    The combo box and text box with the code are on 2 separate forms. Both forms are open. No mainform/subforms exist. DB Attached
    Attached Files Attached Files

  8. #8
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,432
    This worked for me:

    Code:
    =DCount("[ID]","[Spending]","Vendor ='" & [Forms]![Navigation]![NavVendor].[column](1) & "'")

  9. #9
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    JohnathanT

    I have attached 2 Copies of your db.

    Spending1 is a corrected version of the db that you posted.
    The expression I gave you needed to be changed to:
    =DCount("[ID]","[Spending]","Vendor ='" & [Forms]![Navigation]![NavVendor].[column](1) & "'")
    as was shown by davegri
    The addition of [column](1) was required because the "Vendor" field in the spending table was, as you stated, a text field. However, any reference to the "Vendor" combo box returns a number because it uses the Vendor table for its data. That table has two data columns. First is unique identifier for the record (Primary Key). Second is the name of the vendor. The combo box holds both columns but the first is hidden because it means nothing to the user but although the user selects a name it is the numeric value that is saved. [column](1) is needed to reference the second column, which has the text name value. The column numbers start with 0 which is why the second is 1.

    In Spending2 I have changed the structure of the spending table by removing the text field "Vendor" and replacing it with a numeric field "VendorIDfk" wich now holds the Primary Key value from the Vendors table. IMHO that's the better way to do it. I don't see the point of having a Primary Key in the Vendors table and then using some other field in a related table. I would recommend making the same kind of changes regarding "Categories". With those changes to the spending table, we now need the following expression for DCount():
    =DCount("[ID]","[Spending]","VendorIDfk = " & [Forms]![Navigation]![NavVendor])
    I also changed the name of the primary key field in Vendors from ID which is a poor choice of name, especially if you use it other tables, to VendorID
    The apostrophes used as delimiters are no longer needed as the field and combo box are both numeric values.

    BTY I have also added several vendor names to the vendor table that had been used in the spending table but were not in the vendors table and have created a one to many relationship for them in the Relationships window. I have also enforced Referential Integrity between them.

    Hope this helps. Post back if you have any questions.
    Attached Files Attached Files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  10. #10
    JonathanT is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    118
    Bob and Davegri, Thanks for the help. Code works perfectly. Bob, I'll take a look at your suggestions for my tables

  11. #11
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    Quote Originally Posted by JonathanT View Post
    Bob and Davegri, Thanks for the help. Code works perfectly. Bob, I'll take a look at your suggestions for my tables
    Please do. Post back if you have any questions after viewing them
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Referencing form name and control in a procedure
    By Jerry Call in forum Access
    Replies: 3
    Last Post: 06-07-2020, 04:45 PM
  2. Replies: 6
    Last Post: 02-23-2014, 05:53 PM
  3. Replies: 3
    Last Post: 02-15-2013, 03:36 PM
  4. referencing a control
    By looloo in forum Programming
    Replies: 3
    Last Post: 09-23-2011, 07:57 PM
  5. Replies: 0
    Last Post: 12-08-2009, 01:19 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