I had this problem before, when the query is read only the code does not work.I'm stumped, maybe someone else has an idea. Can you attach the db here to play with?
Hi. Pardon me for jumping in, but is this related to your other thread?I had this problem before, when the query is read only the code does not work.
As soon as I remove the subquery everything works as it should.
...
ErrHandler:
If Err.Number <> 2185 Then
MsgBox Err.Description, vbExclamation
End If
End Sub
With this code you can get a very frustrating and difficult to resolve error. When you change the textbox and filter the form even though you are typing in the textbox, if no records are returned the control loses focus. Seems impossible, but it does. Even if you try to set focus you cannot and thus the error.
There are a couple work arounds Depending on how the form is set up. Can you give a screen shot?
Thanks for this. You said a couple, though. What was the other one? Just curious...I do not fully understand this, not sure if it is a bug. But it is the most confusing error.
One simple work around is to make your form as a main form with a subform. You can make that look the same as a Header, detail section.
The way I do it in my Find as you type form is check the record count after the change event. If say the user Types Bal then z and there is no Balz* I do not try to set the sel or read the .text. I pop up a message "No records with Balz" and then roll it back to "bal".
That was twoThanks for this. You said a couple, though. What was the other one?
Private Sub txtContactFilter_Change()
Dim sText As String
Dim strFilter As String
On Error GoTo ErrHandler
sText = Me!txtContactFilter.Text
If sText <> "" Then
strFilter = "[Contact] Like '*" & sText & "*'"
Me.Filter = strFilter
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
With Me.txtContactFilter
.SetFocus
.Value = sText
.SelStart = Len(sText)
.SelLength = 0
End With
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
End Sub
Excellent! Thanks for the clarification. Cheers!That was two
1. Instead of the search box in the header with a detail section, make it a main form with a tabular subform. For some reason this will work. You can format it to look almost the same.
2. Check that records are returned by the search. If not roll the search back.. If no records returned by BALZ strip it back to BAL which will return records. Do not try to search for BALZW
You can try to set the focus before calling the .text method, and it will still tell you that you have to set the focus. It is as if you cannot set the focus if no records exist in the detail section. However, that is not completely true which makes it more confusing.
Code:Private Sub txtContactFilter_Change() Dim sText As String Dim strFilter As String On Error GoTo ErrHandler sText = Me!txtContactFilter.Text If sText <> "" Then strFilter = "[Contact] Like '*" & sText & "*'" Me.Filter = strFilter Me.FilterOn = True Else Me.Filter = "" Me.FilterOn = False End If With Me.txtContactFilter .SetFocus .Value = sText .SelStart = Len(sText) .SelLength = 0 End With Exit Sub ErrHandler: MsgBox Err.Description, vbExclamation End Sub
assume it fails at the line telling you that you must set the focus to txtContactFilter
sText = Me!txtContactFilter.Text
You can add this before
me.TxtContactFilter.Setfocus
msgbox me.activeControl
You have set the focus again and it will show that txtcontactfilter is the activecontrol. Which means it has focus. Still the next line will fail saying it needs the focus.
So this code will fail when no records are returned, and I am guessing it depends if the query is editable or not.
mTextBox.SetFocus
If Not Trim(mTextBox.Text & " ") = "" Then
mTextBox.Value = mTextBox.Text
mForm.Filter = getFilter(mTextBox.Text)
mForm.FilterOn = True
If mForm.Recordset.RecordCount = 0 Then
MsgBox "No items matched filter " & vbCrLf & mForm.Filter, vbInformation, "No Items Found"
mForm.FilterOn = False 'needed to set focus on textbox
DoEvents
mTextBox.SetFocus
mTextBox.Value = Left(mTextBox.Text, Len(mTextBox.Text) - 1)
FilterForm
End If
Else
Call unFilterForm
End If
Yes I had a similar problem before just getting it to work, so when I went to find that thread, I accidently posted their.Hi. Pardon me for jumping in, but is this related to your other thread?
(1) Please help with code and ability to edit records on form | Access World Forums (access-programmers.co.uk)
not sure how to post a screen shot.With this code you can get a very frustrating and difficult to resolve error. When you change the textbox and filter the form even though you are typing in the textbox, if no records are returned the control loses focus. Seems impossible, but it does. Even if you try to set focus you cannot and thus the error.
There are a couple work arounds Depending on how the form is set up. Can you give a screen shot?
With this code you can get a very frustrating and difficult to resolve error. When you change the textbox and filter the form even though you are typing in the textbox, if no records are returned the control loses focus. Seems impossible, but it does. Even if you try to set focus you cannot and thus the error.
There are a couple work arounds Depending on how the form is set up. Can you give a screen shot?
I am still a little new to all of this, are you saying that your code here will replace my code (with the correct field names of course)?Code:mTextBox.SetFocus If Not Trim(mTextBox.Text & " ") = "" Then mTextBox.Value = mTextBox.Text mForm.Filter = getFilter(mTextBox.Text) mForm.FilterOn = True If mForm.Recordset.RecordCount = 0 Then MsgBox "No items matched filter " & vbCrLf & mForm.Filter, vbInformation, "No Items Found" mForm.FilterOn = False 'needed to set focus on textbox DoEvents mTextBox.SetFocus mTextBox.Value = Left(mTextBox.Text, Len(mTextBox.Text) - 1) FilterForm End If Else Call unFilterForm End If
Private Sub txtNumberFilter_Change()
Dim sText As String
Dim strFilter As String
' On Error GoTo ErrHandler
sText = Me!txtNumberFilter.Text
If sText <> "" Then
strFilter = "[OrderNumber] Like '*" & sText & "*'"
Me.Filter = strFilter
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
If Me.Recordset.RecordCount = 0 Then
MsgBox "No records match " & Me.Filter, vbInformation, "No Match"
Me.FilterOn = False
DoEvents
Me.txtNumberFilter.SetFocus
sText = Left(Me.txtNumberFilter.Text, Len(txtNumberFilter.Text) - 1)
Me.txtNumberFilter.Value = sText
Me.Filter = "[OrderNumber] Like '*" & sText & "*'"
Me.FilterOn = True
End If
With Me.txtNumberFilter
.SetFocus
.Value = sText
.SetFocus
.SelStart = Len(sText)
.SelLength = 0
End With
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
End Sub
When you say write one procedure , are you referring to adding the functions and subs you are calling for in the previous code you posted?Personally I would write one procedure so you do not have to update each and every one. But if you want to try one. Try this
Code:Private Sub txtNumberFilter_Change() Dim sText As String Dim strFilter As String ' On Error GoTo ErrHandler sText = Me!txtNumberFilter.Text If sText <> "" Then strFilter = "[OrderNumber] Like '*" & sText & "*'" Me.Filter = strFilter Me.FilterOn = True Else Me.Filter = "" Me.FilterOn = False End If If Me.Recordset.RecordCount = 0 Then MsgBox "No records match " & Me.Filter, vbInformation, "No Match" Me.FilterOn = False DoEvents Me.txtNumberFilter.SetFocus sText = Left(Me.txtNumberFilter.Text, Len(txtNumberFilter.Text) - 1) Me.txtNumberFilter.Value = sText Me.Filter = "[OrderNumber] Like '*" & sText & "*'" Me.FilterOn = True End If With Me.txtNumberFilter .SetFocus .Value = sText .SetFocus .SelStart = Len(sText) .SelLength = 0 End With Exit Sub ErrHandler: MsgBox Err.Description, vbExclamation End Sub
Private Sub txtNumberFilter_Change()
Dim sText As String
Dim strFilter As String
' On Error GoTo ErrHandler
sText = Me!txtNumberFilter.Text
If sText <> "" Then
strFilter = "[OrderNumber] Like '*" & sText & "*'"
Me.Filter = strFilter
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
If Me.Recordset.RecordCount = 0 Then
MsgBox "No records match " & Me.Filter, vbInformation, "No Match"
Me.FilterOn = False
DoEvents
Me.txtNumberFilter.SetFocus
sText = Left(Me.txtNumberFilter.Text, Len(txtNumberFilter.Text) - 1)
Me.txtNumberFilter.Value = sText
Me.Filter = "[OrderNumber] Like '*" & sText & "*'"
Me.FilterOn = True
End If
With Me.txtNumberFilter
.SetFocus
.Value = sText
.SetFocus
.SelStart = Len(sText)
.SelLength = 0
End With
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
End Sub
Public Sub FilterByControl(ctrl as access.control, filterField as string)
Dim sText As String
Dim strFilter As String
' On Error GoTo ErrHandler
sText = ctrl.text
If sText <> "" Then
strFilter = "[" & FilterField & "] Like '*" & sText & "*'"
Me.Filter = strFilter
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
If Me.Recordset.RecordCount = 0 Then
MsgBox "No records match " & Me.Filter, vbInformation, "No Match"
Me.FilterOn = False
DoEvents
ctrl.SetFocus
sText = Left(ctrl.Text, Len(ctrl.Text) - 1)
ctrl.Value = sText
Me.Filter = "[" & FilterField & "] Like '*" & sText & "*'"
Me.FilterOn = True
End If
With ctrl
.SetFocus
.Value = sText
.SetFocus
.SelStart = Len(sText)
.SelLength = 0
End With
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
End Sub
Private Sub txtNumberFilter_Change()
FilterByControl me.txtNumberFilter, "OrderNumber"
end sub