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

shieldscoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bfuchsCommented:
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?
0
PatHartmanCommented:
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.
0
sumit yadavCommented:
Could you please provide your sample sheet?
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

hnasrCommented:
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.
0
Gustav BrockCIOCommented:
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

0
mbizupCommented:
Try saving the record before performing this check.

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

Open in new window

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

2nd messagebox displays No Related record found
0
Gustav BrockCIOCommented:
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.
0
shieldscoAuthor 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
0
Gustav BrockCIOCommented:
Well, obviously, somehow they differ. Change the table in the DLookup domain part.

And why have two tables with identical stuff?
0
shieldscoAuthor Commented:
it's a reconciliation table to find new records
0
Gustav BrockCIOCommented:
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.
0
mbizupCommented:
Did you try my suggestion?  This makes a difference if you are talking about a bound control.
0
shieldscoAuthor Commented:
mbizup - yes and the result was the same .... no related record found
0
PatHartmanCommented:
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.
1
bfuchsCommented:
can you try try suggestions
0
shieldscoAuthor Commented:
bfuchs - changed "Name" to Name1" same result .... no related record found
0
bfuchsCommented:
did you also changed the table?

when moving the openform command inside the if block, do you see some records?
0
shieldscoAuthor Commented:
yes changed the table ..... I don't see any records
0
bfuchsCommented:
I don't see any records
So that means they're no matching records.
what are the names you looking for?
0
shieldscoAuthor Commented:
FULLNAME = FULLNAME
0
Gustav BrockCIOCommented:
It's not the name of the control, but an example of the text in the field.
0
bfuchsCommented:
when if fact there are matching records
We need an example of those records, just the name and what you have in Me.FULLNAME.
0
hnasrCommented:
Try to recreate the issue in sample database, if problem stays upload the sample database. This is the more than 10 comments rule.
0
shieldscoAuthor Commented:
ok
0
PatHartmanCommented:
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.
0
shieldscoAuthor Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOCommented:
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.
0
shieldscoAuthor Commented:
No Gustav Brock that was not the issue... look at the two coded closely
0
Gustav BrockCIOCommented:
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.
0
shieldscoAuthor Commented:
Gustav Brock you are still wrong... I changed my lookup table from the master to the temp table
0
Gustav BrockCIOCommented:
That's what I suspected:

Well, obviously, somehow they differ. Change the table in the DLookup domain part.
0
PatHartmanCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.