Check Duplicate with VBA

  • Thread starter KimTong via AccessMonster.com
  • Start date
K

KimTong via AccessMonster.com

Dear all,

Can I do check duplicate using VBA when user enter data in a certain field?
If the data that tuser entered was already in the system, give a message
'Duplicate data' and the user have to enter a new data.

Thank you in advance.

KT
 
K

Ken Snell \(MVP\)

Use the control's BeforeUpdate event to check your table's data (code sample
below assumes the field is a numeric data type):

Private Sub NameOfTextbox_BeforeUpdate(Cancel As Integer)
If DCount("*", "NameOfYourTable", "NameOfFieldBeingChecked=" & _
Me.NameOfTextbox.Value) > 0 Then
Cancel = True
MsgBox "Duplicate data!"
Me.NameOfTextbox.Undo
End If
End Sub
 
R

riccifs

Use the control's BeforeUpdate event to check your table's data (code sample
below assumes the field is a numeric data type):

Private Sub NameOfTextbox_BeforeUpdate(Cancel As Integer)
If DCount("*", "NameOfYourTable", "NameOfFieldBeingChecked=" & _
Me.NameOfTextbox.Value) > 0 Then
Cancel = True
MsgBox "Duplicate data!"
Me.NameOfTextbox.Undo
End If
End Sub

Hi Ken,
I was trying to use your code to check for duplicates entry. But it's
giving to me an error like this "missing query operator..."
What I'm doing wrong?

Many Thanks.
Stefano.
 
K

Ken Snell \(MVP\)

The code I posted uses generic names. Post the code that you're trying to
use so that we can see the actual names and how you've constructed the code.
 
R

riccifs

The code I posted uses generic names. Post the code that you're trying to
use so that we can see the actual names and how you've constructed the code.

Hi Ken,
I copied and pasted your code as you posted and I changed it with my
name.

Private Sub Ragione_sociale_BeforeUpdate(Cancel As Integer)
If DCount("*", "tblEsercenti", "Ragione_sociale=" & _
Me.Ragione_sociale.Value) > 0 Then
Cancel = True
MsgBox "Duplicate data!"
Me.Ragione_sociale.Undo
End If
End Sub

I hope you will understand it, they are in Italian.

Many Thanks for your help!,
Stefano.
 
G

geppo

Ciao (e-mail address removed) wrote:
---cut---

prova cosi aggiugendo delle apici singole se Ragione_Sociale è tipo testo:
Private Sub Ragione_sociale_BeforeUpdate(Cancel As Integer)


If Not IsNull(DLookup("[Ragione Sociale]", "tblEsercenti", _
"[Ragione sociale]=" & Chr$(34) & Me.Ragione_sociale.Value & Chr$(34))) _
And Me.Ragione_sociale.Value <> Nz(Ragione_sociale.Value.OldValue) Then
Cancel = True
MsgBox "Duplicate data!"
Me.Ragione_sociale.Undo
End If

Oppure dai un occhiata qui è il sito da cui ho appreso il codice suddetto:

http://www.donkarl.com/it/FAQ/FAQ4Maschere.htm#4.25
I hope you will understand it, they are in Italian.

Many Thanks for your help!,
Stefano.

sei Italiano, allora posta su:

Microsoft.public.it.office.access

oppure

it.comp.appl.access

sono due ottimi NewsGroup in italiano.
 
R

riccifs

Ciao (e-mail address removed) wrote:

---cut---

prova cosi aggiugendo delle apici singole se Ragione_Sociale è tipo testo:
Private Sub Ragione_sociale_BeforeUpdate(Cancel As Integer)

If Not IsNull(DLookup("[Ragione Sociale]", "tblEsercenti", _
"[Ragione sociale]=" & Chr$(34) & Me.Ragione_sociale.Value & Chr$(34))) _
And Me.Ragione_sociale.Value <> Nz(Ragione_sociale.Value.OldValue) Then
Cancel = True
MsgBox "Duplicate data!"
Me.Ragione_sociale.Undo
End If

Oppure dai un occhiata qui è il sito da cui ho appreso il codice suddetto:

http://www.donkarl.com/it/FAQ/FAQ4Maschere.htm#4.25


I hope you will understand it, they are in Italian.
Many Thanks for your help!,
Stefano.

sei Italiano, allora posta su:

Microsoft.public.it.office.access

oppure

it.comp.appl.access

sono due ottimi NewsGroup in italiano.

Ciao Geppo,
grazie della tua risposta, ma mi da errore 424 oggetto richiesto...
che faccio!

