473,473 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using DCount for duplicate records

4 New Member
Hi guys!
I'm new in the forum and new Access user as well. I have a form where users can insert new data to a table through it. I kow how how to use Dlookup but not familiar with DCount. I want my vba code to prevent the user of using a combination of values that have already been entered. More specific...

My table with name "Company" has three columns that I need to check.
1) column 1 - Company Name
2) column 2 - Street
3) column 3 - Post Code
The user will be able to enter a new record with only one of these to be the same with a previous one. If a same combination of two of them already exist in the table a message of duplicated record will appear.

code: "What I tried, but does not work"

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. Dim Street As String
  3. Dim Company_Name As String
  4. Dim Town As String
  5. If DCount("[Street]", "Company", "[Company_Name]='" & Me!Company_Name & "' AND [Town]='" & Me!Town & "'") > 0 Then
  6.     msg = "You already have that OrderID and Customer_ID combination" & vbNewLine
  7.     msg = msg & "The record will now be undone"
  8.     MsgBox msg, vbExclamation, "System Duplication Message"
  9.     Me.Undo
  10.     Cancel = True
  11. End If
  12. End Sub
Help please!!!!
Mar 11 '14 #1
8 6502
Seth Schrock
2,965 Recognized Expert Specialist
What do you mean "doesn't work"? Do you get an error message, does it not work as expected, etc.
Mar 11 '14 #2
Dkpitbull
4 New Member
Hi Seth!
Thanks for your reply!
What I get is: 'Run-time error '2465' Microsoft Access can't find the field 'Company_Name'

In my table the field is 'Company Name'
I think that I don't define the DCount correctly..
Mar 11 '14 #3
Seth Schrock
2,965 Recognized Expert Specialist
In your DCount() function, you have an underscore between the words 'Company' and 'name'. You need to have it the same as what is in your table. Based on the error message, I would say that without the underscore is the correct version.

The DCount() and DLookup() functions have the exact same syntax. The difference is what it returns.
Mar 11 '14 #4
Dkpitbull
4 New Member
I tried it with space like "Company Name" and hits as an error in the compiler.When I use Dlookup I set it like this:


Expand|Select|Wrap|Line Numbers
  1. Private Sub Part_Ref_BeforeUpdate(Cancel As Integer)
  2. Dim NewCompanyName As String
  3. Dim stLinkCriteria As String
  4. NewCompanyName = Me.Company_Name.Value
  5. stLinkCriteria = "[Company_Name] = " & "'" & NewCompanyName & "'"
  6. If Me.Company_Name = DLookup("[Company_Name]", "Companyform", stLinkCriteria) Then
  7. MsgBox "This " & NewCompanyName & ", has already been entered in database." _
  8. & vbCr & vbCr & "Please check company info again.", vbInformation, "Duplicate Information"
  9. Me.Undo
  10. End If
  11. End Sub
the above code works perfect but I need to check two values each time. I tried what you recomended without space and it doesn't work again.
Let me make it clear:
My form's name is 'Companyform'
My table is called 'Company'
And the fields of the table that I don't want to be the same are: 'Company Name' , 'Street', 'Town'

Thanks for your reply again Seth!!
Mar 11 '14 #5
Seth Schrock
2,965 Recognized Expert Specialist
Something isn't making sense as your code is using the form name in the Domain parameter which isn't valid per Microsoft's website. What is the exact error you get when you use 'Company Name' with just a space instead of using the underscore?
Mar 11 '14 #6
Dkpitbull
4 New Member
Access doesn'it allow it. the code in the compiler turns red..
Do you think I can use the Dlookup code that I sent you with replacing Dlookup with DCount?
Mar 11 '14 #7
Seth Schrock
2,965 Recognized Expert Specialist
No, I don't think that would work. Please post the code that turns red. The compiler doesn't know if the field names are correct, so there must be a different problem with that. When you post your code, please use the [CODE/] button at the top of the text editor and place your code between the code tags
Mar 12 '14 #8
GKJR
108 New Member
Have you tried Debug.Print on your variables for NewCompanyName and stLinkCriteria to make sure they look right? This is a pretty helpful resource for using DLookUp():

http://www.techonthenet.com/access/f...in/dlookup.php

I also agree that you definitely need to remove the underscore in your field name and just leave the space, so I think your error is in stLinkCriteria somewhere.
Mar 12 '14 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Schlauberger | last post by:
I am currently developing an application in VB dot Net where I am using MS Access parameter query as a data source. I have simplified my code but I am still generating a duplicate set of records?...
2
by: ms | last post by:
Access 2000: I am trying to delete duplicate records imported to a staging table leaving one of the duplicates to be imported into the live table. A unique record is based on a composite key of 3...
4
by: KT | last post by:
Is there any one click solution that would do the trick? I would like to create a button, so the person who maintains the database can perform clean up work to delete duplicate records which...
2
by: Carroll | last post by:
I'm looking for a way in SQL to find duplicate records in a single table, that are the same based on 3 columns, regardless of what is in the other columns in the duplicate records. I would like to...
2
by: nethravathy | last post by:
Hi, The following table namely elcbtripselect contains 5147 records.I want to know wether this table contains duplicate records or not. I tried with following query 1)SELECT...
9
by: rjshrader | last post by:
I have a table (tblStatus) with three fields (CustomerID, StatusType and StatusDate). I use an unbound form with three text boxes to enter data into the table when a command button (cmdSave) is...
1
by: colin-whitehead | last post by:
I have 2 tables tblReports primary key UPN, plus numeric fields Effort, Attain, etc tblComments numeric primary key ID & textfield Text In the Query I select each record from tblReports...
4
by: Thomas Arthur Seidel | last post by:
Hello to all, I have a small or big problem with a customer data base, where during a change of system we might have created duplicate records. This should be easy to find, you might think, but,...
2
by: nomvula | last post by:
hi guys i need some help to duplicate records on my form datasheet: here's the example of my form results: ClientLookup DateCaptured ForecastDate Description ForecastQuantity Forecast Actual UJ...
6
by: Dilip1983 | last post by:
Hi All, I want to delete duplicate records from a large table. There is one index(INDEX_U1) on 4 columns(col1,col2,col3,col4) which is in unusable state. First of all when i tried to rebuild...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.