Error 2164 "can't disable a control while it has the fo

merce33

Active Member
Joined
Oct 27, 2006
Messages
277
I get runtime error 2164 "can't disable a control while it has the focus" when i run the following code. why?

Code:
    With Forms!frmDefectSheets
        'main form frmDefectSheets was previously disabled, need to enable it now that
        'a scan has been made. turning ON the main form
        .AllowEdits = True
        .fSubDefectLineItems.Enabled = True
        .txtQtyProduced.Enabled = True
        
        'set the focus elsewhere so that me.txtBarcode can be disabled
        .SetFocus
        .txtQtyProduced.SetFocus
        .SetFocus
        .txtDateCollected.SetFocus
        .SetFocus
        .txtQtyProduced.SetFocus

        'deactivate barcode textbox until record is saved
        'turning OFF the barcode textbox so no scans can be made until a save
       'THIS IS WHERE THE ERROR IS OCCURRING, BUT WHY?
        Me.txtBarcode.Enabled = False
        
        'set focus on defect sheet form and filter to the scanned record
        .SetFocus
        DoCmd.ApplyFilter , "idsDefectSheetID = " & Me.txtBarcode.Value
        .Refresh
        .Requery
        
        'assign date collected time if it didn't already have one
        If IsNull(.txtDateCollected) Then .txtDateCollected.Value = Now()
        
    End With

as you can see, i have set the focus on a different control and thus should be able to disable it.
any suggestions? The Barcode textbox under question simply receives a barcode scan. i then want to filter the form based on the scan, enable certain controls, and disable the box that received the scan, such that no other scans can be made until the record is saved.

thanks,
Mike
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Why so many .SetFocus? I think I counted 6? Are all of these controls on the same form? After setting focus on a field, then you set it on the form again, another field, then the form, etc. Are you just trying to get Access to set the focus somewhere by doing it so many times?

I have never used a barcode reader before, so I don't know about the focus on the barcode field. Is that the only time the barcode reader will input data to the form? That is, when the focus is set to a barcode enabled field?
 
Upvote 0
yeah, originally i had only 1 setFocus but then i got a bit upset at Access giving me the error so i put in a bunch of setFocuses as retaliation. didn't work with only 1, didn't work with all 6.

The txtBarcode textbox is in a subForm, fSubDefectSheetBarcode, within the main form, frmDefectSheets.

From what i have read of the barcode scanner manual, you can practically forget the fact that it is a barcode scanner at all. The scanner works almost exactly like a keyboard, with the only difference being that instead of manually typing data character by character, the scanner "types" in the data all at once. The actual barcode, itself, is nothing special - merely a windows Font type, just like Arial, MS Sans Serif, Times New Roman, etc.. To represent any word or number in "barcode font" you surround it by astericks. For example, to represent the employee number lngEmployeeID as a barcode on my report, I have control source of the text box control, txtEmployeeID, set equal to
Code:
 txtEmployeeID.ControlSource = "*" & lngEmployeeID & "*"
The barcode representing, say, lngEmployeeID = 3456 will be *3456* (in barcode font) on the sheet of paper, but when scanned into the text box on the form it reads 3456 because the font is Arial.

what i would like to do is: after the scanner "types" the DefectSheetID into the textbox on the fSubDefectSheetBarcode SubForm, then filter the form based on the DefectSheetID, enable certain controls on the main form, and disable the text box in the subForm that received the scan, such that no other scans can be made until the record is saved.

Thanks
 
Upvote 0
I would think you need to set the focus to another field on the subform, then you can set the focus to any other field you may want. I'm thinking that for the subform, it still thinks the focus is in the barcode field. I just relooked at your code, and it all looks like it is dealing only with the main form, and never setting the focus to any other field on the subform. If you don't have any other fields to accept focus on the subform, I would suggest creating a field to accept the focus. You can make the field so it can not be seen by making it VERY small, and setting all the colors to the same as the background of where the field is placed.
 
Upvote 0
thanks Vic, problem solved.
for those of you following along at home, the moral of the story is that both a main form and sub form can each carry their own focused control - setting the focus in one does not change the focus in the other.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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