Avatar of shieldsco
shieldsco
Flag 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
 Else
 'open form


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

 End If

Open in new window

Microsoft Access

Avatar of undefined
Last Comment
PatHartman

8/22/2022 - Mon
bfuchs

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?
PatHartman

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.
sumit yadav

Could you please provide your sample sheet?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Hamed Nasr

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.
Gustav Brock

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

mbizup

Try saving the record before performing this check.

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

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
shieldsco

ASKER
Gustav Brock - me.FULLNAME is the first record on the form
Gustav Brock

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

ASKER
1st messagebox displays the first FULLNAME on the form

2nd messagebox displays No Related record found
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Gustav Brock

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.
shieldsco

ASKER
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
Gustav Brock

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

And why have two tables with identical stuff?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
shieldsco

ASKER
it's a reconciliation table to find new records
Gustav Brock

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.
mbizup

Did you try my suggestion?  This makes a difference if you are talking about a bound control.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
shieldsco

ASKER
mbizup - yes and the result was the same .... no related record found
PatHartman

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.
bfuchs

can you try try suggestions
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
shieldsco

ASKER
bfuchs - changed "Name" to Name1" same result .... no related record found
bfuchs

did you also changed the table?

when moving the openform command inside the if block, do you see some records?
shieldsco

ASKER
yes changed the table ..... I don't see any records
Your help has saved me hundreds of hours of internet surfing.
fblack61
bfuchs

I don't see any records
So that means they're no matching records.
what are the names you looking for?
shieldsco

ASKER
FULLNAME = FULLNAME
Gustav Brock

It's not the name of the control, but an example of the text in the field.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
bfuchs

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 Nasr

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

ASKER
ok
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
PatHartman

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.
ASKER CERTIFIED SOLUTION
shieldsco

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Gustav Brock

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.
shieldsco

ASKER
No Gustav Brock that was not the issue... look at the two coded closely
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Gustav Brock

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.
shieldsco

ASKER
Gustav Brock you are still wrong... I changed my lookup table from the master to the temp table
Gustav Brock

That's what I suspected:

Well, obviously, somehow they differ. Change the table in the DLookup domain part.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
PatHartman

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.