Application.OnKey Fails to Reset Properly - vba

I am trying to help another user in this forum (reference). The goal is to use the Enter key on the numeric keypad to trigger a macro rather than use the Worksheet_Change event macro for the same purpose.Before anything is run, both Enter keys work identically. You type stuff in, touch either key, the material gets placed in the cell and Selection moves downward. If you just pump either key, Selection just moves downward.
In a standard module I put:
Sub NumericEnter()
Application.OnKey "{ENTER}", "InsertIntoTables"
End Sub
and
Sub InsertIntoTables()
MsgBox "Inserting"
End Sub
After I run NumericEnter(), the Enter key on the numeric keypad calls the proper macro each time it is touched. The normal Enter key is not affected.
I then tried to restore the numeric Enter key by running:
Sub ClearEnter()
Application.OnKey "{ENTER}", ""
End Sub
The numeric Enter key partially reverts to its original behavior. It no longer calls the macro; it allows data entry into cell, but it does not move Selection at all! (the regular enter key still behaves normally)
I have no idea of what I have missed.
What I have looked at:
The normal enter key, the ARROW keys, and the TAB key work just fine at changing Selection
Mouse clicking works fine at changing Selection
the values of Application.MoveAfterReturn and Application.MoveAfterReturnDirection look fine as well.

Using just
Application.OnKey "{ENTER}"
with no second argument seems to clear the macro mapping and return the default key action (Excel 2013)
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-onkey-method-excel
expression.OnKey( Key , Procedure )
The description for the Procedure parameter has this:
A string indicating the name of the procedure to be run. If Procedure
is "" (empty text), nothing happens when Key is pressed. This form of
OnKey changes the normal result of keystrokes in Microsoft Excel. If
Procedure is omitted, Key reverts to its normal result in Microsoft
Excel, and any special key assignments made with previous OnKey
methods are cleared.

Related

Opening form to specific record: Object required

I am trying to open a form, from a button, to a particular record by primary key. The value of the primary key I want to open the form to is in a text box named [MainLinkStoff] next to the button. There is a main form called [HUB] and a navigation subform on it. The button and the textbox are both on that navigation subform. I got the reference for the textbox using the expression generator, so it should work. In fact, when plugging the expresion into another textbox somehwere else, it does. The name of the form I want to open is [neusdb]. Hardcoding in the value for the primary key works too. When I run the button, I get:
Runtime error '424':
Object required
The code for my button is as follows:
Private Sub command_new_sdb_copy_Click()
DoCmd.OpenForm "neusdb", , , "stoff_id=" & Forms![HUB]![NavigationSubform].Form![MainLinkStoff]
End Sub
I know I can use a shorter handle for the reference, since the button is located on the same form as the text box. Both versions result in the same error though.
My Database:

In a VBA Userform, which event is triggered when exiting a filed

I'm using Microsoft Office Professional Plus (64 bit) on a Windows 10 (64 bit) platform. I have a subroutine that is processed when I make a change to a Userform field called MyDate. It's called Private Sub MyDate_AfterUpdate(). It's the second field on a form. It works fine as long as the contents of the MyDate field are edited. However, if the user doesn't need to update the contents of the MyDate field because they accept the default of the field and just presses the tab key past that second field, I'd still like the subroutine to be executed. What event can I use to activate code when I simply tab through the field and don't necessarily edit the contents? Thanks for looking at this.
If you look at the top of the code panes, you'll notice two dropdowns. The left one contains all interfaces and event providers you can implement in that class (a UserForm is a class).
Select your MyDate control from that dropdown; the right-side dropdown is now listing every event you could handle for this MyDate control:
In this particular case, the Exit event seems a good candidate:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'make Cancel.Value = True to prevent exiting the control.
'...ideally... make that conditional...
End Sub
By consistently using these dropdowns to let the VBE generate event handler procedures for you (instead of typing them up from memory), you avoid getting it wrong... and getting an event handler signature wrong can do anything from literally nothing at all, to compile errors if you're lucky, or weird and hard-to-diagnose behavior if you're less lucky.

In Access VBA how does one keep a textbox active and select all its contents after Enter key

