Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2014
    Posts
    2

    Unanswered: Set Focus to control

    Hi All,

    I have a control on my form that looks up a value in a linked table. The goal was to ensure that a product part number was scanned in at the previous station before proceeding. Right now, it does that but if the part number is not located/keyed in wrong, it still saves the record. I would like to re-select the control (CathodeID) in the event that the part number is not found. I have the below code. Any help is much appreciated.

    Code:
    Private Sub CathodeID_LostFocus()
                Dim myCartridgeID As String
                myCartridgeID = Me.CathodeID.Text
                
                If Len(Trim(myCartridgeID)) > 0 Then
    
       
                Dim db As DAO.Database
                Dim myRecordSet As DAO.Recordset
                Dim MySQL As String
                
                'Open connection to current Access database
                Set db = CurrentDb()
                
                'Create SQL statement to retrieve value from GST table
                MySQL = "select * From [PottingIn] Where [CathodeIDIn] = " & """" & Me.CathodeID.Text & """"
                
                Set myRecordSet = db.OpenRecordset(MySQL)
                
                'Retrieve value if data is found
                If myRecordSet.EOF = False Then
                   'do nothing
                Else
                   MsgBox ("This Cathode was not Potted In!")
               
                End If
                
                                    
                myRecordSet.Close
                Set myRecordSet = Nothing
                
    
                End If
    End Sub
    Thanks in advance!,
    Audra

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Did you try:
    Code:
               If myRecordSet.EOF = False Then
                   'do nothing
                Else
                   MsgBox ("This Cathode was not Potted In!")
                   Me.CathodeID.SetFocus
                End If
    Have a nice day!

  3. #3
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    You cannot set Focus to a Control from its own LostFocus event. This kind of thing needs to done be in the CathodeID_BeforeUpdate event. If the test fails, you then simply cancel the update of the Control, using

    Cancel = True

    so it'd be

    Code:
    Else
      MsgBox ("This Cathode was not Potted In!")
      Cancel = True         
    End If
    and Focus will remain in the CathodeID Control.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •