Link to home
Start Free TrialLog in
Avatar of shieldsco
shieldscoFlag for United States of America

asked on

Dcount to Find Matching Records on a Form

I using the following code to select matching records, however I'm getting the message that there are no related records when if fact there are matching records. Any help would be appreciated..

If DCount("*", "tblNewEmployees", "[NAME]='" & Me.FULLNAME & "'") = 0 Then
     MsgBox "No related record found", vbExclamation, "ATM"
     Exit Sub
 'open form

 DoCmd.OpenForm "frmManageStaff", , , "[NAME]='" & Me.FULLNAME & "'"

 End If

Open in new window

Avatar of bfuchs
Flag of United States of America image

think name is a reserved word.
try to create a query or name that field differently.
also what happens if you put in the docmd.openform inside the if block, does it opens with some records?
What is the name you are searching for?

Keep in mind that names commonly contain apostrophe's so you really can't use the single quote as the delimiter for a proper name.  You need to use double quotes.  I don't know if this is the problem but it is certainly going to cause one at some point.
Avatar of sumit yadav
sumit yadav

Could you please provide your sample sheet?
The syntax of the If expression is fine.
If DCount("*", "tblNewEmployees", "[NAME]='" & Me.FULLNAME & "'") = 0

Open in new window

Check it in the immediate window by substituting with a name copied from form.
Assume the full name copied is abc xyz,
If DCount("*", "tblNewEmployees", "[NAME]='" & "abc xyz" & "'") = 0

Open in new window

Me.FULLNAME appears to be a concatenation between LastName and FirstName.
What [Name] in table tblNewEmployees refers to?

A sample demo is helpful.
Most likely, your Me.FULLNAME is empty, or at least different from what you expect, so insert a MsgBox to verify.
Also, no reason to count, just look up:

MsgBox "Name is: '" & Me.FULLNAME & "'"
If IsNull(DLookup("[NAME]", "tblNewEmployees", "[NAME]='" & Me.FULLNAME & "'")) Then 
    MsgBox "No related record found", vbExclamation, "ATM"
    Exit Sub
    ' open form
    DoCmd.OpenForm "frmManageStaff", , , "[NAME]='" & Me.FULLNAME & "'"
End If

Open in new window

Try saving the record before performing this check.

docmd.RunCommand acCmdSaveRecord
If DCount("*", "tblNewEmployees", "[NAME]='" & Me.FULLNAME & "'") = 0 Then

Open in new window

Avatar of shieldsco


Gustav Brock - me.FULLNAME is the first record on the form
Very well.
So what does the messagebox display, please?
1st messagebox displays the first FULLNAME on the form

2nd messagebox displays No Related record found
Then that name is not present in the table. Or you have a leading space.

Or, the report may fetch data from somewhere else than this table.
the records are exactly the same in the two tables. I copied the name from on table to the other to ensure they are the same with the same result : no related record found
Well, obviously, somehow they differ. Change the table in the DLookup domain part.

And why have two tables with identical stuff?
it's a reconciliation table to find new records
But if the name is present, DLookup will look it up, so somewhere you have something, we don't know of, that changes the picture.
Did you try my suggestion?  This makes a difference if you are talking about a bound control.
mbizup - yes and the result was the same .... no related record found
Is one of the tables using a Lookup?  If so that is your problem.  One of the reasons (among many) that experts don't like using table level Lookup fields is because they mask the actual contents of a column.  In the Lookup field, it is NOT the text string that is stored, it is the ID.  So if you compare "abc xyz" to 99, you won't get a match.

Remove the Lookup from the table.  You might have to change other queries to include a join to the Lookup table to pick up the text name but this will eliminate future problems.  Use a combo on your form to pick text values from a list but save the underlying primary keys.  Lookups in a table are a crutch and cause nothing but problems once you advance to using queries and code.
can you try try suggestions
bfuchs - changed "Name" to Name1" same result .... no related record found
did you also changed the table?

when moving the openform command inside the if block, do you see some records?
yes changed the table ..... I don't see any records
I don't see any records
So that means they're no matching records.
what are the names you looking for?
It's not the name of the control, but an example of the text in the field.
when if fact there are matching records
We need an example of those records, just the name and what you have in Me.FULLNAME.
Try to recreate the issue in sample database, if problem stays upload the sample database. This is the more than 10 comments rule.
Did you look at the definitions of the two fields?  Is one a Lookup?  Would you be able to identify a Lookup if you saw one?

Several people have offered suggestions.  If one doesn't work, you might try another without being prompted.
Avatar of shieldsco
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh my. The field is named FULLNAME, not NAME.
Not even your first code could have run:

DoCmd.OpenForm "frmManageStaff", , , "[NAME]='" & Me.FULLNAME & "'"

Open in new window

You certainly could have found out at the first or second post.
No Gustav Brock that was not the issue... look at the two coded closely
That's what I did. First:

If DCount("*", "tblNewEmployees", "[NAME]='" & Me.FULLNAME & "'") = 0 Then

Open in new window


If DCount("*", "Employees", "[FULLNAME]='" & Me.Name1 & "'") = 0 Then

Open in new window

Neither table name, field name, nor control name match.

How you could miss that completely is not easy to understand.
Gustav Brock you are still wrong... I changed my lookup table from the master to the temp table
That's what I suspected:

Well, obviously, somehow they differ. Change the table in the DLookup domain part.
Pat - quite naturally I tried everything suggested... what kind of question is that!!!  I found my own solution with input others.
I am not prescient.  If you don't respond to a suggestion, how would I know that you tried it?  The fact that you are using a different table and that is working is telling indeed.  Can you strip the data from the two original tables and post them in a new db.  Do not do anything to the table definition.  Just remove the data.

Just FYI, do you know if Me.Name is the name of the form/report or your column named "name"?  You run into some interesting problems when you make the mistake of using certain reserved words as your column names.  Access simply should not allow it but sadly it does.