In an Access form I am using a textbox to filter an unbound subform. I want to allow the end user to quickly perform quick sequential searches. After the Enter key is pressed to perform a first search, I want the cursor/focus to remain in the text box and the current search text to be selected. In this way the user can directly start entering a new query, replacing the old search text.
I tried the answers to this question, but due to the Enter key, the selection always gets lost:
The solution I came up with is to intercept the Enter key press. It is important to cancel the event, otherwise the Enter key will remove the selection.
Private Sub txtSearch2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Call Search
If Len(Me.txtSearch2.Text & "") = 0 Then Exit Sub
Me.txtSearch2.SelStart = 0
Me.txtSearch2.SelLength = Len(Me.txtSearch2.Text)
DoCmd.CancelEvent
End If
End Sub

Cancel alt + down arrow: remaining effect from VBA code

I have a pivot table with page field. I wrote a few lines of code under the Pivot Table update event to do something.
When the page field filter is used via the keyboard, the filter form is accessed with the shortcut alt + down arrow typed on a page field cell.
Code works fine and at end moves focus to a specific cell.
Problem is that alt + down arrow effect remains! The cell is shown with a drop down list displayed and the operator needs to hit the escape key to continue working. I try to avoid this by adding a sendkeys {esc} line after, but the sendkeys numlock bug is more annoying than having to directly hit the escape key.
My question is:
Is there another way to close, using vba code, the drop down list displayed in final cell?
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Sheet1.ListObjects("precomanda").Resize Range(Sheet1.ListObjects("precomanda").HeaderRowRange, _
Sheet1.ListObjects("precomanda").HeaderRowRange.Offset(Sheet1.PivotTables("Istoric").PivotFields("Produs").DataRange.Count, 0))
Sheet1.Range("A5").Activate
Application.SendKeys ("{ESC}")
End Sub

Prevent moving to next record when hitting Enter?

I have a form in Access 2003 that should only be working with a single record. I can set the Cycle property to Current Record, but the form still jumps to the next record when I press Enter. My first thought was a KeyPreview property, but I'm not seeing one. My other thought is maybe the KeyPress or KeyUp event, but I thought I'd ask in case of unintended consequences. Any ideas?
The Cycle property only affects the TAB key.
To control the behaviour of the Enter Key that's a global property.
Go to Tools/Options - Keyboard tab and on "Move After Enter" select "Next Field"
There are KeyPress and KeyDown events that you can use to trap the Enter key too but that's more work.
This can also be done in vba.
Application.GetOption "Move After Enter" 'get current setting
Application.SetOption "Move After Enter", 0 'don't move
Application.SetOption "Move After Enter", 1 'Next Field
Application.SetOption "Move After Enter", 2 'Next Record
http://www.pcreview.co.uk/forums/enter-key-moving-next-field-t3454281.html
The moment keys like TAB, Alt, PgUP, PgDn, Enter are pressed at the end of adding the record (after the last field mostly), the database auto saves all the info you entered on the form and wipes out the fields so that you can enter the next value. So what happens is that the data is saved but the form looks empty.
3 things to do:
Form Cycle Property set to Current Record.
Form Key Preview Property set to Yes.
Add following code to the KeyDown Event of the form:
'33 - PgUp; 34 - PgDown; 9 - Tab; 18=Alt; 13=Enter
Select Case KeyCode
Case 33, 34, 18, 9, 13
KeyCode = 0
Case Else
'Debug.Print KeyCode, Shift
End Select
I found this while scouring the web and don't take credit/responsibility for the code but I don't know where I found it. Works for me!
The Cycle property only works with the Tab key.
There are two options you could pursue.
You could trap the Enter key in KeyDown/KeyUp/KeyPressed
- OR -
You could filter the data source to the one record you want them editing, and disable adding new records through that form.
You can add below code to your form 'BeforeUpdate' event. If use want to move to next record, it will ask user to save then close the form before they can move to another recorde.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MsgBox("Save?", vbYesNo)
Case vbYes
DoCmd.Close
Case vbNo
Cancel = True
End Select
End Sub
If you go into Access Options on the file page, go to Client Settings and the first setting will let you choose where your focus changes to when you press enter. At least in Access 2013.