Home > Enterprise >  MS Access Go to control on subform
MS Access Go to control on subform

Time:10-01

I have a data entry form where two of the fields are contained in a subform within the main form. We are using barcode scanners to enter data. These barcodes essentially press enter after every scan, however sometimes our barcodes are damaged and, when scanned, can sometimes add random special characters. When ever someone scans a barcode in the "Part Number" field that contains any of the following characters ( , #, %, =) i have a msgbox that pops up alerting the person scanning that the barcode didn't read correctly. After the message pops up I want to refocus on the "Part Number" field so that they can rescan the barcode or just type the data manually (if the barcode keeps being buggy) rather than having to click back in to that field, deleting the wrong data, then re entering the data. My code is below:

Private Sub Part_Number_LostFocus()
Dim RegEx As Object, MyString As String, Answer As Integer
Set RegEx = CreateObject("VBScript.RegExp")
MyString = [Forms]![frmInventoryCountEntry]![tblInventoryCount subform].Form![Part Number]
With RegEx
    .Pattern = "[ #%=] "
End With
If RegEx.Test(MyString) = True Then
    MsgBox "Part Number Contains Invalid Character", vbOKOnly
    DoCmd.GoToControl "tblInventoryCount subform"
    DoCmd.GoToControl "Part Number"

I've tried set focus as well and no matter what the cursor always goes to the next field rather than refocusing on the "Part Number" field. I'm sure the solution is simple but I've tried a bunch of different things and I'm starting to get frustrated :( can anyone help me out? Btw, I'm using access 2003

CodePudding user response:

Have you tried .SetFocus?

You might also consider using a Before Update event and then cancelling if the string contains those symbols. That way the focus never leaves the Part Number control.

  • Related