Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using custom menu item doesn't work same as shortcut key

Status
Not open for further replies.

MikeCDPQ

Technical User
Sep 11, 2003
173
CA
I posted following problem yesterday and haven't received any suggestions :-( Any idea anyone ?

I have a code that I would like to run either through a menu item on a shortcut menu or from a autokeys (keystroke F8)

Here is the problem piece of code:

Do While CurrentFieldNext = StartingCurrentField
DoCmd.GoToRecord , , acNext
Set CurrentFieldNext = Screen.ActiveControl
Loop

If I press F8 which is my autokeys shortcut it works fine.

If I click my menu item on my shortcut menu it doesn't.

When pressing F8, my loop reads the records values and when the value changes it goes on to do other things.

If however I click to call the same function, the:
Set CurrentFieldNext = Screen.ActiveControl always keeps the value of the first record it encountered and does not change and obviously goes into an endless loop.

Somehow, by clicking on my menu, my control (field) probably looses the focus momentarily and that's what the problem might be.

However, my CurrentFieldName which gives me the name of the current control does give me the right information in either case.

What I am missing ?

Any help will be appreciated as I have been struggling with this for hours and getting to my wits end.

Thanks !

 
I'm not sure I understand 100% of what is going on in all of that, but I do have a possible stab at a sollution (at least a partial sollution).

Have you tried employing something like this?

In one module:
Code:
Public Sub PubVars()
   Dim strCurrFld As String
   Dim strPrevFld As Sting
   'Dim whatever other public vars you might want
End Sub

Then, in your form module:
Code:
Private Sub Form_Load
  FirstControl.SetFocus 'If necessary for whatever reason
  strCurrFld = FirstControl.Name
  'Whatever else you want to do at load
End Sub

'Other Procs

Private Sub Form_Dirty
  If strCurrFld = ActiveControl.Name Then Exit Sub
  If strCurrFld = vbNullString Then
    strCurrFld = ActiveControl.Name
  Else
    strPrevFld = strCurrField
    strCurrField = ActiveControl.Name
  End If
End Sub

The basic idea I have here is that for everytime the form is updated or changed in any way, it is checking to see, ok, did it move to a new control, if so, what was the current control is now previous, and current is the new active control.

Then, you could use this code to set or reset whatever you are trying to do. So, for instance, run whatever other code you are needing to run, and at the end, set the focus back to the most previous (current) control:
Code:
  Form.Controls(strCurrFld).SetFocus

And, also if it is getting stuck in an endless loop, you definitely need to fix that first, regardless of whatever else you do. It could be that you need a conditional Exit Do or Exit For statement in there for certain criteria.

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top