Doubt in SQL, need a help here

Armando Vilela Baiões
Armando Vilela Baiões used Ask the Experts™
on
Hello Fellows

I have this SQL, which is fetching already inserted values ​​at an old date, for records with a new date. SQl is working fine, but I have some difficulty doing the following:
If I mistakenly re-run the query, the records become duplicates, I want to avoid this with something like "WHERE NOT EXISTS", but I'm not sure how to do that part. Can you give me a hand?

Greetings to all.

SQL:
Private Sub Command39_Click()

' remind that [DATA] must be like this -> #" & Format(DataFormatada, "mm/dd/yyyy") & "#
Dim DataFormatadaOld As Date
Dim DataFormatadaNew As Date
DataFormatadaOld = Format(Forms!frmFScomposicao!ComboFSDATA, "short date")
DataFormatadaNew = Format(Forms!frmFScomposicao!TransferData, "short date")

Dim db As Database
Dim strSQL As String
Set db = CurrentDb()

strSQL = "INSERT INTO tblFSListaTecnicos (AtribDataTecnico, AtribEquipaTecnico, TecnicoLista, FuncaoTecnicoLista) " _
& "SELECT #" & Format(DataFormatadaOld, "mm/dd/yyyy") & "#, '" & Forms!frmFScomposicao!ComboFSEQUIPA & "', " _
& "TecnicoLista, FuncaoTecnicoLista " _
& "FROM tblFSListaTecnicos " _
& "WHERE ((tblFSListaTecnicos.AtribDataTecnico = #" & Format(DataFormatadaNew, "mm/dd/yyyy") & "#) And (tblFSListaTecnicos.AtribEquipaTecnico = '" & Forms!frmFScomposicao!TransferEquipa & "'))"
db.Execute strSQL, dbFailOnError
Me.Refresh
MsgBox db.RecordsAffected & " Tecnicos Adicionados"





End Sub

Open in new window


Thanks in advanced
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
HuaMin ChenProblem resolver

Commented:
Suppose AtribDataTecnico is the relevant Key column in the table, then you can put

... where #" & Format(DataFormatadaOld, "mm/dd/yyyy") & "# not in (select AtribDataTecnico from tblFSListaTecnicos)"

Open in new window


into your query.

Author

Commented:
Thats not it.

Author

Commented:
Imagine this

in day AAAA
 I have this

Julian                       day AAAA
Mary                        day AAAA

i run query and in day BBBB

I want those people in day AAAA


So the result is:

Julian                       day BBBB
Mary                        day BBBB

But if i re-run query mistakenly in day BBBB

I get the result


Julian                       day BBBB
Mary                        day BBBB
Julian                       day BBBB
Mary                        day BBBB
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2016

Commented:
need more info;

<I want those people in day AAAA> how do you determine day AAAA ? is this the max date for these people?

Author

Commented:
No! That info i get from a combo in form.

There are 4 combos criteria....

DataFormatadaOld = Format(Forms!frmFScomposicao!ComboFSDATA, "short date") gets old date where i want to pick persons
DataFormatadaNew = Format(Forms!frmFScomposicao!TransferData, "short date")gets criteria for new date to person picked

Author

Commented:
sorry is the other way around DataFormatadaOld is the new date and DataFormatadaNew date from where i want to pick persons

Author

Commented:
But since I've been doing this for a couple of hours, I've chosen to do things differently. I will delete the query and I will do things with a listbox in which I will loop and compare with the inserted records, if they do not exist, he adds. I'm finishing programming now and it seems to be working fine. Anyway, I would like to know if you can do this with the query.

Author

Commented:
But I can better describe what I mean. Let's say that on X-Day I have Mary, Alex and Jon on team A. I'm going for day Y and I want those same people, but linked to a different date "Y" and a team that may be different or not.
Top Expert 2016

Commented:
<elaborate this more -  if they do not exist> what are your criterias to find if the records does not exists?  
which field contains the name of people?

Author

Commented:
those records are all in one table, the criteria for "if they do not exists" is a combined factor. If Julia combined with Date X And Team A dont exists, then insert, else, dont insert because all three combinations already exists.
Top Expert 2016

