Creating a search form (having problems)

Zorkmid

Registered User.
Local time
Today, 07:37
Joined
Mar 3, 2009
Messages
188
Hi there,

I am very new to MS Access and to VBA. I am developing a database to keep track of adverse incidents in the workplace. I have a form that adds them to the database that works just great, but now I want to build a form that lets users look-up and update (for follow-up purposes) existing records. I found an example that seems to apply on:
http://allenbrowne.com/ser-62.html

I would like to modify this a little bit. I have pulled a query including all fields into the form, and added controls to enter "Incident number, Type of incident, a range of dates, and the name of the people that it affected". Correct me if I am wrong, but I think this constitutes a subform. Problem is that with the code I have now, no filtering seems to take place, and my command button just returns ALL records. H

Here is the code:

Code:
Private Sub cmdSearch_Click()
    
  Dim strWhere As String
  Dim lngLen As Long
  Const conJetDate = "\mm\/dd\/yyyy\#"
    
    '******************************************************************************************
    'look at each search box and build up the criteria string from all non-blank search boxes.
    '******************************************************************************************
        'ENCON number search
    If Not IsNull(Me.cboENCON) Then
        strWhere = strWhere & "([ENCON] = """ & Me.cboENCON & ") AND "
    End If
    
        'Search by name
    If Not IsNull(Me.txtName) Then
        strWhere = strWhere & "([Name] = Like ""*" & Me.txtName & "*"") AND "
    End If
        
        'Search by type of incident
    If Not IsNull(Me.cboType) Then
        strWhere = strWhere & "([Type] = """ & Me.cboType & ") AND "
    End If
    
        'Search by Resolved or Pending incidents
    If Me.cboResolved = 1 Then
        strWhere = strWhere & "([Resolved or Pending] = True) AND "
       ElseIf Me.cboResolved = 2 Then
        strWhere = strWhere & "([Resolved or Pending] = False) AND "
    End If
    
        'Search within dates
    If Not IsNull(Me.txtStartDate) Then
        strWhere = strWhere & "([Date] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
    End If
    
    If Not IsNull(Me.txtEndDate) Then
        stWhere = strWhere & "([Date] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
    End If
    
    '**********************************************************************************************
    'Chop off the trailing AND and use the resulting string as the filter for the form
    '**********************************************************************************************
    
    lngLen = Len(strWhere) - 5
    
    If lngLen <= 0 Then   'Was nothing in the string.
        MsgBox "Please select at least one criterion.", vbInformation, "Nothing to do."
    Else
        strWhere = Left$(strWhere, lngLen)
        Me.Filter = strWhere
        Me.FilterOn = True
        Risk_Data_Query_subform.Visible = True
    End If
    
  
    
    
End Sub

I am not sure where the problem might lay, and I would love some expert help.

-Thanks very much, Z
 
Code:
Else
        strWhere = Left$(strWhere, lngLen)
        [COLOR=red]Me.Filter = strWhere
        Me.FilterOn = True
[/COLOR]        Risk_Data_Query_subform.Visible = True

The filter property you are setting is on the main form and not your subform.

You could try:

