Results 1 to 8 of 8
  1. #1
    jillp is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2012
    Posts
    12

    dcount function - new to Access VBA

    Hello, I am very new to Access and the googling I have done suggests that I use Dcount, but I can't get it to work.


    I have a really simple table with just 2 field.

    Type (unique - key - no duplicates - number)
    Notes

    Now as the user enters a new type I want to check the table to check they have not entered before.

    But, I have the following and get 'type mismatch'
    What should I be doing ?
    Thanks


    Code:
    Dim TypeID As String
          
        TypeID = Me.Type.Value
        MsgBox TypeID
        
        
        If DCount("[Typeid]", "tablecontacttype", "Type" > 0) Then
            MsgBox TypeID
        End If

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    This should help:

    DLookup Usage Samples
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jillp is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2012
    Posts
    12
    Thank you very much.
    So simple when you know how.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem, and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    You want to check if the TypeID number exists in the table using DCount, so what you need is to check if the DCount function is > 0, so it needs to look like this:

    If DCount("[Typeid]", "tablecontacttype", "TypeID = " & typeID) > 0

    This assumes that TypeID is the name of the field in the tablecontacttype table.

    Notice that I put the ">0" outside the DCount; this is because you are comparing the result of the DCount function to 0.

    HTH

    John

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Your DCount() was malformed, as has been said, and needs to be corrected, but the problem is more complex than just the part that John has pointed out, with the > 0 having to be outside of the DCount()!

    You state that the Field name is Type, and that is what needs to be used in the DCount, not TypeID, which is a Variable! It should be

    Code:
    If DCount("[Type]", "tablecontacttype", "Type = " & Me.Type) > 0


    Also, your malformed DCount actually wasn't what caused the 'type mismatch' error! That was caused by these two lines:

    Code:
    Dim TypeID As String
    
    TypeID = Me.Type.Value


    TypeID is declared to be a String Variable, which is to say it is Text, but you then assign the Value of Me.Type to it, and Type is defined as a Number; hence the 'type mismatch!'

    BTW, Type is a Reserved Word, in Access, and should not be used as the name of a Control or Field! Doing so is likely to not only create problems, but create problems that will be very difficult to debug!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    jillp is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2012
    Posts
    12
    Thanks for all the help above.
    I have now on your advice renamed both the fields that I called 'type' and 'note' to more specific names.

    This is now working after your help, how do I get the thread to show 'solved'

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad we could help!

    At the top of the thread, in the green bar (on my PC, anyway) you'll see 'Thread Tools;' drop it down and selected 'mark as solved.'

    Good luck with your project, and Welcome to AFN!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Replies: 6
    Last Post: 06-01-2012, 03:51 PM
  2. Access 2007 Dcount
    By BeitersIT in forum Access
    Replies: 1
    Last Post: 04-21-2012, 01:20 PM
  3. DCOUNT function with IN statement
    By dwilson in forum Access
    Replies: 2
    Last Post: 08-12-2011, 09:54 AM
  4. Using a date range with Dcount function
    By mleberso in forum Reports
    Replies: 4
    Last Post: 06-17-2011, 08:56 AM
  5. DCount function problem
    By 10 Gauge in forum Forms
    Replies: 5
    Last Post: 02-28-2011, 02:08 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