Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2011

    Unanswered: run a sub when a form itself has the focus

    I have a Sub i'd like to run when the main form itself has the focus. There is a main form that has customer info and buttons for several popup forms (non-modal) that are linked to the customer that is currently displayed on the main form. The user can bounce back and forth between open forms. I have a Sub that does what i want, but only if i put it in the timer event of the main form; don't like to use the timer event on databases on a network drive, slows it down. So i've been trying On Got Focus and On Activate and a few others, but the event doesn't get triggered when the user clicks on the Main Form, away from one of the other popup forms. I simply need to trigger this sub when the Main Form itself has the focus, not any particualr control.

    Thank you

  2. #2
    Join Date
    Apr 2014
    Provided Answers: 34
    The form has an event called ON LOST FOCUS. did you try that?
    (remember the FORM event, not a control)

  3. #3
    Join Date
    Oct 2011
    Unfortunately no…

    Maybe I should explain further. I'm trying to prevent the user from changing from one customer to another on the main form “frmMain” while any other popup form is open.
    I have this sub in a module:

    Public Sub MainFormLock()

    Dim intForms As Integer, frmCurrentForm As Form
    intForms = Forms.Count
    frmCurrentForm As Form
    Set frmCurrentForm = Screen.ActiveForm

    If frmCurrentForm.Name = "frmMain" And intForms > 1 Then
    Forms!frmMain!txtMultiSearch.Enabled = False
    Forms!frmMain!cmdSearch.Enabled = False
    Forms("frmMain").NavigationButtons = False
    Forms!frmMain!txtMultiSearch.Enabled = True
    Forms!frmMain!cmdSearch.Enabled = True
    Forms("frmMain").NavigationButtons = True
    End If

    End Sub

    It disables a search box and button as well as the navigation buttons of the form if the user is on the frmMain and there is another form open. It does work just fine if I call MainFormLock in the Timer Event .. but I want to avoid that. Can’t seem to find another event on the frmMain to trigger MainFormLock. Based on other searches around web, it seems difficult to test when a form itself has the focus or is active (like when a user bounces back and forth between open forms).

    Thank you

  4. #4
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    You shoudl be abel to do waht you want, however it isn't going to be straightforward as I dont' think what you are tryng to do was envisaged by the designersd of Access.

    on focus wont work, IIRC, unless you put the code in each and every control as well as the form itself. Im pretty certain the controls on focus has to be set as its different from the forms focus.

    what I think you need to do is
    disable the navigation buttons, disable the controls, disable the form close whilst the sub forms are open.

    you could declare a public variable / property in the master form. increment it by one every time one of the child forms opens (in its on open event), decrement it everytime one of the child forms closes (on close event)

    when you opne your child forms, lock up the top level form as required.

    when you decrement the the counter (use a method/function/sub NOT direct variable assignement) and as part of that method/function/sub unlock the controls your previously locked.

    do the pqrent form lock in a function eg LockControls(state as Boolean)
    private function LockControls(state as boolean)
    mycontrol.enabled = state
    ...and so on for each item to be set
    end fucntion

    then when you leave the main form
    when you return

    so that you can set all the controls in one hit. there is no risk of you locking one control and forgetting to unlock it because you are using a different code block

    as to which properties and or controls you lock thats up to you and your requirments.

    whatever way you do achieve this will be longwinded, precisely because I don't think its a design paradigm the developes of Access consider. and that may reflect your own design paradigm.
    I'd rather be riding on the Tiger 800 or the Norton

Tags for this Thread

Posting Permissions

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