Code:
Else
        strWhere = Left$(strWhere, lngLen)
        Me.[COLOR=red]Risk_Data_Query_subform.[/COLOR]Filter = strWhere
        Me.[COLOR=#ff0000]Risk_Data_Query_subform.[/COLOR]FilterOn = True
        Risk_Data_Query_subform.Visible = True

Here is a link for refrencing forms and subforms:
http://www.mvps.org/access/forms/frm0031.htm

JR
 
Also bear in mind the Name, Date and Type are ALL reserved words and should not be used as field names. Access cannot always recognise them correctly as you would expect. Shange them now.
 
Edit correct syntax for filterproperty is:

Code:
strWhere = Left$(strWhere, lngLen)
        Me.[COLOR=red]Risk_Data_Query_subform.[/COLOR][COLOR=red]Form.[/COLOR]Filter = strWhere
        Me.[COLOR=#ff0000]Risk_Data_Query_subform.Form.[/COLOR]FilterOn = True
        Risk_Data_Query_subform.Visible = True

JR
 
Code:
Else
        strWhere = Left$(strWhere, lngLen)
        [COLOR=red]Me.Filter = strWhere[/COLOR]
[COLOR=red]       Me.FilterOn = True[/COLOR]
        Risk_Data_Query_subform.Visible = True

The filter property you are setting is on the main form and not your subform.

You could try:

Code:
Else
        strWhere = Left$(strWhere, lngLen)
        Me.[COLOR=red]Risk_Data_Query_subform.[/COLOR]Filter = strWhere
        Me.[COLOR=#ff0000]Risk_Data_Query_subform.[/COLOR]FilterOn = True
        Risk_Data_Query_subform.Visible = True
[/quote]
 
When I try this I get a "Compile error, Method or Data Member not found" that seems to highlight the first "filter" bit of code.
 
-Z
 
Edit correct syntax for filterproperty is:

Code:
strWhere = Left$(strWhere, lngLen)
        Me.[COLOR=red]Risk_Data_Query_subform.[/COLOR][COLOR=red]Form.[/COLOR]Filter = strWhere
        Me.[COLOR=#ff0000]Risk_Data_Query_subform.Form.[/COLOR]FilterOn = True
        Risk_Data_Query_subform.Visible = True

JR

Saw the edit after my previous post, with this code the error I get is "Runtime error 2448, you can't assign a value to this object."

edit: No worries about the typo, helps me to learn :)
 
That was wierd! I tested it with this code:

Code:
Private Sub cmbclick_Click()
    Me.Payment_Sheet.Form.Filter = "id =" & Me.txtfilter
    Me.Payment_Sheet.Form.FilterOn = True
    
End Sub

and it did filter my test subform.

Perhaps it's something else going on in the properties of your subform, have you checked for spelling errors. I am curios of why you chose to filer a subform rather than the mainform which is easier.

Maybe someone else has an answer for this runtime error.

JR
 
Im totally open to doing this an easier way. All I need is the structure to search for records that match all criteria, return them in such a way that a couple of the fields are displayed for editing.

If you have a suggestion on the best way to accomplish that, I'd be more than willing to listen and give it a shot.

-Z
 
There is nothing wrong with the subform idea if you got alot of fields to be displayed just make sure your refrencesyntax is correct. If you use your mainform place the display fields in the forms Detail section and use the header section for the searchfields and filter buttons

If you post a samplebase, maybe someone can look at it to check for obvious syntax mistakes.

JR
 
JanR, thanks I guess it's worth a shot. Here is the database trimmed down to 2 records and only the non-archive table.

-Z
 

Attachments

Now when I try it I seem to get the error:

Runtime error '3075'
Syntax error in the string in query expression '([ENCON] = 2012341)'

But I get the same error no matter which criterion I try to search by.

Highlighted in the debugger is this line

Me.Risk_Data_Query_subform.Form.Filter = strWhere


in:
Code:
 lngLen = Len(strWhere) - 5
 
    If lngLen <= 0 Then   'Was nothing in the string.
        MsgBox "Please select at least one criterion.", vbInformation, "Nothing to do."
    Else
        strWhere = Left$(strWhere, lngLen)
        Me.Risk_Data_Query_subform.Form.Filter = strWhere
        Me.Risk_Data_Query_subform.Form.FilterOn = True
        Risk_Data_Query_subform.Visible = True
    End If
 
Change this line:

strWhere = strWhere & "([ENCON] = """ & Me.cboENCON & ") AND "

to this

strWhere = strWhere & "([ENCON] = " & Me.cboENCON & ") AND "
 
You must also change this line:
Code:
strWhere & "([Type] = """ & Me.cboType & ") AND "

To:

Code:
strWhere & "([Type] = """ & Me.cboType & [COLOR=red]""[/COLOR]") AND "

It's because cboType is a textbased combobox.

With Bob's change and mine it should work.

JR :)
 
My Dummy Form at home seems to be working! (There are still a few kinks, some of which you may hear about :-) )


Thank you so very much!!

:D:D:D:D:D
 
Ive worked out some of the kinks, but I can't seem to get the code that search by [Name] with the "like" function to work correct

Code:
'Search by name
    If Not IsNull(Me.txtName) Then
        strWhere = strWhere & "([Name] = Like ""*" & Me.txtName & "*"") AND "
    End If

I also can't seem to get the function that restricts the filter to records between 2 dates.

Code:
'Search within dates
    If Not IsNull(Me.txtStartDate) Then
        strWhere = strWhere & "([Date] >= " & Format(Me.txtStartDate, conJetDate) & ") AND "
    End If
    
    If Not IsNull(Me.txtEndDate) Then
        stWhere = strWhere & "([Date] < " & Format(Me.txtEndDate + 1, conJetDate) & ") AND "
    End If

WHen I search by name or dates, I always get the error:

Run-time error '2448'
You can't assign a value to this object




I'd also like to be able to search for either "resolved" or "unresolved" records which appear in my table under the field "Resolved or Pending" as 1's and 2's

Code:
'Search by Resolved or Pending incidents
    If Me.cboResolved = "Resolved" Then
        strWhere = strWhere & "([Resolved or Pending] = True) AND "
       ElseIf Me.cboResolved = "Unresolved" Then
        strWhere = strWhere & "([Resolved or Pending] = False) AND "
    End If

The filter for this field doesnt work at all, just returns all records.

-Z

P.S. Sorry to bother you gurus again, I have fiddled around quite a bit to no avail. :(

Even if someone could direct me to a good resource where this sort of info is freely avaialble would be super. :)
 
Your first issue:
Code:
strWhere & "([Name] [COLOR=red]=[/COLOR] Like ""*" & Me.txtName & "*"") AND "

Remove the equal sign
Code:
strWhere & "([Name] Like ""*" & Me.txtName & "*"") AND "

3. Issue
I'd also like to be able to search for either "resolved" or "unresolved" records which appear in my table under the field "Resolved or Pending" as 1's and 2's

True is stored as -1 and False as 0 Not 1 and 2

Code:
strWhere = strWhere & "([Resolved or Pending] = [COLOR=red]1[/COLOR]) AND "
       ElseIf Me.cboResolved = "Unresolved" Then
        strWhere = strWhere & "([Resolved or Pending] = [COLOR=red]2[/COLOR]) AND "

Your date issue could be txtStartDate and txtEndDate on your form is not formated as a date field.

Hope this brings you further.

JR
 
3. Issue


True is stored as -1 and False as 0 Not 1 and 2

Code:
strWhere = strWhere & "([Resolved or Pending] = [COLOR=red]1[/COLOR]) AND "
       ElseIf Me.cboResolved = "Unresolved" Then
        strWhere = strWhere & "([Resolved or Pending] = [COLOR=red]2[/COLOR]) AND "

Your date issue could be txtStartDate and txtEndDate on your form is not formated as a date field.

Hope this brings you further.

JR
[/quote]


Thanks for the insights again JANR, my [name] filter works great now. I get a new error when I try the resolved/unresolved filter...

Run time error '2001'
You cancelled the previous operation.
 
I get a new error when I try the resolved/unresolved filter...

Run time error '2001'
You cancelled the previous operation.
That usually comes about if you have a DLookup that returns a Null and is trying to be assigned somewhere - OR it also can happen when you don't have the correct delimiters around some of your data (perhaps your date part of the SQL isn't correct).
 
