---
title: "Sida som tar emot sökning från flera fält?"
type: "forum-thread"
url: "https://www.webforum.nu/amne/asp/6064-sida-som-tar-emot-sökning-från-flera-fält"
topic: "ASP"
topic_url: "https://www.webforum.nu/amne/asp"
author: "brw"
published: "2000-07-17T06:07:00.000Z"
updated: "2000-07-17T07:25:00.000Z"
replies: 6
views: 331
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/asp/6064-sida-som-tar-emot-sökning-från-flera-fält"
---

# Sida som tar emot sökning från flera fält?

## #1 — brw, 2000-07-17T06:07Z

Jag har gjort ett formulär där man skall kunna söka på 3 fält (companycode, datum till datum)
men jag vet inte hur jag ska formulera sqlsträngen som tar emot och kollar allt så att jag kan sortera det.

Companycode är alltså specifikt per företag så om jag söker på companycode: 145664 och 1999-12-31 till 2000-06-12
så skall den visa endast de poster som är reggade mellan dessa datum och som tillhör detta companycodenummer.

**formular.asp**

```
<form name="ccdate" method="post" action="sereceiverdisp.asp">
Companycode: <input type="text" name="companycode"><br>
Between date: <input type="text" name="date1"> and <input type="text" name="date2">
              <input type="submit" value="Search" name="submit">
            </form>
```

**sereceiverdisp.asp**

```
<%
companycode=Request.Form("companycode")
date1=Request.Form("date1")
date2=Request.Form("date2")

If len(trim(companycode))> 0 then
	companycode= "%" & companycode & "%"
End If

If len(trim(date1))> 0 then
	date1= "%" & date1 & "%"
End If

If len(trim(date2))> 0 then
	date2= "%" & date2 & "%"
End If

	Set Connection = Server.CreateObject("ADODB.Connection")
	Connection.Open "driver={Microsoft Access Driver (*.mdb)};dbq=c:\databaser\rec.mdb"

If len(trim(companycode))> 0 then
	SQLStmt = "SELECT * FROM recsys WHERE companycode LIKE '" & companycode & "' ORDER BY receiver"
If len(trim(date1)) and len(trim(date2)) > 0 then
	SQLStmt = "SELECT * FROM recsys WHERE datereg BETWEEN date1 AND date2 order by datereg DESC"
End If

Set RS = Connection.Execute(SQLStmt)
%>
```

Kan nån hjälpa mig med koden som tar emot från formuläret? 

/brw

Permalänk: https://www.webforum.nu/p/6064

## #2 — Vide, 2000-07-17T06:21Z

Det du kan göra är såhär:

```
If len(trim(companycode))> 0 then
SQLStmt = "SELECT * FROM recsys WHERE companycode LIKE '" & companycode & "'"
If len(trim(date1)) and len(trim(date2)) > 0 then
SQLStmt = SQLStmt & "AND (datereg BETWEEN date1 AND date2) order by datereg DESC"
Else
SQLStmt = SQLStmt & " ORDER BY receiver"
End If
```

Är dina datumfält av datatypen datum, så skall du ha # som omfamnar värdet... och du måste nog ha datatypen datum om du skall jämföra två datum...

```
If len(trim(date1))> 0 then
date1= "#" & date1 & "#"
End If

If len(trim(date2))> 0 then
date2= "#" & date2 & "#"
End If
```

Och skall du verkligen ha wildcards för företagskoden? det finns väl bara en specifik för varje... eller skall man kunna hämta ur flera företag? om inte:

```
If len(trim(companycode))> 0 then
companycode = companycode
End If
```

Alltså så skulle du kunna ta bort dom raderna...

Lycka till! :)

Permalänk: https://www.webforum.nu/p/244474

## #3 — brw, 2000-07-17T07:06Z

Tack för koden!

Det fungerar när jag söker på bara companycode... men söker jag på companycode, date1 och date2 samtidigt med värden som jag vet finns inlagda så får jag följande fel:

