Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá, estou precisando de uma ajuda. Estou trabalhando em um código que faça uma busca em um banco de dados Access, e que busque em duas tabelas diferentes: artigos e noticias. A busca funciona quando o termo procurado se encontra em algum dos campos das duas tabelas simultaneamente, ou se o termo procurado não estiver em nenhuma das duas. Porém quando preciso procurar algo que esteja apenas em artigos, ou apenas em noticias ele dá erro.
O que eu fiz foi apenas replicar o código e variáveis e aplicá-lo para a outra tabela, este é o código:
>
<%
Dim cnnSearchAll
Dim cnnSearchAll2
Dim strDBPathAll
Dim strDBPathAll2
Dim rstSearch
Dim rstSearch02
Dim strSQL
Dim strSQL2
Dim strSearch
strSearch = Request.QueryString("busca")
%>
<form action="busca.asp" method="get">
<input name="busca" value="<%= strSearch %>" />
<input type="submit" />
</form>
<%
If strSearch <> "" Then
'Busca Artigos
strDBPathAll = Server.MapPath("database.mdb")
Set cnnSearchAll = Server.CreateObject("ADODB.Connection")
cnnSearchAll.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPathAll & ";"
strSQL = "SELECT codigo, titulo, artigo " _
& "FROM **artigos** " _
& "WHERE titulo LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "OR artigo LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "ORDER BY titulo ASC"
Set rstSearch = cnnSearchAll.Execute(strSQL)
Dim i
i = 0
response.write "Artigos"
If rstSearch.EOF AND rstSearch.BOF Then
response.write "Nenhum resultado em Artigos"
Else
Do While i <= 3
response.write "<a href=""artigo.asp?codigo=" & rstSearch.Fields("codigo").Value & """>" & rstSearch.Fields("titulo").Value & "</a><br />"
i = i + 1
rstSearch.MoveNext
Loop
i = 0
response.write "<br /><br />"
End If
rstSearch.Close
Set rstSearch = Nothing
cnnSearchAll.Close
Set cnnSearchAll = Nothing
'Busca Notícias
strDBPathAll2 = Server.MapPath("database.mdb")
Set cnnSearchAll2 = Server.CreateObject("ADODB.Connection")
cnnSearchAll2.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPathAll2 & ";"
strSQL2 = "SELECT codigo, titulo, noticia " _
& "FROM **noticias** " _
& "WHERE artigo LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "OR noticia LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "ORDER BY codigo DESC"
Set rstSearch02 = cnnSearchAll2.Execute(strSQL2)
response.write "Notícias"
If rstSearch02.EOF AND rstSearch02.BOF Then
response.write "Nenhum resultado em Noticias"
Else
Do While i <= 3
response.write "<a href=""noticia.asp?codigo=" & rstSearch02.Fields("codigo").Value & """>" & rstSearch02.Fields("titulo").Value & "</a><br />"
i = i + 1
rstSearch02.MoveNext
Loop
i = 0
End If
rstSearch02.Close
Set rstSearch02 = Nothing
cnnSearchAll2.Close
Set cnnSearchAll2 = Nothing
End If
%>
Se alguém pudesse me ajudar ficaria muito grato.Valeu, abraço.
Carregando comentários...