Append Query is Able to Repeat the Entry

ddrew

seasoned user
Local time
Today, 21:37
Joined
Jan 26, 2003
Messages
911
I am running an Append Query [qry_AppendToWeatherFromGP] to update some comboboxes as part of an import function.

My query is:

Code:
INSERT INTO tbl_Weather ( Weather )
SELECT tbl_DetectionImports.Weather
FROM tbl_DetectionImports;

The function is:

Code:
Private Sub btn_SaveImport_Click()
    On Error GoTo btn_SaveImport_Click_Err

    DoCmd.SetWarnings False

    DoCmd.OpenQuery "qry_AppendToWeatherFromGP", acViewNormal, acEdit

    DoCmd.OpenQuery "qry_DeleteGPTrainingImports"

    DoCmd.SetWarnings True

    MsgBox "Record has been imported"
    Me.Requery
btn_SaveImport_Click_Exit:
    Exit Sub

btn_SaveImport_Click_Err:
    MsgBox Error$
    Resume btn_SaveImport_Click_Exit

End Sub

My problem is that if the item is already in the list it will put it in regardless. How can I prevent this from happening
 
simply make an outer join query
Something like
Code:
SELECT tbl_DetectionImports.Weather
FROM tbl_DetectionImports
Left join tbl_Weather on tbl_Weather.Weather = tbl_DetectionImports.Weather
where tbl_Weather.Weather  is null
 
How about rewriting the CODE to a RunTime Query?
Code:
Private Sub btn_SaveImport_Click()
    On Error GoTo btn_SaveImport_Click_Err
    CurrentDB.Execute "INSERT INTO tbl_Weather ( Weather ) " & _
                      "SELECT tbl_DetectionImports.Weather " & _
                      "FROM tbl_DetectionImports " & _
                      "WHERE tbl_DetectionImports.Weather NOT IN " & _
                      "(SELECT tbl_Weather.Weather FROM tbl_Weather);"

    DoCmd.OpenQuery "qry_DeleteGPTrainingImports"

    MsgBox "Record has been imported"
    Me.Requery
btn_SaveImport_Click_Exit:
    Exit Sub

btn_SaveImport_Click_Err:
    MsgBox Error$
    Resume btn_SaveImport_Click_Exit

End Sub
 
simply make an outer join query
Something like
Code:
SELECT tbl_DetectionImports.Weather
FROM tbl_DetectionImports
Left join tbl_Weather on tbl_Weather.Weather = tbl_DetectionImports.Weather
where tbl_Weather.Weather  is null

Do I still need to make it an Append Query?
 
Yes, the INSERT query's SELECT part should be replaced with the SELECT namliam provided. Good luck.
 

Users who are viewing this thread

Back
Top Bottom