True is stored as -1 and False as 0 Not 1 and 2

Code:
strWhere = strWhere & "([Resolved or Pending] = [COLOR=red]1[/COLOR]) AND "
       ElseIf Me.cboResolved = "Unresolved" Then
        strWhere = strWhere & "([Resolved or Pending] = [COLOR=red]2[/COLOR]) AND "

Your date issue could be txtStartDate and txtEndDate on your form is not formated as a date field.

Hope this brings you further.

I was able to fix my Resolved/Unresolved filter by changing:
Code:
strWhere = strWhere & "([Resolved or Pending] = [COLOR=red]1[/COLOR]) AND "
       ElseIf Me.cboResolved = "Unresolved" Then
        strWhere = strWhere & "([Resolved or Pending] = [COLOR=red]2[/COLOR]) AND "

To:
Code:
If Me.cboResolved = "Resolved" Then
        strWhere = strWhere & "([Resolved or Pending] = """ & 1 & """) AND "
       ElseIf Me.cboResolved = "Unresolved" Then
        strWhere = strWhere & "([Resolved or Pending] = """ & 2 & """) AND "
    End If

I must say I'm not really 100% sure why this works, but it does :)

I am however still left with my "date range" problem.

-Z
 

Users who are viewing this thread

Back
Top Bottom