Results 1 to 3 of 3
  1. #1
    cuylersmith is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    4

    Find out if a particular button is pressed from another control that has focus.

    Hi everyone,

    I have code that requires data to be entered in two controls. I test this by using the following example code:

    Private Sub ctlTruckCoName_LostFocus()
    If IsNull(ctlTruckCoName) Then
    strWarning = "Please either ENTER a Trucking Company Name" & Chr(10) & Chr(13) & Chr(13) _
    & "or CLICK the 'Exit Add Trucker/Vehicle' Button."
    MsgBox strWarning, vbCritical + vbOKOnly, "No Trucking Company Name."
    Me.cmdCancelAdd.SetFocus


    End If
    End Sub


    I have an exit button on the form that exits and returns to a control form without saving or doing any thing with any changes in this form. In the code above you will see that I change the focus from the ctlTruckCoName control to the cmdCancelAdd control. This avoids a circular situation in nothing entered in the control would continually move back to the field.

    What I wish to happen is this code is executed any time focus is moved to a different control other than the cmdCancelAdd control. If the cmdCancelAdd control is clicked I wish to exit the form without the above code being exercised. I would also like the above code executed and focus returned to the ctlTruckCoName control anytime focus tries to move to a control other than the cmdCancelAdd control.

    I don't know if there is a way to tell from an active control what control was clicked, but that seems to be what I need to do. If I knew the cmdCancelAdd button (control) had been pressed while the ctlTruckCoName control has the focus I could then skip executing this code.

    Is there a way to do this?

    Cuyler Smith

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    It is easy to detect the state of a radio button, checkbox, or toggle button but other controls don't have a 'state' or yes/no (on/off). Could use a global variable or an unbound textbox that gets populated if a command button is clicked. That variable could be accessed by any procedure.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Validation code to insure that a Control is populated (has data in it) cannot be done using an event associated with that Control. What good does having this kind of Validation code in the ctlTruckCoName_LostFocus event if the user never moves into the ctlTruckCoName_LostFocus Control? The Record can be saved without the Required data!

    This type of Validation code has to be in the Form_BeforeUpdate event. Here's an example that checks for data in two Controls, but can be expanded, as needed:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
     If Nz(Me.Control1,"") = "" Then
       MsgBox "Control1 Must Not Be Left Blank!"
       Cancel = True
       Control1.SetFocus
       Exit Sub
     End If
     
    If Nz(Me.Control2, "") = "" Then
       MsgBox "Control2 Must Not Be Left Blank!"
       Cancel = True
       Control2.SetFocus
       Exit Sub
     End If
    
    End Sub


    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 10
    Last Post: 11-27-2016, 11:04 PM
  2. Replies: 3
    Last Post: 04-17-2012, 03:26 PM
  3. Replies: 4
    Last Post: 10-28-2011, 03:28 PM
  4. How to control a button when it has focus
    By shubhamgandhi in forum Programming
    Replies: 2
    Last Post: 07-28-2011, 03:18 PM
  5. Lock Record when "Add" button is pressed
    By jo15765 in forum Access
    Replies: 34
    Last Post: 11-28-2010, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums