Solved

Another setfocus problem

Posted on 2004-08-12
18
433 Views
Last Modified: 2008-03-17
I apologize, but none of the previous setfocus solutions have helped me.   I am trying to setfocus to a text box and I am getting the 2110 error message "can't move focus to the control."   Here is the code:

Private Sub cmdApproved_Click()
    Dim stDocName As String
    stDocName = "Entry Form"
    cmbStatus.SetFocus
    cmbStatus.Text = "Approved"            <-------------
    txtApprovalDate.SetFocus
    txtApprovalDate.Text = Format(Now, "short date")
    DoCmd.Save acForm, stDocName
    cmdApproved.Enabled = False
End Sub

Private Sub cmbStatus_Change()
    If cmbStatus.Text = "Completed" Then
        txtCompletionDate = Format(Now, "Short Date")
        cmdApproved.Enabled = False
    Else
        txtCompletionDate = ""
    End If
    txtLastUpdate.Locked = False
    txtLastUpdate.SetFocus                                    <----------------------
    txtLastUpdate.Text = Forms!Welcome!cmbWelcomeScreenName
    txtLastUpdate.Locked = True
    cmdSave.Enabled = True
    cmdCancel.Enabled = True
End Sub

When the cmdApproved button is clicked, it jumps from where my first arrow is down to cmbStatus_Change() as expected, because it changed the value of that combo box.  But when it gets to my second arrow - txtLastUpdate.SetFocus, I get the error message.    Note that when someone simply changes cmbStatus using the drop-down box, there is no error message.  This app was already written before I got a hold of it.  They asked me to lock the txtLastUpdate field so users can't change it manually, so I am now trying to get around that by unlocking whenever necessary.  Thanks for any advice you can give me.
0
Comment
Question by:ladylucy
  • 9
  • 9
18 Comments
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11784075
Any chance txtLastUpdate.Enabled = False?
0
 

Author Comment

by:ladylucy
ID: 11784090
Nope.   That was mentioned on many of the solutions I found before.  Good guess.  Thanks.
0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11784092
Also what if you comented out the txtLastUpdate.SetFocus, and just ran the code like so?

    txtLastUpdate.Locked = False
    'txtLastUpdate.SetFocus                                    <---------------------- Skipp
    txtLastUpdate.Text = Forms!Welcome!cmbWelcomeScreenName
    txtLastUpdate.Locked = True
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11784104
or:

    txtLastUpdate.Locked = False
    Me.repaint
    txtLastUpdate.SetFocus                                    <---------------------- Skipp
    txtLastUpdate.Text = Forms!Welcome!cmbWelcomeScreenName
    txtLastUpdate.Locked = True
0
 

Author Comment

by:ladylucy
ID: 11784149
As I expected... error... "can't reference a property or method for a control unless the control has the focus"    Thanks for trying.
0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11784318
Did you try my other suggestion of inserting the Me.Repaint before your setfucus command?
0
 

Author Comment

by:ladylucy
ID: 11784359
Yes.  It didn't work, with or without the setfocus line.  Same error.  
(I am leaving the office briefly... back later.)  Thanks.
0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11784602
What version of Access are you running?

Try updating the field without calling the .text property like this:

Private Sub cmbStatus_Change()
    If cmbStatus.Text = "Completed" Then
        txtCompletionDate = Format(Now, "Short Date")
        cmdApproved.Enabled = False
    Else
        txtCompletionDate = ""
    End If
    txtLastUpdate = Forms!Welcome!cmbWelcomeScreenName
    cmdSave.Enabled = True
    cmdCancel.Enabled = True
End Sub

Notice I removed the .Locked lines and the .SetFocus as when updating it like this you should be able to update it while it is locked and without having to set the focus (I just tried it in Access 2003).
0
 

Author Comment

by:ladylucy
ID: 11785662
Access 2000...  OK, that wasn't the actual solution, but it led me to the reason for the problem.    When I did what you said, I got an error on

   cmbStatus.Text = "Approved"      

It said: "The text you entered isn't an item in the list."
Explanation: Only managers can see the Approved button, and clicking the button is supposed to be the only way of changing this field to "Approved"

When I changed the code back to the way it was, and changed the above line to

cmbStatus.Text = "Awaiting Approval"    (which is an item in the list)

it worked!   That means it won't let me setfocus to the next control until I have finished properly setting the value of the last control to an item in the list ... I guess.  

Now I have to figure out how to get around this...  

I wonder why this worked before I locked that txtLastUpdate text box.   I'll have to go back to the previous version and see what else I changed.  There were other changes.
0
 
LVL 15

Accepted Solution

by:
will_scarlet7 earned 250 total points
ID: 11785764
Try just reading the value of cmdStatus like I suggested without setting the focus there. That should allow you to get the value even if you are not an admin and the value = "Approved" because you do not need to validate that field unless you set the focus there and then move the focus.
*******************
Private Sub cmbStatus_Change()
    If cmbStatus = "Completed" Then
        txtCompletionDate = Format(Now, "Short Date")
        cmdApproved.Enabled = False
    Else
        txtCompletionDate = ""
    End If
    txtLastUpdate = Forms!Welcome!cmbWelcomeScreenName
    cmdSave.Enabled = True
    cmdCancel.Enabled = True
End Sub
0
 

Author Comment

by:ladylucy
ID: 11785921
I'm not having a problem with checking cmbStatus = "completed"... But that works for populating cmbStatus = "Approved" and not creating any errors!   Yay!

Private Sub cmdApproved_Click()
Dim stDocName As String
    stDocName = "Entry Form"
    cmbStatus = "Approved"      

But of course, now I have another frustration... it is not populating the value of txtLastUpdate at all... whether or not I have the setfocus there.   Aaaaargh!   I will give you the points for helping me with the initial problem.   Thanks for all your help.   You got rid of a lot of frustration.  
0
 

Author Comment

by:ladylucy
ID: 11786027
FYI - I just put in a STOP to step through the code and found that the code does not jump to cmbStatus_Change() when you don't use the following lines:

cmbStatus.SetFocus
cmbStatus.Text = "Approved"

If you simply use:

cmbStatus = "Approved"

it goes to the next line within the same sub.

I guess I'm still stuck...  but your ideas did help me get out of the rut that I was in.  

0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11786136
Ok. Glad I could help. ThanX for the points.
0
 

Author Comment

by:ladylucy
ID: 11786180
No problem... Thank you.   Watch for my question about populating a combo box with a value that is not in the list!   Haha!   You showed me how to do it quite nicely, but somehow it isn't acknowledged as a "change."   But I should put this under a new topic. It's no longer a setfocus issue.   Tata!
0
 

Author Comment

by:ladylucy
ID: 11893858
OK, I had cmbStatus.LimitToList equal to True.    duh!!!!      So I added to the code... see arrows...

Dim stDocName As String
    stDocName = "Entry Form"
    cmbStatus.LimitToList = False            <------------
    cmbStatus.SetFocus
    cmbStatus.Text = "Approved"          
    cmbStatus.LimitToList = True         <-------------
    txtApprovalDate.SetFocus


Now it works....
0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11893965
Excelent. Glad you got it worked out!
0
 

Author Comment

by:ladylucy
ID: 11894043
Yeah, I was on vacation last week...  Came back and looked at it with a clear and rested mind.  And bingo!  ...a VERY SIMPLE answer.   How silly.
0
 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11894206
Amazing what a break can do.

God bless!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

838 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