Results 1 to 8 of 8
  1. #1
    naeemahmad is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Location
    Riverside, USA
    Posts
    46

    Dcount using textbox with like

    Hi guys. I think this is pretty siple but cant seem to resovle its syntax. Heres my code and what im trying to achieve;

    I want to user to type in values in a textbox..then the value is used as a criteria for a DCount function to determin is records exist.

    Source Field Data Type is NUMBER

    QUESTION:
    1) Can or should I use a string valriable and build the like (wildcard) expression to use as a criterion or can or should I build the LIKE expression within the DCount expression, and if so.. can someone help me with that dcount and LIKE syntax?

    THANKS ALL FOR ANY GUIDENCES!

    Private Sub Combo89_Change()
    Me.Refresh
    Dim LNumofRecs As String


    Dim qryPRM As String

    qryPRM = Forms![frmTIMESHEET_MAINMENU]![Combo89] & "*"

    LNumofRecs = DCount("*", "qry_MAINMENU_QRY_LT_RESULTS", "[LT] = " & qryPRM & "")

    Me.Requery
    If LNumofRecs <= 0 Then
    MsgBox ("no rec found!")
    Exit Sub
    End If

    End Sub

  2. #2
    naeemahmad is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Location
    Riverside, USA
    Posts
    46
    OK guys. Just figereued it out. Here my expression syntax below for anyone with the same simple question. just had to look at it one more time and it clicked.

    LNumofRecs = DCount("*", "qry_MAINMENU_QRY_LT_RESULTS", "[LT] Like ""*" & Forms![frmTIMESHEET_MAINMENU]![Combo89] & "*""")

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Instead of storing the Dcount result as a string I would

    Dim LNumofRecs As long

    Also, you just need to include the single quote with the = operator

    "[LT] = '" & qryPRM & "'")

    should do it

    finally
    why use a wild card for your Dcount field? Grab the PK field for that domain.

    DCount("PrimaryKeyField", "qry_MAINMENU_QRY_LT_RESULTS",

  4. #4
    naeemahmad is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Location
    Riverside, USA
    Posts
    46
    Very cool. Thanks for the helpfull reminder. Also you are right about storing DCount as string.

    Quick Question: Can you please expand/clarify your suggestion; .. sound very interesting.

    "Grab the PK field for that domain."

    Thanks agin!

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    The first argument in the Dcount() function is requesting a field name. By using the wildcard, I believe you are asking it to count all field names. Perhaps it only considers the first field it comes across, don't know. I try to explicitly define the field it should count and also define a field that I know is "Indexed" and of Number data type.

    Replace the * with

    DCount("PrimaryKeyField", "qry_MAINMENU_QRY_LT_RESULTS",

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Use of wildcard in DCount or Count is valid because the function counts records. Except for nulls, content of fields is irrelevant so designating a field is not necessary in the field argument. Using wildcard and not specifying field should include all records in the count as long as each record has value in at least any one field. If the count must be restricted then use the WHERE CONDITION argument. Specifying a field will exclude records from count with null in the field (actually, shows 0 if there is a GROUP BY clause). Maybe specifying a field where it is certain to have a value in every record is more efficient, but I doubt it will be noticeable.
    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.

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Interesting info, thanks

  8. #8
    naeemahmad is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Location
    Riverside, USA
    Posts
    46
    Great info. Thank you. Actually that's exactly why I chose the count all rows as opposed to a particular field. Although there may not be much (if any) potential variance.. I just didn't want to be exclusive to one field. Just my OCD kicking in again! LoL

    Anyways, great info and really appreciate the responses!!!

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

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2014, 05:18 PM
  2. Replies: 2
    Last Post: 04-20-2013, 03:37 AM
  3. Replies: 8
    Last Post: 04-12-2013, 08:59 PM
  4. Replies: 17
    Last Post: 02-08-2012, 10:06 AM
  5. Replies: 0
    Last Post: 06-11-2009, 09:51 AM

Tags for this Thread

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