PESQUISA SQL

USUARIO.EXCLUIDOS 22/08/2007 08:03:42
#231965
Pessoal no código abaixo onde efetuo a pesquisa na TbIncidentes alguns registro soa duplicados devido ao critério de pesquisa como posso fazer para que a SQL agrupe a pesquisa pelo campo ID da tabela.


Desde já agradeço.....

Private Sub cmdOk_Click()
Call B_Dados

Dim Onde As String
Dim Cond As Boolean

SQL = "Select * From TbIncidentes"
Cond = False

If IsDate(txtData.Text) = True And IsDate(txtData1.Text) = True Then
If Cond = False Then
Onde = " Where Data Between #" & Format(txtData.Text, "mm/dd/yyyy") & "# And #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Data Between #" & Format(txtData.Text, "mm/dd/yyyy") & "# And #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
End If
ElseIf IsDate(txtData.Text) = True Then
If Cond = False Then
Onde = " Where Data = #" & Format(txtData.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Data = #" & Format(txtData.Text, "mm/dd/yyyy") & "# "
End If
ElseIf IsDate(txtData1.Text) = True Then
If Cond = False Then
Onde = " Where Data <= #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Data <= #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
End If
End If


If IsDate(txtData.Text) = True And IsDate(txtData1.Text) = True Then
If Cond = False Then
Onde = " Where Conclusao Between #" & Format(txtData.Text, "mm/dd/yyyy") & "# And #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Conclusao Between #" & Format(txtData.Text, "mm/dd/yyyy") & "# And #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
End If
ElseIf IsDate(txtData.Text) = True Then
If Cond = False Then
Onde = " Where Conclusao = #" & Format(txtData.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Conclusao = #" & Format(txtData.Text, "mm/dd/yyyy") & "# "
End If
ElseIf IsDate(txtData1.Text) = True Then
If Cond = False Then
Onde = " Where Conclusao <= #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Conclusao <= #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
End If
End If

If cmbCompetencia.Text <> "" Then
If Cond = False Then
Onde = " Where Comite_Competencia ='" & cmbCompetencia.Text & "'"
Cond = True
Else
Onde = Onde & " AND Comite_Competencia ='" & cmbCompetencia.Text & "'"
End If
End If

If optTrabalho.Value = True Then
If Cond = False Then
Onde = " Where Tipo_Incidente ='" & optTrabalho.Caption & "' "
Cond = True
Else
Onde = Onde & " AND Tipo_Incidente ='" & optTrabalho.Caption & "' "
End If
End If
If optAmbiental.Value = True Then
If Cond = False Then
Onde = " Where Tipo_Incidente ='" & optAmbiental.Caption & "'"
Cond = True
Else
Onde = Onde & " AND Tipo_Incidente ='" & optAmbiental.Caption & "'"
End If
End If

SQL = SQL & Onde
Dim RC As Recordset

Set RC = Bd.OpenRecordset(SQL, dbOpenSnapshot)

If RC.RecordCount = 0 Then
MsgBox "Não foi localizado nenhum registro que atenda ao critério da pesquisa!!!!", vbInformation, "Aviso"
cmdCancelar_Click
Exit Sub
End If

Do While Not RC.EOF
RC11.AddNew
RC11("ID") = RC("ID")
RC11("Data") = RC("Data")
RC11("Hora") = RC("Hora")
RC11("Relator") = RC("Relator")
RC11("Origem") = RC("Origem")
RC11("Empresa_Relatante") = RC("Empresa_Relatante")
RC11("Comite_Relatante") = RC("Comite_Relatante")
RC11("Empresa_Geradora") = RC("Empresa_Geradora")
RC11("Local_Incidente") = RC("Local_Incidente")
RC11("Tipo_Incidente") = RC("Tipo_Incidente")
RC11("Descricao_Incidente") = RC("Descricao_Incidente")
RC11("Acao_Imediata") = RC("Acao_Imediata")
RC11("Comite_Competencia") = RC("Comite_Competencia")
RC11("Causa_Incidente") = RC("Causa_Incidente")
RC11("Status") = RC("Status")
RC11("Previsao") = RC("Previsao")
RC11("Conclusao") = RC("Conclusao")
RC11("T_Conclusao") = RC("T_Conclusao")
RC11("Responsavel") = RC("Responsavel")
RC11("Tipo_Relato") = RC("Tipo_Relato")
RC11("Risco") = RC("Risco")
RC11("Empresa") = RC("Empresa")
RC11("Forma") = RC("Forma")
RC11.Update
RC.MoveNext
Loop
RC11.Close

Call Limpa_Melhoria
Call Calcula_Resultado
Call Carrega_Valores

End Sub


THIGO 22/08/2007 08:34:49
#231968
Amigo qual e o banco de dados que vc esta utilizando se for o oracle tenta usar o DISTINCT ou o group by talves de certo beleza


flw
USUARIO.EXCLUIDOS 22/08/2007 11:53:11
#232030
Thiago
Estou usando um banco de dados mdb, porem usando o DISTINCT ou o GROUP BY não funcionou, comecei a programar recentemente e acredito que seja o meu código que esteja errado, porem se alguém souber como faço para deletar na tabela os registros que possuem o mesmo código no campo ID já seria o suficiente assim a tabela não ficaria com dois registros onde o código fosse 20.


Agradeço o apoio

USUARIO.EXCLUIDOS 22/08/2007 12:24:01
#232040
Resposta escolhida
cara tenta assim

select distinct(id)
from TbIncidentes

o distinct tira as duplicidade.

Att,

Marcelo Bressan
THIGO 22/08/2007 14:34:28
#232065
amigo casa nao de certo com a dica do m_bressan faz um delete com esses registro que vc nao quer nesta tabela tipo delete tabela where id = !!


flw
USUARIO.EXCLUIDOS 22/08/2007 14:52:23
#232069
Amigo, se o problema é para não gravar registros duplicados na tabela do RC11, faça assim:


Private Sub cmdOk_Click()
Call B_Dados

Dim Onde As String
Dim Cond As Boolean
Dim IDAnt As Variant

SQL = "Select * From TbIncidentes Order By ID"
Cond = False

If IsDate(txtData.Text) = True And IsDate(txtData1.Text) = True Then
If Cond = False Then
Onde = " Where Data Between #" & Format(txtData.Text, "mm/dd/yyyy") & "# And #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Data Between #" & Format(txtData.Text, "mm/dd/yyyy") & "# And #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
End If
ElseIf IsDate(txtData.Text) = True Then
If Cond = False Then
Onde = " Where Data = #" & Format(txtData.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Data = #" & Format(txtData.Text, "mm/dd/yyyy") & "# "
End If
ElseIf IsDate(txtData1.Text) = True Then
If Cond = False Then
Onde = " Where Data <= #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Data <= #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
End If
End If


If IsDate(txtData.Text) = True And IsDate(txtData1.Text) = True Then
If Cond = False Then
Onde = " Where Conclusao Between #" & Format(txtData.Text, "mm/dd/yyyy") & "# And #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Conclusao Between #" & Format(txtData.Text, "mm/dd/yyyy") & "# And #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
End If
ElseIf IsDate(txtData.Text) = True Then
If Cond = False Then
Onde = " Where Conclusao = #" & Format(txtData.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Conclusao = #" & Format(txtData.Text, "mm/dd/yyyy") & "# "
End If
ElseIf IsDate(txtData1.Text) = True Then
If Cond = False Then
Onde = " Where Conclusao <= #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
Cond = True
Else
Onde = Onde & " AND Conclusao <= #" & Format(txtData1.Text, "mm/dd/yyyy") & "# "
End If
End If

If cmbCompetencia.Text <> "" Then
If Cond = False Then
Onde = " Where Comite_Competencia ='" & cmbCompetencia.Text & "'"
Cond = True
Else
Onde = Onde & " AND Comite_Competencia ='" & cmbCompetencia.Text & "'"
End If
End If

If optTrabalho.Value = True Then
If Cond = False Then
Onde = " Where Tipo_Incidente ='" & optTrabalho.Caption & "' "
Cond = True
Else
Onde = Onde & " AND Tipo_Incidente ='" & optTrabalho.Caption & "' "
End If
End If
If optAmbiental.Value = True Then
If Cond = False Then
Onde = " Where Tipo_Incidente ='" & optAmbiental.Caption & "'"
Cond = True
Else
Onde = Onde & " AND Tipo_Incidente ='" & optAmbiental.Caption & "'"
End If
End If

SQL = SQL & Onde
Dim RC As Recordset

Set RC = Bd.OpenRecordset(SQL, dbOpenSnapshot)

If RC.RecordCount = 0 Then
MsgBox "Não foi localizado nenhum registro que atenda ao critério da pesquisa!!!!", vbInformation, "Aviso"
cmdCancelar_Click
Exit Sub
End If

Do While Not RC.EOF
If RC("ID") <> IDAnt Then
RC11.AddNew
RC11("ID") = RC("ID")
RC11("Data") = RC("Data")
RC11("Hora") = RC("Hora")
RC11("Relator") = RC("Relator")
RC11("Origem") = RC("Origem")
RC11("Empresa_Relatante") = RC("Empresa_Relatante")
RC11("Comite_Relatante") = RC("Comite_Relatante")
RC11("Empresa_Geradora") = RC("Empresa_Geradora")
RC11("Local_Incidente") = RC("Local_Incidente")
RC11("Tipo_Incidente") = RC("Tipo_Incidente")
RC11("Descricao_Incidente") = RC("Descricao_Incidente")
RC11("Acao_Imediata") = RC("Acao_Imediata")
RC11("Comite_Competencia") = RC("Comite_Competencia")
RC11("Causa_Incidente") = RC("Causa_Incidente")
RC11("Status") = RC("Status")
RC11("Previsao") = RC("Previsao")
RC11("Conclusao") = RC("Conclusao")
RC11("T_Conclusao") = RC("T_Conclusao")
RC11("Responsavel") = RC("Responsavel")
RC11("Tipo_Relato") = RC("Tipo_Relato")
RC11("Risco") = RC("Risco")
RC11("Empresa") = RC("Empresa")
RC11("Forma") = RC("Forma")
RC11.Update
End If
IDAnt = RC("ID")
RC.MoveNext
Loop
RC11.Close

Call Limpa_Melhoria
Call Calcula_Resultado
Call Carrega_Valores

End Sub

Tópico encerrado , respostas não são mais permitidas