Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

KeyDown code for SubForm

I use the following code to let the user move to the next record on a form (frmDonations) by pressing Ctrl-RightArrow:

_______________________________________________________
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   
    Dim intCtrlDown As Integer
   
    intCtrlDown = (Shift And acCtrlMask) > 0
   
    If (KeyCode = vbKeyRight And intCtrlDown = True) And (cmdGoToNext.Visible = True) Then
        cmdGoToNext_Click
   
    ElseIf (KeyCode = vbKeyLeft And intCtrlDown = True) And (cmdGoToPrevious.Visible = True) Then
        cmdGoToPrevious_Click
   
    End If
   
End Sub
_________________________________________________

cmdGoToNext_Click looks like this:
_________________________________________________

Private Sub cmdGoToNext_Click()

    Check_Allocations
   
    If StopCodeExecution = True Then
        StopCodeExecution = False
        Exit Sub
    Else
        DoCmd.GoToRecord acDataForm, "frmDonations", acNext
    End If

End Sub
_________________________________________________

The main form frmDonations has a subform in it, and the code above works fine if the focus is in the main form frmDonations. But the code doesn't work if the focus is in the subform.

Please can you show me what to do to make the same keystrokes work when the focus is in the subform too. (I want it to move to the next record of the main form frmDonations.)

Thanks.
0
naqayya
Asked:
naqayya
1 Solution
 
Lex_LugerCommented:
Hi naqayya, try this way:
set the procedure Form_KeyDown to Public and in subform on OnKeyDown event write: Forms!frmDonations.Form_KeyDown KeyCode, Shift
0
 
zuijdhoekCommented:


Lex' suggestion will not work because you can't make a form's module public. In order to navigate through the main form records you have to implement following code in the subforms' Form_Keydown mondule. Pay attention to the FindFirst method. Use the primary key value in your main form datasource in order to synchronize both recordsets.

Hope this makes sense to you,

Mark

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo ErrHandling
   Dim intCtrlDown As Integer
   Dim rst As Recordset
   
   intCtrlDown = (Shift And acCtrlMask) > 0
   
   With Me.Parent.Form
     Set rst = .RecordsetClone
     rst.FindFirst "ItemID=" & ![ItemID]
     
     If (KeyCode = vbKeyRight And intCtrlDown = True) And (.cmdGotoNext.Visible = True) Then
       rst.MoveNext
       .Bookmark = rst.Bookmark
     ElseIf (KeyCode = vbKeyLeft And intCtrlDown = True) And (.cmdGoToPrevious.Visible = True) Then
       rst.MovePrevious
       .Bookmark = rst.Bookmark
     End If
     
   End With
   
ExitHere:
   Set rst = Nothing
   Exit Sub
   
ErrHandling:
   Select Case Err.Number
   Case Else
     MsgBox Err.description
   End Select
   
   Resume ExitHere
   
End Sub
0
 
naqayyaAuthor Commented:
Mark: Thanks.

I need to carry out a check before moving to the next/previous record. The code I showed you has a call to Check_Allocations in the cmdGoToNext_Click() proc.

What do I have to do to carry out the check too.

Thanks.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
naqayyaAuthor Commented:
Mark:

I tested your code and I get a 'Type mismatch' error(13) whenever I press any key with focus on the subform. The code line that gives the error is:

Set rst = .RecordsetClone

Any clues?
0
 
naqayyaAuthor Commented:
Mark:

I figured out how to carry out the check (proc Check_Allocations), but could you help me with the error that I am getting?

Is it something to do with my backend database? My Access project (.adp) is connected to a SQL Server database where the data is stored.

Appreciate your help.
0
 
NosterdamusCommented:
Hi nagayya,

>>>but could you help me with the error
that I am getting?

Change:
Set rst = .RecordsetClone

To:
Set rst = Me.RecordsetClone


HTH,

Nosterdamus
0
 
