Results 1 to 5 of 5
  1. #1
    sleptema is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    6

    DCount() not counting?

    I have a button that users can add customers. When they click an inputbox records their result, and if the customer doesn't exist, it adds it to tblCustomer, if it does, it brings a warning message up. The problem is it always rejects the input whether it is a customer or not. The insert into command works fine, so it cannot be that.

    Any ideas?

    Code:
    Private Sub cmdNewCustomer_Click()
        Dim NewCustomer As String
        Dim StrSQL As String
        
    AddCustomer:
            NewCustomer = InputBox("What is the new customers name?", "Add Customer")
            
            If NewCustomer = "" Then
            Exit Sub
            ElseIf DCount("Customer", "tblCustomer", "' & NewCustomer & '") > 0 Then
            MsgBox "That customer already exists!", vbInformation, "Duplicates Detected"
            GoTo AddCustomer
            End If
               
            DoCmd.OpenTable "tblCustomer", acViewNormal, acAdd
            DoCmd.GoToRecord acDataTable, "tblCustomer", acNewRec
            StrSQL = "INSERT INTO tblCustomer (Customer) VALUES ('" & NewCustomer & "')"
            CurrentDb.Execute StrSQL, dbfailonerror
            DoCmd.Close acTable, "tblCustomer", acSaveYes
            cboCustomer.Requery
            
            MsgBox "New customer recorded!", vbInformation, "Entry Successful"
            
    End Sub


  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Your DCount() syntax needs adjustment in the last argument.
    http://access.mvps.org/access/general/gen0018.htm

  3. #3
    sleptema is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    6
    You may need to elaborate on that further please.

    For strings: (note the apostrophe before and after the value)
    DLookup("FieldName" , "TableName" , "Criteria= 'string'")

    The field within the table is "Customer"
    The table name is "tblCustomer"
    The criteria I am searching for is the string that the inputbox output, which in debug mode, shows the value I want.

    I can't see what I am doing wrong.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The criteria argument needs to be an expression: "somefield=somevalue"

    DCount("Customer", "tblCustomer", "Customer='" & NewCustomer & "'")
    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
    sleptema is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2013
    Posts
    6
    Quote Originally Posted by June7 View Post
    The criteria argument needs to be an expression: "somefield=somevalue"

    DCount("Customer", "tblCustomer", "Customer='" & NewCustomer & "'")
    God damnit! I was so close. Anyway you were bang on the money, works a charm now. Thanks so much. Stuck on that issue for a long time haha.

    (obviously I am no VBA genius hahaha)

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

Similar Threads

  1. DCount
    By Newbie11 in forum Reports
    Replies: 4
    Last Post: 08-03-2012, 12:23 AM
  2. DCount
    By nsteenhaut in forum Queries
    Replies: 2
    Last Post: 10-04-2011, 05:00 PM
  3. DCount only counting some dates
    By Jules48 in forum Programming
    Replies: 7
    Last Post: 09-22-2011, 02:29 AM
  4. DCount and If
    By Madmax in forum Access
    Replies: 3
    Last Post: 07-28-2011, 06:53 AM
  5. Help with Dcount
    By tozey in forum Programming
    Replies: 1
    Last Post: 08-10-2010, 10:53 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