Dcount to Find Matching Records on a Form

shieldsco
shieldsco used Ask the Experts™
on
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
 Else
 'open form


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

 End If

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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?
Distinguished Expert 2017

Commented:
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.
Could you please provide your sample sheet?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Hamed NasrRetired IT Professional

Commented:
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 Valuable Expert 2015
Distinguished Expert 2018

Commented:
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
Else
    ' open form
    DoCmd.OpenForm "frmManageStaff", , , "[NAME]='" & Me.FULLNAME & "'"
End If

Open in new window

mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
Try saving the record before performing this check.

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

Open in new window

Author

Commented:
Gustav Brock - me.FULLNAME is the first record on the form
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Very well.
So what does the messagebox display, please?

Author

Commented:
1st messagebox displays the first FULLNAME on the form

2nd messagebox displays No Related record found
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
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.

Author

Commented:
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
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Well, obviously, somehow they differ. Change the table in the DLookup domain part.

And why have two tables with identical stuff?

Author

Commented:
it's a reconciliation table to find new records
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
OK.
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.
mbizupNerd
Most Valuable Expert 2012
Top Expert 2013

Commented:
Did you try my suggestion?  This makes a difference if you are talking about a bound control.

Author

Commented:
mbizup - yes and the result was the same .... no related record found
Distinguished Expert 2017

Commented:
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

Author

Commented:
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?

Author

Commented:
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?

Author

Commented:
FULLNAME = FULLNAME
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
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.
Hamed NasrRetired IT Professional

Commented:
Try to recreate the issue in sample database, if problem stays upload the sample database. This is the more than 10 comments rule.

Author

Commented:
ok
Distinguished Expert 2017

Commented:
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.
Pat - quite naturally I tried everything suggested... what kind of question is that!!!  I found my own solution with input others.

 MsgBox "Employee Name is: '" & Me.Name1 & "'"
 


If DCount("*", "Employees", "[FULLNAME]='" & Me.Name1 & "'") = 0 Then
 
     MsgBox "No related record found", vbExclamation, "OHMA"
     Exit Sub
 Else
 'open form


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

 End If

Open in new window

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
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.

Author

Commented:
No Gustav Brock that was not the issue... look at the two coded closely
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
That's what I did. First:

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

Open in new window

Then:

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.

Author

Commented:
Gustav Brock you are still wrong... I changed my lookup table from the master to the temp table
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
That's what I suspected:

Well, obviously, somehow they differ. Change the table in the DLookup domain part.
Distinguished Expert 2017

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial