Test for duplicate value before running append query

R

Ruth

I have a form that collects information for a after-market part to be
built. There are two combo boxes-- the first lists the Original Mfg.
Name, which filters the second box that lists the part numbers for
that mfg. The third is a text box where a serial number is entered.
The combination of the OEM part number (PN) and the aftermarket
producer's Serial Number (SN) make up the PartID. There are some other
information fields as well.

I created an append query that concatenates the PN and SN fields to
CellID which becomes the Key field in the primary table. I don't want
to see the append message each time, but if I turn off warnings, you
don't see the error message when the update fails. I don't think the
Dcount is working because the message box doesn't activate.

Here's the code I have now:
Private Sub btnAddCell_Click()
On Error GoTo btnAddCell_Err

' Turn Off Screen Display while macros run
DoCmd.Echo False, "Please wait while new cell is created "
' Turns on hourglass while macro runs
DoCmd.Hourglass True
' Turns warning off so you don't answer prompt while macro runs
DoCmd.SetWarnings False
' Saves new record
DoCmd.RunCommand acCmdSaveRecord
'Checks for duplicate values that with make up Cell ID
If DCount("*", "[tblCellEntry]", [PN] = """ & Me.PN & """ And [SN]
= """ & Me.SN & """) = 0 Then
' RunsQuery to Add New Cell
DoCmd.OpenQuery "qryAddNewCell", acViewNormal, acEdit
' Closes append query
DoCmd.Close acQuery, "qryAddNewCell"
' Closes New Cell Form
DoCmd.Close acForm, "frmNewCell"
' Turns off hourglass
DoCmd.Hourglass False
' Turns warnings back on
DoCmd.SetWarnings True
' Activates Screen display again
DoCmd.Echo True, ""
Else
MsgBox "A cell already exists with this S/N. Choose a new S/N
or return to the Main Menu and Edit Existing Cell",
(vbDefaultButton1), "Duplicate S/N"
End If

btnAddCell_Exit:
Exit Sub

btnAddCell_Err:
MsgBox "A cell already exists with this S/N. Choose a new S/N or
return to the Main Menu and Edit Existing Cell", (vbDefaultButton1),
"Duplicate S/N"
Resume btnAddCell_Exit
End Sub.

I'm sure there's a glaring error there somewhere, but I'm not finding
it!

Many thanks in advance,
Ruth
 
R

Ruth

Please disregard references to "macro" in the notes... I reused some
of my old code (that worked!) that I converted from a macro some moons
ago.
 

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