Commented:
what is the name of the field that contains Julia?
what is the name of the field that contains Team A?
what is the name of the field that contains Date X?

Author

Commented:
TecnicoLista [Julia]
AtribEquipaTecnico [Team A]
AtribDataTecnico [Date X]
Top Expert 2016

Commented:
here is a sample query using NOT Exists based on three  fields

SELECT tblFSListaTecnicos.TecnicoLista, tblFSListaTecnicos.AtribEquipaTecnico, tblFSListaTecnicos.AtribDataTecnico
FROM tblFSListaTecnicos 
WHERE NOT Exists(Select T.TecnicoLista FROM tblFSListaTecnicos As T Where T.TecnicoLista = tblFSListaTecnicos.TecnicoLista And  T.AtribEquipaTecnico =tblFSListaTecnicos.AtribEquipaTecnico And T.AtribDataTecnico = tblFSListaTecnicos.AtribDataTecnico) 

Open in new window


it will be best if you can upload a copy of the db

Author

Commented:
I still have not given up on the query, so I'll try your suggestion Rey.

Author

Commented:
Hi again
Rey,
I used your technique, but now the query did not fetch any records.2017-03-07.png
The two combos from above are the Date and Team which must appear in the copied records. The two bottom combos are the criteria where the query should fetch people and teams from old date and team already inserted that need to be copied.

Heres the code:
Dim DataFormatadaOld As Date
Dim DataFormatadaNew As Date
DataFormatadaOld = Format(Forms!frmFScomposicao!ComboFSDATA, "short date")
DataFormatadaNew = Format(Forms!frmFScomposicao!TransferData, "short date")

Dim db As Database
Dim strSQL As String
Set db = CurrentDb()

strSQL = "INSERT INTO tblFSListaTecnicos (AtribDataTecnico, AtribEquipaTecnico, TecnicoLista, FuncaoTecnicoLista) " _
& "SELECT #" & Format(DataFormatadaOld, "mm/dd/yyyy") & "#, '" & Forms!frmFScomposicao!ComboFSEQUIPA & "', tblFSListaTecnicos.TecnicoLista, tblFSListaTecnicos.FuncaoTecnicoLista" _
& " FROM tblFSListaTecnicos " _
& " WHERE not Exists(Select T.* FROM tblFSListaTecnicos As T Where T.TecnicoLista = tblFSListaTecnicos.TecnicoLista " _
& "And  T.AtribEquipaTecnico = '" & Forms!frmFScomposicao!TransferEquipa & "' " _
& "And T.AtribDataTecnico = #" & Format(DataFormatadaNew, "mm/dd/yyyy") & "#)"
db.Execute strSQL, dbFailOnError
Me.Refresh
MsgBox db.RecordsAffected & " Tecnicos Adicionados"

Open in new window

Top Expert 2016

Commented:
it will be best if you can upload a copy of the db. just fake sensitive info

Author

Commented:
ok... ill do that

Author

Commented:
Open form frmFScomposicao, then choose the two upper combo for Date 3-10-2017 and EQ3. You should see some records... next navigate to ... 4-10-2017. No records there. Then on the lower combos choose the first date 3-10-2017 and EQ3 that's the records i want for 4-10-2017.... any questions, im around.
Database1.accdb

Author

Commented:
I hope I have not forgotten to delete some references that generate a mistake, but I think everything is working in the basics.
Top Expert 2016
Commented:
copy this query and save as Q_Append
INSERT INTO tblFSListaTecnicos ( AtribDataTecnico, AtribEquipaTecnico, TecnicoLista, FuncaoTecnicoLista )
SELECT [Forms]![frmFScomposicao]![ComboFSDATA] AS AtribDataTecnico, [Forms]![frmFScomposicao]![ComboFSEQUIPA] AS AtribEquipaTecnico, T.TecnicoLista, T.FuncaoTecnicoLista
FROM tblFSListaTecnicos AS T LEFT JOIN (SELECT tblFSListaTecnicos.TecnicoLista FROM tblFSListaTecnicos WHERE (tblFSListaTecnicos.AtribDataTecnico=Forms!frmFScomposicao!ComboFSDATA) And (tblFSListaTecnicos.AtribEquipaTecnico=Forms!frmFScomposicao!ComboFSEQUIPA))  AS T2 ON T.TecnicoLista=T2.TecnicoLista
WHERE T2.TecnicoLista Is Null;