NosterdamusCommented:
actualy, I suggest that you be more specific by replacing:
With Me.Parent.Form
    Set rst = .RecordsetClone
    rst.FindFirst "ItemID=" & ![ItemID]
   
    If (KeyCode = vbKeyRight And intCtrlDown = True) And (.cmdGotoNext.Visible = True) Then
      rst.MoveNext
      .Bookmark = rst.Bookmark
    ElseIf (KeyCode = vbKeyLeft And intCtrlDown = True) And (.cmdGoToPrevious.Visible = True) Then
      rst.MovePrevious
      .Bookmark = rst.Bookmark
    End If
   
  End With


with:
    Set rst = Forms![YourMasterFormNameHere].RecordsetClone
    rst.FindFirst "ItemID=" & ![ItemID]
   
    If (KeyCode = vbKeyRight And intCtrlDown = True) And (Forms![YourMasterFormNameHere].cmdGotoNext.Visible = True) Then
      rst.MoveNext
      Forms![YourMasterFormNameHere].Bookmark = rst.Bookmark
    ElseIf (KeyCode = vbKeyLeft And intCtrlDown = True) And (Forms![YourMasterFormNameHere].cmdGoToPrevious.Visible = True) Then
      rst.MovePrevious
      Forms![YourMasterFormNameHere].Bookmark = rst.Bookmark
    End If
   
HTH,

Nosterdamus
0
 
naqayyaAuthor Commented:
Thanks Nosterdamus:

I'm getting the same error with your new code on the same code line:

Set rst = Forms!frmDonations.RecordsetClone

Any clues?
0
 
NosterdamusCommented:
Hi nagayya,

Lets try another approach.

Say that the first TextBox control's name on your main form is MyFirstControl.

Attached is a modification to your existing Form_KeyDown code:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

   'Just place the real name of the first textbox in the main form between the double quotes.
   'Leave (MyFirstControl) inside the code as is!
   Const MyFirstControl = "PlaceTheRealNameOfTheControlHere"

   Dim intCtrlDown As Integer
   
   intCtrlDown = (Shift And acCtrlMask) > 0
   
   If (KeyCode = vbKeyRight And intCtrlDown = True) And cmdGoToNext.Visible = True) Then
          Forms!frmDonations(MyFirstControl).SetFocus
          cmdGoToNext_Click
   
   ElseIf (KeyCode = vbKeyLeft And intCtrlDown = True) And cmdGoToPrevious.Visible = True) Then
       Forms!frmDonations(MyFirstControl).SetFocus
       cmdGoToPrevious_Click
   
   End If
   
End Sub


Does it work for you?

Nosterdamus
0
 
naqayyaAuthor Commented:
Nosterdamus: Thanks.

Your new code doesn't compile; I get the error:

Sub or function not defined

for the line cmdGoToNext_Click

Thanks again.
0
 
NosterdamusCommented:
Hi nagayya,

Your original question included the following code:

Private Sub cmdGoToNext_Click()

   Check_Allocations
   
   If StopCodeExecution = True Then
       StopCodeExecution = False
       Exit Sub
   Else
       DoCmd.GoToRecord acDataForm, "frmDonations", acNext
   End If

End Sub


That means that you have a subroutine called cmdGoToNext_Click. The question is, does this code located in the MainForm or is it located in the child (sub) form?

My guess is that it is located in the subform, as the command:
Forms!frmDonations(MyFirstControl).SetFocus
will set the focus to the main form, and since cmdGoToNext_Click is defined as private sub in the subform, the error message is triggered, as the sub is not visible to the application while executing from the main.

Try to move the cmdGoToNext control (button) and cmdGoToPrev control to the main menu and see if it works for you.

HTH,

Nosterdamus
0
 
NosterdamusCommented:
Correction:
Try to move the cmdGoToNext control (button) and cmdGoToPrev control to the main menu and see if it works for you.

Should read:
Try to move the cmdGoToNext control (button) and cmdGoToPrev control to the main FORM and see if it works for you.

0
 
naqayyaAuthor Commented:
Thanks Nosterdamus:

Sorry for confusing you. The code for cmdGoToNext_Click and cmdGoToPreviuous_Click is in the MAIN FORM ie frmDonations.
0
 
NosterdamusCommented:
And where does the following sub located?
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

In the Master or in the Sub form?
0
 
naqayyaAuthor Commented:
Nosterdamus:

Right lets get this straight:

The two subs in my original question are located in the master form frmDonations.

