Send Keys (1 Viewer)

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Sep 12, 2006
Messages
15,614
The only thing I use sendkeys for is to send an {esc} key. (undo)

I just tried it, and it failed "permisison denied - error 70"

What should I do to undo an invalid entry in a control, in the control before update event. It's a legal entry but correctly rejected. This form happend to be unbound.

I have tried these within the control before update so far.
me.undo - does nothing
runcommand accmdundo - isn't available now.

what's the right way to undo an entry/simulate an {esc} press
 

Micron

AWF VIP
Local time
Today, 05:51
Joined
Oct 20, 2018
Messages
3,476
IIRC Undo is only for bound forms.
Set to Null or "" in control BeforeUpdate?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:51
Joined
Oct 29, 2018
Messages
21,358
Try using the Control's AfterUpdate event and set itself to Null.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Sep 12, 2006
Messages
15,614
I can't use the after update. If I cancel the before update, I don't get an after update.

I think i'll leave it to the users to press Esc manually.
It's not always blank - sometimers it needs to go back to the old value.

A lot of code to amend though.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:51
Joined
Oct 29, 2018
Messages
21,358
I can't use the after update. If I cancel the before update, I don't get an after update.

I think i'll leave it to the users to press Esc manually.
It's not always blank - sometimers it needs to go back to the old value.

A lot of code to amend though.
Hi Dave. Just curious, do you have to use the BeforeUpdate as well? What if you could do everything you need by just using the AfterUpdate event? I thought the OldValue property is only applicable to bound controls? Just thinking out loud...
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Sep 12, 2006
Messages
15,614
Hm.

I suppose with an unbound from, I don't need to use beforeupdate/afterupdate - it's just force of habit. I do have various checks on the different controls to make sure that the input value is legit,(before update) and further commands in the after update to process the entry.

Pressing <Esc> does return the current control to a former value. The before update event still has the cancel parameter which prevents the after update running.

I just checked again - an unsaved new value DOES return to a previous value in an unbound control, but not once it's saved. That's probably the difference. I don't think you can undo the entire record to it's unedited state, but the single control operation was all I needed.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:51
Joined
Feb 28, 2001
Messages
27,001
With an unbound form, I didn't think either of the update events (or the current event, for that matter) ever fired. But returning something to a "former value" requires that there must exist a .OldValue property, and if the form is unbound, many of the controls MUST (by their nature) be unbound. If so, THEY don't have .OldValue either, so an Undo won't be available. (And you say you see that message, so I guess that's right.) I believe that the form can't get dirty either because that condition implies that the current and previous contents don't match. If there is no underlying record for Access to compare to, I don't think it can decide this question.

You're an experienced guy, Dave, so you should know this. OR... What am I not seeing here? How DO you return something to a previous value on an unbound form? Store it somewhere via VBA?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Sep 12, 2006
Messages
15,614
Try it. Create an unbound form with a couple of fields. Enter a value in a unbound control. Then start to edit the value. Before you leave the control you can press <esc> and undo the edit, returning it to the value before you changed it.

There is certainly a before update and an after update event.

I have always done this, but I used it last evening while testing a different change, and was surprised to get an error. I think I originally used sendkeys "{esc}" because I hadn't been able to find another method in the past, and sendeys just worked. (an older usage I saw did this with a DoMenuItem command, but I never used that)

[Edit - I think it was a version of this command
DoMenuItem acFormBar, 3, 0, , acMenuVer70]


eg this similar discussion about issues with sendkeys in Vb.
https://stackoverflow.com/questions/2307738/sendkeys-permission-denied-error-in-visual-basic


Here's the code for an unbound form, with a couple of unbound controls.
This is the code for the first textbox.

The problem is that I would like to automatically undo the edit if the value is not acceptable.


Code:
Option Compare Database
Option Explicit

Private Sub Text0_BeforeUpdate(Cancel As Integer)
MsgBox "before update event fired"

     'assume the entered value was not acceptable, so you want to cancel it, and return it to the previous value.
    If MsgBox("Cancel Y/N ", vbQuestion + vbYesNo) = vbYes Then
        Cancel = True
        On Error GoTo failsend
        SendKeys "{esc}" 'this is the problem. This now errors with Error 70, hence the error handler to demonstrate it.
exitpoint:
        Exit Sub
    End If

    Exit Sub

failsend:
    MsgBox "Error: " & err & vbCrLf & _
    "Desc: " & err.description
    Resume exitpoint
End Sub

Private Sub Text0_AfterUpdate()
      MsgBox "after update event fired"
End Sub

'previous version with no sendkeys issue. 
Private Sub Text0Version_BeforeUpdate(Cancel As Integer)
     'assume the entered value was not acceptable, so you want to cancel it, and return it to the previous value.

        Cancel = True
        SendKeys "{esc}" 'this is the problem. This now errors with Error 70. 

End Sub
 
Last edited:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:51
Joined
Sep 12, 2006
Messages
15,614
out of interest, I just plugged in the snippet from the above article, (I called it MySendKeys) and it sends the escape with no error, so I think I have a solution now.

Code:
Public Sub MySendkeys(text as variant, Optional wait As Boolean = False)
   Dim WshShell As Object
   Set WshShell = CreateObject("wscript.shell")
   WshShell.Sendkeys cstr(text), wait
   Set WshShell = Nothing
End Sub
 

Users who are viewing this thread

Top Bottom