Open in new window


Note: Set the popup property of the form to NO first
open the form and make your selection, then run the query Q_Append
then check your table,

you will notice that if you don't make a new selection nothing will be added to the table.

post back your result.

here is the Query using EXISTS
INSERT INTO tblFSListaTecnicos ( AtribDataTecnico, AtribEquipaTecnico, TecnicoLista, FuncaoTecnicoLista )
SELECT [Forms]![frmFScomposicao]![ComboFSDATA] AS AtribDataTecnico, [Forms]![frmFScomposicao]![ComboFSEQUIPA] AS AtribEquipaTecnico, T.TecnicoLista, T.FuncaoTecnicoLista
FROM tblFSListaTecnicos AS T 
Where Exists (SELECT tblFSListaTecnicos.TecnicoLista FROM tblFSListaTecnicos WHERE (tblFSListaTecnicos.AtribDataTecnico=Forms!frmFScomposicao!ComboFSDATA) And (tblFSListaTecnicos.AtribEquipaTecnico=Forms!frmFScomposicao!ComboFSEQUIPA)) = False

Open in new window

Commented:
Hi Rey


I used AppendQuery and it fetches the data, but duplicates and triples them and so on. The same goes for EXISTS. Maybe this is not the right way to do this. Anyway, I used a more "mechanical" method. I put a listbox that is fed by the two Combos from the bottom. This ListBox has the records I want to fetch. Then with a Loop, these records are inserted into the form. This way, it works and I get the results I want.

The code is as follows:

Dim TotalCombo As Integer
TotalCombo = TecnicosParaCopia.ListCount
For x = 0 To TotalCombo - 1
TecnicosParaCopia = TecnicosParaCopia.ItemData(x)

'''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''
Forms!frmFScomposicao.subfrmFSlistatecnicos.SetFocus
Forms!frmFScomposicao.subfrmFSlistatecnicos!TecnicoLista.SetFocus
  DoCmd.GoToRecord , "", acFirst
 contadorperson = 0
Do While Not IsNull(Forms!frmFScomposicao!subfrmFSlistatecnicos!TecnicoLista)

 If Forms!frmFScomposicao.subfrmFSlistatecnicos!TecnicoLista = TecnicosParaCopia Then
 
     MsgBox "Tecnico " & [TecnicosParaCopia] & " repetido!", vbInformation, ""
    
     Me.TecnicosParaCopia.SetFocus
     Exit Sub
     End If
   Forms!frmFScomposicao.subfrmFSlistatecnicos.SetFocus
    DoCmd.GoToRecord , "", acNext

    Loop
   Forms!frmFScomposicao.subfrmFSlistatecnicos!TecnicoLista = Me!TecnicosParaCopia.Column(0)
   Forms!frmFScomposicao.subfrmFSlistatecnicos!FuncaoTecnicoLista = Me!TecnicosParaCopia.Column(1)
   Forms!frmFScomposicao.subfrmFSlistatecnicos!AtribDataTecnico = Me!ComboFSDATA
   Forms!frmFScomposicao.subfrmFSlistatecnicos!AtribEquipaTecnico = Me!ComboFSEQUIPA
   Forms!frmFScomposicao.subfrmFSlistatecnicos.SetFocus
''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''
Next x

Open in new window

Database1.accdb
Top Expert 2016

Commented:
way back in my post, I asked about the criteria you need to know what record that don't exists..
the solution - queries I created was based on the criteria you provided.

Author

Commented:
I'm not saying that your solution is not right. This is a case of misunderstanding ... no one is to blame. The functionality I'm looking for is simply to copy the records (eg names of people and skills) entered on 03/10/2017 to a later date, for example 10/10/2017. Anyway, I've been able to solve the problem, thanks also to your help. Thank you. Greetings.

Author

Commented:
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial