Need correction in Very Short search code

rehanemis

Registered User.
Local time
Today, 21:44
Joined
Apr 7, 2014
Messages
195
Hi Professionals,


I have copied code from http://blogs.office.com/2012/05/03/using-a-combo-box-to-search-as-you-type/ and tried it to my Access program but not working as they mentioned. Can any body tell what is the wrong into the code and can give me access file after implementing it for demo.

Then in the Change event of the Combo Box, we add the following code:
Private Sub cboFilter_Change()​
‘ If the combo box is cleared, clear the form filter.
If Nz(Me.cboFilter.Text) = “” Then
Me.Form.Filter = “”
Me.FilterOn = False

‘ If a combo box item is selected, filter for an exact match.
‘ Use the ListIndex property to check if the value is an item in the list.

ElseIf Me.cboFilter.ListIndex <> -1 Then
Me.Form.Filter = “[Company] = ‘” & _
Replace(Me.cboFilter.Text, “‘”, “””) & “‘”
Me.FilterOn = True

‘ If a partial value is typed, filter for a partial company name match.
Else
Me.Form.Filter = “[Company] Like ‘*” & _
Replace(Me.cboFilter.Text, “‘”, “””) & “*'”
Me.FilterOn = True
End If

‘ Move the cursor to the end of the combo box.
Me.cboFilter.SetFocus
Me.cboFilter.SelStart = Len(Me.cboFilter.Text)
End Sub


Your help will be highly appreciated.


Thanks
 
Do you get any error or what is the problem?
One thing I can see which isn't correct is the “” it should be " and the same for it should be '. try to replace all the above mention character!
 
Thanks for your reply.

Can you please give a look at the DB i have attached and can correct it.

Thanks
 

Attachments

Replace the code you've with the below:

Code:
Private Sub cboSearch_Change()
If Nz(Me.cboSearch.Text) = "" Then
    Me.Form.Filter = ""
    Me.FilterOn = False
    
ElseIf Me.cboSearch.ListIndex <> -1 Then
  Me.Form.Filter = "[FirstName] ='" & Me.cboSearch.Text & "'"
  Me.FilterOn = True
ElseIf Me.cboSearch.ListIndex <> -1 Then
   Me.Form.Filter = "[FirstName] = '" & Replace(Me.cboSearch.Text, "'", """")
   Me.FilterOn = True
Else
    Me.Form.Filter = "[FirstName] Like '*" & Me.cboSearch.Text & "*'"
    Me.FilterOn = True
End If
Me.cboSearch.SetFocus
Me.cboSearch.SelStart = Len(Me.cboSearch.Text)
End Sub
 
Hi,

One More thing, Can you add comments into your code, what changes you made so that i can understand the working of whole code.

Thanks
 
Sorry, not following this thread, just highlighting two little lines for JHB to correct.
Code:
[COLOR="red"]ElseIf Me.cboSearch.ListIndex <> -1 Then[/COLOR]
  Me.Form.Filter = "[FirstName] ='" & Me.cboSearch.Text & "'"
  Me.FilterOn = True
[COLOR="Red"]ElseIf Me.cboSearch.ListIndex <> -1 Then[/COLOR]
   Me.Form.Filter = "[FirstName] = '" & Replace(Me.cboSearch.Text, "'", """")
   Me.FilterOn = True
 
Hi,

One More thing, Can you add comments into your code, what changes you made so that i can understand the working of whole code.

Thanks
Compare it to the code you have in the database you attached.
Take advance of what vbaInet wrote, you've two with the same if statement.
 

Users who are viewing this thread

Back
Top Bottom