Stefano.
 
G

geppo

Ciao said:
Ciao (e-mail address removed) wrote:

---cut---

prova cosi aggiugendo delle apici singole se Ragione_Sociale è tipo
testo:
Private Sub Ragione_sociale_BeforeUpdate(Cancel As Integer)

If Not IsNull(DLookup("[Ragione Sociale]", "tblEsercenti", _
"[Ragione sociale]=" & Chr$(34) & Me.Ragione_sociale.Value &
Chr$(34))) _ And Me.Ragione_sociale.Value <>
Nz(Ragione_sociale.Value.OldValue) Then Cancel = True
MsgBox "Duplicate data!"
Me.Ragione_sociale.Undo
End If

Oppure dai un occhiata qui è il sito da cui ho appreso il codice
suddetto:

http://www.donkarl.com/it/FAQ/FAQ4Maschere.htm#4.25


I hope you will understand it, they are in Italian.
Many Thanks for your help!,
Stefano.

sei Italiano, allora posta su:

Microsoft.public.it.office.access

oppure

it.comp.appl.access

sono due ottimi NewsGroup in italiano.

Ciao Geppo,
grazie della tua risposta, ma mi da errore 424 oggetto richiesto...
che faccio!

Stefano.

In che punto del codice ti da l'errore?


Comunque accertati che i nomi dei controlli siano scritti in modo corretto.
 
R

riccifs

Ciao said:
Ciao (e-mail address removed) wrote:
---cut---
prova cosi aggiugendo delle apici singole se Ragione_Sociale è tipo
testo:
Private Sub Ragione_sociale_BeforeUpdate(Cancel As Integer)
If Not IsNull(DLookup("[Ragione Sociale]", "tblEsercenti", _
"[Ragione sociale]=" & Chr$(34) & Me.Ragione_sociale.Value &
Chr$(34))) _ And Me.Ragione_sociale.Value <>
Nz(Ragione_sociale.Value.OldValue) Then Cancel = True
MsgBox "Duplicate data!"
Me.Ragione_sociale.Undo
End If
End Sub
Oppure dai un occhiata qui è il sito da cui ho appreso il codice
suddetto:
http://www.donkarl.com/it/FAQ/FAQ4Maschere.htm#4.25
I hope you will understand it, they are in Italian.
Many Thanks for your help!,
Stefano.
sei Italiano, allora posta su:
Microsoft.public.it.office.access
oppure
it.comp.appl.access
sono due ottimi NewsGroup in italiano.
Ciao Geppo,
grazie della tua risposta, ma mi da errore 424 oggetto richiesto...
che faccio!

In che punto del codice ti da l'errore?

Comunque accertati che i nomi dei controlli siano scritti in modo corretto..

In questo punto:

If Not IsNull(DLookup("[Ragione_sociale]", "tblEsercenti", _
"[Ragione_sociale]=" & Chr$(34) & Me.Ragione_sociale.Value & Chr
$(34))) _
And Me.Ragione_sociale.Value <>
Nz(Ragione_sociale.Value.OldValue) Then

Per i nomi sui controlli credo non siano scritti bene... boo!?

Grazie comunque,
Stefano.
 
R

riccifs

Ciao said:
Ciao (e-mail address removed) wrote:
---cut---
prova cosi aggiugendo delle apici singole se Ragione_Sociale è tipo
testo:
Private Sub Ragione_sociale_BeforeUpdate(Cancel As Integer)
If Not IsNull(DLookup("[Ragione Sociale]", "tblEsercenti", _
"[Ragione sociale]=" & Chr$(34) & Me.Ragione_sociale.Value &
Chr$(34))) _ And Me.Ragione_sociale.Value <>
Nz(Ragione_sociale.Value.OldValue) Then Cancel = True
MsgBox "Duplicate data!"
Me.Ragione_sociale.Undo
End If
End Sub
Oppure dai un occhiata qui è il sito da cui ho appreso il codice
suddetto:
http://www.donkarl.com/it/FAQ/FAQ4Maschere.htm#4.25
I hope you will understand it, they are in Italian.
Many Thanks for your help!,
Stefano.
sei Italiano, allora posta su:
Microsoft.public.it.office.access
oppure
it.comp.appl.access
sono due ottimi NewsGroup in italiano.
--
Ciao
Geppo
Ciao Geppo,
grazie della tua risposta, ma mi da errore 424 oggetto richiesto...
che faccio!
Stefano.
In che punto del codice ti da l'errore?
Comunque accertati che i nomi dei controlli siano scritti in modo corretto.

