Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

Another setfocus problem

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
ladylucy
Asked:
ladylucy
  • 9
  • 9
1 Solution
 
will_scarlet7Commented:
Any chance txtLastUpdate.Enabled = False?
0
 
ladylucyAuthor Commented:
Nope.   That was mentioned on many of the solutions I found before.  Good guess.  Thanks.
0
 
will_scarlet7Commented:
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
will_scarlet7Commented:
or:

    txtLastUpdate.Locked = False
    Me.repaint
    txtLastUpdate.SetFocus                                    <---------------------- Skipp
    txtLastUpdate.Text = Forms!Welcome!cmbWelcomeScreenName
    txtLastUpdate.Locked = True
0
 
ladylucyAuthor Commented:
As I expected... error... "can't reference a property or method for a control unless the control has the focus"    Thanks for trying.
0
 
will_scarlet7Commented:
Did you try my other suggestion of inserting the Me.Repaint before your setfucus command?
0
 
ladylucyAuthor Commented:
Yes.  It didn't work, with or without the setfocus line.  Same error.  
(I am leaving the office briefly... back later.)  Thanks.
0
 
will_scarlet7Commented:
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
 
ladylucyAuthor Commented:
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
 
will_scarlet7Commented:
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
 
ladylucyAuthor Commented:
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
 
ladylucyAuthor Commented:
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
 
will_scarlet7Commented:
Ok. Glad I could help. ThanX for the points.
0
 
ladylucyAuthor Commented:
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
 
ladylucyAuthor Commented:
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
 
will_scarlet7Commented:
Excelent. Glad you got it worked out!
0
 
ladylucyAuthor Commented:
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
 
will_scarlet7Commented:
Amazing what a break can do.

God bless!
0

Featured Post

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.

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now