Microsoft OLE DB Provider for ODBC Drivers fel '80040e10' 

\[Microsoft\]\[Drivrutin för ODBC Microsoft Access 97\] För få parametrar angavs. 2 förväntades

Vad är fel?

Koden ser ut enligt följande nu:

```
companycode=Request.Form("companycode")
date1=Request.Form("date1")
date2=Request.Form("date2")

If len(trim(companycode))> 0 then
companycode = companycode
End If

If len(trim(date1))> 0 then
date1 = "#" & date1 & "#"
End If

If len(trim(date2))> 0 then
date2 = "#" & date2 & "#"
End If

	Set Connection = Server.CreateObject("ADODB.Connection")
	Connection.Open "driver={Microsoft Access Driver (*.mdb)};dbq=c:\databaser\login.mdb"

If len(trim(companycode))> 0 then
SQLStmt = "SELECT * FROM recsys WHERE companycode LIKE '" & companycode & "'"
If len(trim(date1)) and len(trim(date2)) > 0 then
SQLStmt = SQLStmt & "AND (datereg BETWEEN date1 AND date2) order by datereg DESC"
Else
SQLStmt = SQLStmt & "ORDER BY receiver"
End If

Set RS = Connection.Execute(SQLStmt)
```

/brw

Permalänk: https://www.webforum.nu/p/244475

## #4 — Vide, 2000-07-17T07:13Z

date1 och date2 är ju variabler...

```
If len(trim(companycode))> 0 then
SQLStmt = "SELECT * FROM recsys WHERE companycode LIKE '" & companycode & "'"
If len(trim(date1)) and len(trim(date2)) > 0 then
SQLStmt = SQLStmt & "AND (datereg BETWEEN " & date1 & " AND " & date2 & ") order by datereg DESC"
Else
SQLStmt = SQLStmt & "ORDER BY receiver"
End If
```

Annars så testa att skriva ut SQLsatsen så ser man lättare vad som är fel...

Byt
Set RS = Connection.Execute(SQLStmt)
Mot
Response.write SQLStmt

Det kommer inte att fungera, men du kommer att få en textsträng som du kan kolla igenom efter fel...

Permalänk: https://www.webforum.nu/p/244476

## #5 — brw, 2000-07-17T07:18Z

Då får jag:

SELECT \* FROM recsys WHERE companycode LIKE '894452'AND (datereg BETWEEN date1 AND date2) order by datereg DESC 

verkar som om den inte tar datum värdena?

och sen

Körningsfel i Microsoft VBScript fel '800a01a8' 
Objekt krävs.: '' 

/recsys/sereceiverdisp.asp, rad 123 

och rad 123 är där jag börjar rabbla upp dem

\<%
Do Until RS.EOF    
counter = counter + 1
If counter Mod 2 = 0 then
 color = "#CCCCCC"
Else
 color = "#ffffff"
End If
%\>

Permalänk: https://www.webforum.nu/p/244477

## #6 — Vide, 2000-07-17T07:22Z

OK! Ersätt med koden nedan... och byt tillbaks till 'Set RS...'

```
If len(trim(companycode))> 0 then
SQLStmt = "SELECT * FROM recsys WHERE companycode LIKE '" & companycode & "'"
If len(trim(date1)) and len(trim(date2)) > 0 then
SQLStmt = SQLStmt & " AND (datereg BETWEEN " & date1 & " AND " & date2 & ") order by datereg DESC"
Else
SQLStmt = SQLStmt & "ORDER BY receiver"
End If
```

Permalänk: https://www.webforum.nu/p/244478

## #7 — brw, 2000-07-17T07:25Z

Hehe funkar fint som fan nu!

Tackar! =))

Permalänk: https://www.webforum.nu/p/244479

---

Tråden på webben: https://www.webforum.nu/amne/asp/6064-sida-som-tar-emot-sökning-från-flera-fält
