View Full Version : Append table without duplicate


Richie
06-16-2003, 11:15 PM
Hi,
How do you check that tblTempTable does not have same record as my tblTable1 before tblTempTable can be appended. If a criteria is required, how do you write it? TblTempTable is in Dbase2.mdb while tblTable1 is in Dbase1.mdb. Below is my code which I hope someone can correct or alter.
Many Thanks.

Private Sub btnAppend_Click()
On Error GoTo Err_btnAppend_Click

strSql = "INSERT INTO tblTempTable(TicketNo,FerryName,[FDate] IN 'C:\Dbase2.mdb' " & _
"SELECT tblTable1.TicketNo,tblTable1.FerryName,tblTable1.F Date FROM tblTable1 ;"

DoCmd.SetWarnings False
DoCmd.RunSQL strSql
DoCmd.SetWarnings True
'Debug.Print strSql

Exit_btnAppend_Click:
On Error Resume Next
Set db = Nothing
Set ws = Nothing
Exit Sub

Err_btnAppend_Click:
MsgBox Err.Description
Resume Exit_btnAppend_Click

End Sub

llkhoutx
06-17-2003, 10:17 AM
It looks like ticketNo might be unique, assuming it is and assuming you tablke is in the current mdb, before appending, search your table for that ticket number, if it does not exist, add the record;

strsql="tblTempTable(TicketNo

dim db as dao.database
dim rs as dao.recordset
dim strsql as string
set db=currentdb
set rs=db.openrecordset("tblTempTable",dbopensnapshot)
rs.findfirst "[TicketNo]=" & TicketNO
if rs.nomatch then exit sub
'yourcode to add record follows

Jon K
06-17-2003, 05:31 PM
Private Sub btnAppend_Click()
On Error GoTo Err_btnAppend_Click

Dim strSql As String

strSql = "INSERT INTO tblTempTable" & _
" (TicketNo, FerryName, FDate) In 'C:\Dbase2.mdb'" & _
" SELECT tblTable1.TicketNo, tblTable1.FerryName, tblTable1.FDate" & _
" FROM tblTable1 LEFT JOIN [C:\Dbase2.mdb].tblTempTable " & _
" ON (tblTable1.TicketNo = tblTempTable.TicketNo)" & _
" AND (tblTable1.FerryName = tblTempTable.FerryName)" & _
" AND (tblTable1.FDate = tblTempTable.FDate)" & _
" WHERE tblTempTable.TicketNo is null;"

DoCmd.SetWarnings False
DoCmd.RunSQL strSql
DoCmd.SetWarnings True

Exit Sub

Err_btnAppend_Click:
MsgBox Err.Description

End Sub

Richie
06-20-2003, 12:09 AM
Hi Guys,
I tried them and they worked. Thanks