Solved

Error Handling

Posted on 2006-11-24
5
274 Views
Last Modified: 2008-02-01
I am filtering data on a form

Using the following code

Private Sub txtMember_Change()

    txtMemCriteria = txtMember.Text & "*"
    Me.RecordSource = "qryLHOFilter"
    DoCmd.GoToControl "txtMember"
    If Not IsNull(Me.txtMember) Then Me.txtMember.SelStart = Len(Me.txtMember)
   
   
End Sub

it is working but when I enter text like x where no records start with x I am getting the following error message

Run Time Error 2185
You can't referenece a property or method for a control unless the
control has the focus.

I want it to give a message there are no records for that selection and change the text criteria back to null
0
Comment
Question by:brogrimes
  • 3
  • 2
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 18008178
If qryLHOFilter's Where clause is based on the text criteria, try using DCount to determine if any records exist before proceding with the rest of your code:

Private Sub txtMember_Change()

    txtMemCriteria = txtMember.Text & "*"

   '** Check for records and exit sub if none arefound
    If DCount("*","qryLHOFilter") = 0 then          
       msgbox "No records found"
       me.txtMember = Null
       me.txtMemCriteria = Null
       exit sub
    end if

    Me.RecordSource = "qryLHOFilter"
    DoCmd.GoToControl "txtMember"
    If Not IsNull(Me.txtMember) Then Me.txtMember.SelStart = Len(Me.txtMember)
   
End Sub
0
 

Author Comment

by:brogrimes
ID: 18008354
Thanks, that is working to some degree, my fault in the explanation

The message is appearing when i enter "ax" but when that happens I want the record sourse to retrive all the records again so the user can start again.

What is happening is when I enter 'a' it is filtering all the a's

When I enter 'x' I get the message, I click OK and the records from the 'a' selection are still there. I changed the code a little for the control to get the focus

Private Sub txtMember_Change()

 txtMemCriteria = txtMember.Text & "*"

   '** Check for records and exit sub if none arefound
    If DCount("*", "qryLHOFilter") = 0 Then
       MsgBox "No records found"
       DoCmd.GoToControl "txtMember"
        If Not IsNull(Me.txtMember) Then Me.txtMember.SelStart = Len(Me.txtMember)
       
       'Me.txtMember = "Null"
       'Me.txtMemCriteria = "Null"
       Exit Sub
    End If

    Me.RecordSource = "qryLHOFilter"
    DoCmd.GoToControl "txtMember"
    If Not IsNull(Me.txtMember) Then Me.txtMember.SelStart = Len(Me.txtMember)

End Sub

This brings me to the control but it does not take out the 'ax' and the cursor appears between the 'a' and the 'x'

Thanks
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 18008977
Is this what you want to do?
- Clear txtMember
- Reset the record source so that all records show (txtMemCriteria = "*")
- ensure that the focus is on txtMember

Can you post the SQL to qryLHOFilter?

Try this:

Private Sub txtMember_Change()

 txtMemCriteria = txtMember.Text & "*"

   '** Check for records and exit sub if none are found
    If DCount("*", "qryLHOFilter") = 0 Then
        MsgBox "No records found"      
         Me.txtMember = ""             '** Clear txtMember
         Me.txtMemCriteria = "*"      '** Use the * wildcard to get all records
          'If Not IsNull(Me.txtMember) Then Me.txtMember.SelStart = Len(Me.txtMember)
          'Exit Sub        '** remove this line to reset filter and set focus              
    End If

    Me.RecordSource = "qryLHOFilter"
    DoCmd.GoToControl "txtMember"
    If  NZ(Me.txtMember,"") <> "" Then Me.txtMember.SelStart = Len(Me.txtMember)    '** Nz will also check for empty strings

End Sub
0
 

Author Comment

by:brogrimes
ID: 18009203
Thanks alot

My application is really starting lo look good, appreciate it.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18009221
Glad I could help ;-)  
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question