So they work fine if the focus is on the master form.

Now I want to make a new KeyDown sub in the subform, so that if the focus is in the subform it will make the MASTER FORM'S record move next or move previous.

Hope it is clear now, Nosterdamus, and sorry for the confusion.
0
 
NosterdamusCommented:
Ok!

Lets go back to our little routine:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

  'Just place the real name of the first textbox in the main form between the double quotes.
  'Leave (MyFirstControl) inside the code as is!
  Const MyFirstControl = "PlaceTheRealNameOfTheControlHere"

  Dim intCtrlDown As Integer
 
  intCtrlDown = (Shift And acCtrlMask) > 0
 
  If (KeyCode = vbKeyRight And intCtrlDown = True) And cmdGoToNext.Visible = True) Then
         Forms!frmDonations(MyFirstControl).SetFocus
         cmdGoToNext_Click
 
  ElseIf (KeyCode = vbKeyLeft And intCtrlDown = True) And cmdGoToPrevious.Visible = True) Then
      Forms!frmDonations(MyFirstControl).SetFocus
      cmdGoToPrevious_Click
 
  End If
 
End Sub


If you want to place it in the sub form as well, then you MUST make sure that you have sub cmdGoToNext_Click and cmdGoToPrevious_Click routines in the sub form as well!

Unless, you make 1 global routine for cmdGoToNext_Click and 1 global routine for cmdGoToPrevious_Click, place it in a global Module and the you can call it from ever you want.

Please note that if you choose to create global routines for the Next & Prev, then you'll have to make the following routines and parameters global as well:
Check_Allocations
StopCodeExecution

Otherwize you'll get the compile error as well.

For additional explanation about the scope of parameters (Private, Public etc.), please search the MS Help for "understanding", and pick "understanding scope and visibility" from the list.



Hope that it clears thing a bit for you now...

Nosterdamus
0
 
NosterdamusCommented:
Ok!

Lets go back to our little routine:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

  'Just place the real name of the first textbox in the main form between the double quotes.
  'Leave (MyFirstControl) inside the code as is!
  Const MyFirstControl = "PlaceTheRealNameOfTheControlHere"

  Dim intCtrlDown As Integer
 
  intCtrlDown = (Shift And acCtrlMask) > 0
 
  If (KeyCode = vbKeyRight And intCtrlDown = True) And cmdGoToNext.Visible = True) Then
         Forms!frmDonations(MyFirstControl).SetFocus
         cmdGoToNext_Click
 
  ElseIf (KeyCode = vbKeyLeft And intCtrlDown = True) And cmdGoToPrevious.Visible = True) Then
      Forms!frmDonations(MyFirstControl).SetFocus
      cmdGoToPrevious_Click
 
  End If
 
End Sub


If you want to place it in the sub form as well, then you MUST make sure that you have sub cmdGoToNext_Click and cmdGoToPrevious_Click routines in the sub form as well!

Unless, you make 1 global routine for cmdGoToNext_Click and 1 global routine for cmdGoToPrevious_Click, place it in a global Module and the you can call it from ever you want.

Please note that if you choose to create global routines for the Next & Prev, then you'll have to make the following routines and parameters global as well:
Check_Allocations
StopCodeExecution

Otherwize you'll get the compile error as well.

For additional explanation about the scope of parameters (Private, Public etc.), please search the MS Help for "understanding", and pick "understanding scope and visibility" from the list.



Hope that it clears thing a bit for you now...

Nosterdamus
0
 
NosterdamusCommented:
Sorry for the DUP posts...
0
 
naqayyaAuthor Commented:
Nosterdamus:

Right lets get this straight:

The two subs in my original question are located in the master form frmDonations.

So they work fine if the focus is on the master form.

Now I want to make a new KeyDown sub in the subform, so that if the focus is in the subform it will make the MASTER FORM'S record move next or move previous.

Hope it is clear now, Nosterdamus, and sorry for the confusion.
0
 
naqayyaAuthor Commented:
Thanks Nosterdamus, it works now.

Much appreciated!
0
 
NosterdamusCommented:
Hi nagayya,

Glad I could help & good luck with your app.

;-)

Nosterdamus
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now