20. juli 2001 - 12:48Der er
11 kommentarer og 1 løsning
søgning
hej
jeg har en access db som efterhånden har en del indhold. Nu vil jeg så gerne kunne søge i den ud fra nogle kriterier. Det skal fungere på denne måde man indtaster f.eks. 5 søgeord og så skal der komme en liste over alle de værdier som opfylder de søgekriterier.
Er der nogen som kan hjælpe med det ?
Jeg kan ikke ¨ligge db\'en op så i kan hente den da der er information i som ikke må videregives
Do you have 13 fields in one table in the database. For each field do you want to be able to enter one seach criteria for each field in the table or do you want to be able to have more than one criteria for each field in the table?
You have a form which shows ALL records from your table/query. (One detail per record).
In the form header place the fields (text combo boxes etc.) where you enter your search criteria.
Place a button in the form footer. In the buttons on click event add and modify the following code.
Dim sFilter As String
sFilter = \"1 \"
If Not fldIsEmpty(Me.fld1) Then sFilter = sFilter & \" AND dBfield1 = \" & Me.fld1 End If
If Not fldIsEmpty(Me.fld2) Then sFilter = sFilter & \" AND dBfield2 = \" & Me.fld2
End If
Me.Filter = sFilter Me.FilterOn = True Me.Refresh
and add the following function to a module.
Function fldIsEmpty(fld As Variant)
If IsNull(fld) Or fld = \"\" Or IsNull(Len(fld)) Then fldIsEmpty = True Else fldIsEmpty = False End If
End Function
Explanation: In this example there is only one search criteria per field in the database. The line sFilter = \"1 \" is the same as \"WHERE 1\" in an SQL statement, in other words everything is selected. By using this I dont need to test if I have to append an AND to the next field in the SQL.
Then I test to see if a criteria is entered if not then go to the next field. If I had 13 fields then I would have 13 If statements, one for each field.
This should give you some idea as to how it CAN be done.
If you want to have more than one search criteria per field in the database then things start getting complicated! This is because we then have to start checking if none, one or more than one of the criteria for a specific dB field have been enterd. This may result in having to append an OR in the filter.
Finally when the filter is buildt I switch the filter on (me.filterOn = True) to show just those records I am interested in.
You could have another button (or a toggle button) to switch the filter off again.
Me.fld1 and Me.fld2 are the names of the search criteria fields on the header. dBfield1 and dBfield2 are the actual fields your search on in the database table/query
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.