In questo punto:

If Not IsNull(DLookup("[Ragione_sociale]", "tblEsercenti", _
"[Ragione_sociale]=" & Chr$(34) & Me.Ragione_sociale.Value & Chr
$(34))) _
And Me.Ragione_sociale.Value <>
Nz(Ragione_sociale.Value.OldValue) Then

Per i nomi sui controlli credo non siano scritti bene... boo!?

Grazie comunque,
Stefano.

No, no! ho risolto... scusa ora mi funziona ho tolto le parentisi
quadre è ora va bene.
Ti ringrazio veramente molto, per il tuo aiuto.

Ciao Geppo!
Stefano.
 
G

geppo

Ciao said:
Ciao (e-mail address removed) wrote:
Ciao (e-mail address removed) wrote:

prova cosi aggiugendo delle apici singole se Ragione_Sociale è
tipo testo:
Private Sub Ragione_sociale_BeforeUpdate(Cancel As Integer)
If Not IsNull(DLookup("[Ragione Sociale]", "tblEsercenti", _
"[Ragione sociale]=" & Chr$(34) & Me.Ragione_sociale.Value &
Chr$(34))) _ And Me.Ragione_sociale.Value <>
Nz(Ragione_sociale.Value.OldValue) Then Cancel = True
MsgBox "Duplicate data!"
Me.Ragione_sociale.Undo
End If
Oppure dai un occhiata qui è il sito da cui ho appreso il codice
suddetto:

I hope you will understand it, they are in Italian.
Many Thanks for your help!,
Stefano.
sei Italiano, allora posta su:



sono due ottimi NewsGroup in italiano.
Ciao Geppo,
grazie della tua risposta, ma mi da errore 424 oggetto richiesto...
che faccio!

In che punto del codice ti da l'errore?
Comunque accertati che i nomi dei controlli siano scritti in modo
corretto.

In questo punto:

If Not IsNull(DLookup("[Ragione_sociale]", "tblEsercenti", _
"[Ragione_sociale]=" & Chr$(34) & Me.Ragione_sociale.Value & Chr
$(34))) _
And Me.Ragione_sociale.Value <>
Nz(Ragione_sociale.Value.OldValue) Then

Per i nomi sui controlli credo non siano scritti bene... boo!?

Grazie comunque,
Stefano.

No, no! ho risolto... scusa ora mi funziona ho tolto le parentisi
quadre è ora va bene.
Ti ringrazio veramente molto, per il tuo aiuto.

Di nulla, figurati.

p.s.: se hai bisogno posta sui newsgroup che ti ho segnalato, ci sono
ragazzi davvero forti.
Ciao Geppo!
Stefano.

Ciao Stefano
Geppo
 
K

Ken Snell \(MVP\)

I do not understand Italian, so I do not know if you found an answer in this
thread from your conversation with geppo or not?
 
R

riccifs

I do not understand Italian, so I do not know if you found an answer in this
thread from your conversation withgeppoor not?

Hi Ken,
with the help of Geppo I finally got a solution for my problem.
Many thanks the same for your interest about it, I really appreciate
that!

Bye,
Stefano.
 
K

KimTong via AccessMonster.com

Hi Ken,

I just to run your script, but the message said "Data Type mismatch" on this
part:

If DCount("*", "Roger", "AWB=" & _
Me.AWB.Value) > 0 Then

I am sure I have the same data type between those 2 fields. Do you know why
it happened like that?

KF
Use the control's BeforeUpdate event to check your table's data (code sample
below assumes the field is a numeric data type):

Private Sub NameOfTextbox_BeforeUpdate(Cancel As Integer)
If DCount("*", "NameOfYourTable", "NameOfFieldBeingChecked=" & _
Me.NameOfTextbox.Value) > 0 Then
Cancel = True
MsgBox "Duplicate data!"
Me.NameOfTextbox.Undo
End If
End Sub
Dear all,
[quoted text clipped - 6 lines]
 
G

geppo

Ciao said:
Hi Ken,

I just to run your script, but the message said "Data Type mismatch"
on this part:

If DCount("*", "Roger", "AWB=" & _
Me.AWB.Value) > 0 Then

I am sure I have the same data type between those 2 fields. Do you
know why it happened like that?

KF

but AWB that type of data contains?

If it is then type text you have to write her/it this way:

If DCount("*", "Roger", "AWB='" & Me.AWB.Value &"'") > 0 Then
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top