Run-time 2110: Can't move focus to the control

JONeill

Board Regular
Joined
Sep 2, 2018
Messages
58
I have a form that when the user double-clicks a listbox, the record is retrived and placed on the form for editing. I keep getting the runtime error when I try to clear the form. The code is below.

Code:
'Traverses form and clears contents from textboxes
    For Each cCont In Me.Controls

        If TypeName(cCont) = "TextBox" Then
            cCont.SetFocus
            cCont.Text = ""
        End If
    Next cCont

The code is used to clear textboxes on a form. The control in question is a listbox. This is a project that I originally started in Excel and realized that it would better to use Access although, I've never programmed in it before. The SetFocus is in there because I got an error that I couldn't change properties on a control unless it had focus. Seemed a bit strange to me. I'm beginning to wonder if the file isn't corrupted some how.I have not tried to change each indiviual textbox but that would be the next step. I'm hoping that someone has an idea of what's going on. It seems to me that there is no reason why this code shouldn't run. This logic has run on every MS product I've ever programmed in.


Another strange thing is the name of the control in the error message is Maintenance Log. It says "Run-time 2110 Maintenance Log Can't move focus to the control txtDescription. txtDescription is on the form but there is no control named Maintenance Log. The listbox is named lstMaintLog. I did change it from lstMaintenanceLog after I wired the double-click event to it. I changed the event name and recreated it so, there shouldn't be any problem there.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Found out what the problem was. I should've use the value property instead of the text property. I don't understand why VBA usage isn't more consistent across the Office suite.
 
Upvote 0
I would suggest you use the .value property unless you have reason to do otherwise (i.e., in your excel and access code). In Excel, .Text means "the text as displayed" (and is very dangerous because the displayed value is not necessarily the same as the underlying value - to use a very simple example, a display value might be rounded off). Access has no such concept.

In some cases, Excel programmers prefer to use the displayed value of a an Excel cell though I think the need to do so should be rare and offhand I can't think of a reason why you would want do at the moment. Perhaps in cases where precision is less important and you actually want the formatted output. However, if a user does something such as changing the width of a cell you can actually change the displayed value and therefore the value of the .Text property - so it's very dangerous if you need the real values in the cells. I think more often when you see someone using the .Text property in Excel it is because they don't really know the difference between .Text and .Value and so they picked the first one.
 
Upvote 0
I sure didn't know the difference. Thanks for the tip. I've only been coding in Excel since July and just started Access. I had to use it because the project just works better with a database.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,558
Messages
6,114,297
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top