Question Locked

Access Memo Field Search Using VB code

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi guys out there,
There is this database Iam creating, I have a table with 40 fields among which there are Date/time, Text, Number, Memo and Yes/No fields and I have created the form bound to that tabe. I have also created Unbound form (for searching purpose) which is similar in layout with the bound one; in this search form the user will fill-in the keywords in as many text boxes as he wish to narrow down the search. The search results are displayed in the bound form (in a splitform view).
I have tried number of vb source codes I gethered from various sources from the internet they work well with text fields and other but they do not work with MEMO fields. The search on the MEMO field just returns nothing.
If anyone out there has a solution to this problem I will appreciate the assistance to make this project work.
The algorithm is: the code searches only the textboxes filled and discards the empty ones this is done after the user presses the search button. (Note: the fiels bears the same name in database table and Search Form)
The sample code is as follows:

Dim strWhere As String 'The criteria string.
Dim lngLen As Long 'Length of the criteria string to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.
If Not IsNull(Me.txtCode) Then
strWhere = strWhere & "([txtRefCode] Like ""*" & Me.txtRefCode & """) AND "
End If
'The next code is for memo field (which fails to work)
If Not IsNull(Me.txtDescription) Then
strWhere = strWhere & "([txtDescription] Like ""*" & Me.txtDescription & """) AND "
End If
'Date field example next. It worked, but Iwasnt satisfied because I wanted the user to search for an exact date (which worked fine) or the operator and a date eg. > 2/27/2008 (which wont work with this code; suggestions requested.
If Not IsNull(Me.txtDateReported) Then
strWhere = strWhere & "([txtDateReported] = " & Format(Me.txtDateReported, conJetDate) & ") AND "
End If
'The code goes on for all 40 fields then finally:
'See if the string has more than 5 characters (a trailng " AND ") to remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Where there was nothing in the string.
MsgBox "No criteria", vbOKOnly, "No Data Intered."
Else 'There is something there, so remove the " AND " at the end.
DoCmd.OpenForm "frmSearchResults"
DoCmd.Maximize
strWhere = Left$(strWhere, lngLen)
'Finally, apply the string as the form's Filter.
Forms!frmSearchResults.Filter = strWhere
Forms!frmSearchResults.FilterOn = True
End If

I came accross this code in the net and it was very helpful exept for the memo fields and date (which I need to use operators like >,< and <>.)

I am stuck here and desperate in need of resque. Anyone out there?
Thnx in advance

6 replies

by tony hopkinson

Like ""*" & Me.txtDescription & "*"")

The one you have would return everything ending in Me.txtDescription, whereas bracketting with * will return return any where me.txtdescription is in the data somewhere.

You should have the same problem with straight text fields as well.

There's a confusion of terms here, this might help

http://sqlserver2000.databases.aspfaq.com/what-are-the-main-differences-between-access-and-sql-server.html

Got to bear in mind access is a horible bastardisation of the standard.

Oh and doing like on text/memo fields is hideously slow and access has no useful facilities to speed it up.

by mirajik

Thanks the suggestion worked fine.
I still have a problem with date search, the code I am using only returns the records with exact date as the date entered in the search box, I want the user to have option either to enter exact date or to search date ranges, say using operators like <,> or <> with.

by tony hopkinson

between for ranges.

Dates you have to be careful with.
If the field is a datetime

then 15/4/2008 <> 15/4/2008 14:53:00

There are several ways you can work yoyur way round this , but they can get expensive.

DatePart(adate, year) = DatePart(bdate,year)

access has shortcuts year, month and day I seem to remember

Another trick is to convert the date to a particular format and compare that e.g.

Look up the SQL function Convert for that

Hths

by andoniss72

i need your help
i am working on access and i want a vb code to search in form using 2 criteria example: first name and last name fields
and the result must get 1st name , last name , father name, mother name, address, phone,,,,
please send me the code by email to ; rnajdi@hotmail.com

best regards

This discussion is closed to new replies.