Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Why do I get an error in a Text Box's "On Enter" event in an "empty" Access Form?

Posted on 2011-02-22
7
Medium Priority
?
519 Views
Last Modified: 2012-05-11
I am developing an Access 2007 "project" (ADP) as a front-end to a SQL Server 2005 Express database.

I have a "continuous" Access Form that lists the records in a Table before they are printed on an Invoice or Credit Note. These rows of data can be modified or new rows added within the Form.

In the Footer of the Form I have a large Tab Control with several Tab Pages. Each of these Tab Pages contains further Combo controls and Text Box controls that govern what will be printed on the Invoice or Credit Note when it is eventually printed.

Normally this Form displays rows of records in the Detail Section when it is first opened, and I have no problems as I move from Tab Page to Tab Page in the Footer. However, if there are no records displayed to begin with, I get the following run-time error when I open a Tab Page and the focus is placed on a Text Box in the page.

Run-time error 2185
You can't reference a property or method for a control unless the control has the focus.

This happens in Text Boxes that contain messages that might be printed on the Invoice or Credit. It happens in the "On Enter" event procedure where I set ".SelStart" and ".SelLength" to 0, to position the text cursor at the first character of the text rather than selecting all the text.

I do not get this error if the Tab Page's first control is a Combo Box or if it is a Text Box for which I have not established an "On Enter" sub procedure. The cursor seems to sit in these controls OK. If I add a row of data to the Form's Detail section before opening the Tab Pages that have the "On Enter" procedures I do not get the error message.

Can someone explain to me why I get this error message when my Form is empty? I have another text control called ".txtLineCount" that I use to display the number of data lines. Even when I add the expression :
"If Nz(Me.TxtLineCount, 0) > 0 then" in my "On Enter" event procedure I get the error message at that point.

Many thanks. Colin.
0
Comment
Question by:colinasad
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:jmro20
ID: 34955164
Try setting the focus in the first line of the function like this: [YourTextBox].SetFocus
0
 

Author Comment

by:colinasad
ID: 34955784
Thanks for the prompt response, jmro20.

I tried your suggestion (with the lines shown below) but am still getting the same error message.    

    Me.txtTranTermsMessage.SetFocus
    With Me.txtTranTermsMessage
        .SelStart = 0
        .SelLength = 0
    End With

The VBA appears to crash on the ".SelStart=0" line. So it seems to be happy with the "Me.txtTranTermsMessage.SetFocus" line, but still doesn't like trying to do anything with the control's properties.

I think it is to do with having no real records in the continuous Form. I could live with that if I had a way of recognising that fact. My own ".txtLinesCount" field generates the same error when I try to check it when the Form's recordset is empty.

Can anyone tell me a safe way of checking for an empty recordset?

Regards. Colin.


0
 
LVL 3

Expert Comment

by:jmro20
ID: 34955852
Try this:
If Len(Trim$(Me.txtTranTermsMessage)) > 0 Then
    Me.txtTranTermsMessage.SetFocus
        With Me.txtTranTermsMessage
            .SelStart = 0
            .SelLength = 0
        End With
End If
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:colinasad
ID: 34955902
As I typed my previous message, it occured to me to try the following, in place of the ".SetFocus" expression before I set the ".SelStart" and ".SelLength" values :

"If Me.Recordset.RecordCount > 0 Then"
   
This seems to work OK. At least I don't get the run-time error when I have no records listed in the Detail section of my Continuous Form.

Does anyone have any further suggestions or thoughts they can offer on how better to handle this?

Thanks. Colin.
0
 

Accepted Solution

by:
colinasad earned 0 total points
ID: 34955918
Thanks, jmro.
I only received your last message after I submitted mine.
I'll give your suggestion a try, but I think the problem is with the empty recordset.

Colin.
0
 
LVL 3

Expert Comment

by:jmro20
ID: 34956018
Yes the problem is with the empty recordset.
0
 

Author Closing Comment

by:colinasad
ID: 35067649
Is it OK to accept my own response as the answer? I think we proved that the empty recordset causes the problems when we try to modify a control's properties.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

782 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