After Update, stay on Same control but sel.start sel.length to highlight it (1 Viewer)

bignose2

Registered User.
Local time
Today, 08:20
Joined
May 2, 2010
Messages
219
Hi,

I want to be able to type data into Outdate1, e.g. press enter
Process stuff on the value but keep focus on Outdate1 & highlight it, ready to type over without manually deleting.

If in the AfterUpdate of the Outdate1 control I have ..

Me!OutDate1.SetFocus
Me!OutDate1.SelStart = 0
Me!OutDate1.SelLength = Len(Me!OutDate1)

It does not work, I think it does highlight but then reset & cursor at the start

I sure over the years tried this but never had an luck for an easy solution, I think it is to do with the focus actually moving away after update.
I think I managed before going to another control & back again but messy.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:20
Joined
May 7, 2009
Messages
19,169
can you use Sendkeys:

SendKeys "+{TAB}"
 

bignose2

Registered User.
Local time
Today, 08:20
Joined
May 2, 2010
Messages
219
Many thanks,
All these years!
I must admit I was always advised to avoid sendkeys (not sure why) but such a simple & working solution is not going to be ignored.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:20
Joined
May 7, 2009
Messages
19,169
sendkeys sometimes mess with your "number keypad".
not a big issue (the numlock sometimes get toggled on/off).
i don't know if it got fixed in A2016/2019.

for a better alternative, this is the code (paste it in new Module).
to use:

Call fSendKeys("+{TAB}")

Code:
Option Compare Database
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' author:
'
'   Dev Ashish and Arvin Meyer
'
' modified:
'
'   arnelgp
'   new VB7 and VBA7 support
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' VERY GOOD, WORKING!
'
'******** Code Start ***********
      ' Declare Type for API call:
      Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128   '  Maintenance string for PSS usage
      End Type

      ' API declarations:
#If VBA7 Then
    Private Declare PtrSafe Function GetVersionEx Lib "kernel32" _
        Alias "GetVersionExA" ( _
            lpVersionInformation As OSVERSIONINFO) As Long
           
    Private Declare PtrSafe Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare PtrSafe Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long

    Private Declare PtrSafe Sub keybd_event Lib "user32" ( _
        ByVal bVk As Byte, _
        ByVal bScan As Byte, _
        ByVal dwflags As Long, _
        ByVal dwExtraInfo As LongPtr)

#Else
      Private Declare Function GetVersionEx Lib "kernel32" _
         Alias "GetVersionExA" _
         (lpVersionInformation As OSVERSIONINFO) As Long

      Private Declare Sub keybd_event Lib "user32" _
         (ByVal bVk As Byte, _
          ByVal bScan As Byte, _
          ByVal dwflags As Long, ByVal dwExtraInfo As Long)

      Private Declare Function GetKeyboardState Lib "user32" _
         (pbKeyState As Byte) As Long

      Private Declare Function SetKeyboardState Lib "user32" _
         (lppbKeyState As Byte) As Long
#End If

' Constant declarations:
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_CAPITAL = &H14
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Const VER_PLATFORM_WIN32_NT = 2
Const VER_PLATFORM_WIN32_WINDOWS = 1

Function IsCapsLockOn() As Boolean
        Dim o As OSVERSIONINFO

        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsCapsLockOn = keys(VK_CAPITAL)
End Function

Sub ToggleCapsLock()
        Dim o As OSVERSIONINFO

        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)

        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
        'Toggle capslock
            keys(VK_CAPITAL) = Abs(Not keys(VK_CAPITAL))
            SetKeyboardState keys(0)
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
          'Simulate Key Press>
            keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
          'Simulate Key Release
            keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
               Or KEYEVENTF_KEYUP, 0
        End If
End Sub

Function IsNumLockOn() As Boolean
        Dim o As OSVERSIONINFO
       
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsNumLockOn = keys(VK_NUMLOCK)
End Function

Sub ToggleNumLock()
        Dim o As OSVERSIONINFO
               
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)

          If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
                keys(VK_NUMLOCK) = Abs(Not keys(VK_NUMLOCK))
                SetKeyboardState keys(0)
          ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
          'Simulate Key Press
            keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
          'Simulate Key Release
            keybd_event VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY _
               Or KEYEVENTF_KEYUP, 0
          End If
       
End Sub

Function IsScrollLockOn()
        Dim o As OSVERSIONINFO
       
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        IsScrollLockOn = keys(VK_SCROLL)
End Function

Sub ToggleScrollLock()
        Dim o As OSVERSIONINFO
       
        o.dwOSVersionInfoSize = Len(o)
        GetVersionEx o
        Dim keys(0 To 255) As Byte
        GetKeyboardState keys(0)
        If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  '=====Win95
            keys(VK_SCROLL) = Abs(Not keys(VK_SCROLL))
            SetKeyboardState keys(0)
        ElseIf o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   '=====WinNT
            'Simulate Key Press
            keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
            'Simulate Key Release
            keybd_event VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
        End If
End Sub

Private Sub mySendKeys(sKeys As String, Optional bWait As Boolean = False)
    Dim bNumLockState As Boolean
    Dim bCapsLockState As Boolean
    Dim bScrollLockState As Boolean
    bNumLockState = IsNumLockOn()
    bCapsLockState = IsCapsLockOn()
    bScrollLockState = IsScrollLockOn()
    SendKeys sKeys, bWait
    If IsNumLockOn() <> bNumLockState Then
        ToggleNumLock
    End If
    If IsCapsLockOn() <> bCapsLockState Then
        ToggleCapsLock
    End If
    If IsScrollLockOn() <> bScrollLockState Then
        ToggleScrollLock
    End If
End Sub

Function fSendKeys(sKeys As String, Optional bWait As Boolean = False)
' Function to make it callable from macros
    mySendKeys sKeys, bWait
End Function
'******** Code End ***********
 

Isaac

Lifelong Learner
Local time
Today, 01:20
Joined
Mar 14, 2017
Messages
8,738
You were told to avoid sendkeys for very good reason, you absolutely should avoid it. It's like telling VBA: "Start typing on my keyboard like a ghost, having no idea where those keystrokes might go". They might go to Access. They might click "Ok" on a pop-up you didn't expect on your keyboard asking you if you want to delete your C drive. They might launch nuclear codes that start WWIII. You get the idea.

You absolutely want to get it working and not use SendKeys. Sendkeys is the equivalent of throwing a football at your computer and hoping you jostle something worth jostling that, in some twist of Darwinian evolutionary fate, just happens to press the right key at the right time and make everything all right. Ok, I'll stop now.

Wondering if this helps at all

Me!OutDate1.SetFocus
Me!OutDate1.SelStart = 0
Me!OutDate1.SelLength = Len(Me!OutDate1.Text)
 

AccessBlaster

Registered User.
Local time
Today, 01:20
Joined
May 22, 2010
Messages
5,828
1614448375944.png
 

bignose2

Registered User.
Local time
Today, 08:20
Joined
May 2, 2010
Messages
219
Unfortunately this does not help, always known how to use .SelLenght etc. I use it a lot.
Me!OutDate1.SetFocus
Me!OutDate1.SelStart = 0
Me!OutDate1.SelLength = Len(Me!OutDate1.Text)
It is something about AfterUpdate & not moving control.
If it naturally Tabs to next control & back again it is highlighted automatically.

Also I always use the the setting AccessBlaster suggested, does not help.

I wonder if arnelgp function is safer and unlikely to start WWW3, it still uses sendkeys but the rest I do not really understand.
 

bignose2

Registered User.
Local time
Today, 08:20
Joined
May 2, 2010
Messages
219
Good job Trump still not in charge as that would be his next advise to beat covid,

A good gargle of bleach whilst executing a SendKey statement.

Back to SendKeys "+{TAB}", of course I realize what it is doing now, on moving to next control it Tabs back, did not fully appreciate the action and not too keen on it doing this, it actually runs whatever code is in that next control.

Might stick with..
Me!OutDate1.SetFocus
Me!OutDate1.SelStart = 0
Me!OutDate1.SelLength = Len(Me!OutDate1.Text)
does not highlight ready to type over but does stay on the control, cursor at start and you can use delete keys,
No real hardship here but on re-visiting for a different part of my program I thought I would try to once again.

I often use on a search box and want to replace/type a whole new word so don't want to be deleting each character. Actually on this project not so important.

Thanks for all you help.
 

Mike Krailo

Well-known member
Local time
Today, 04:20
Joined
Mar 28, 2020
Messages
1,030
What if you just press F2 when you need to highlight. F2 toggles between selecting and edit mode.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:20
Joined
Oct 29, 2018
Messages
21,358
I often use on a search box and want to replace/type a whole new word so don't want to be deleting each character.
Have you tried using the BeforeUpdate event instead? Just a thought...
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:20
Joined
Jan 23, 2006
Messages
15,364
As theDBGuy suggested --perhaps the BeforeUpdate event.

As I understand
in the AfterUpdate of the Outdate1 control I have ..
the record has already been processed/written.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:20
Joined
Oct 29, 2018
Messages
21,358
BeforeUpdate is a validation event.
you can't SetFocus on itself or any other control.
Correct; but, I was thinking the OP can Cancel the event to keep the focus on the same unbound control, since it's only used for searching anyway. To highlight the Text, maybe they can try Undo (only once). Again, it's just a thought...
 

DonMac

New member
Local time
Today, 08:20
Joined
Feb 22, 2017
Messages
3
Might stick with..
Me!OutDate1.SetFocus
Me!OutDate1.SelStart = 0
Me!OutDate1.SelLength = Len(Me!OutDate1.Text)
does not highlight ready to type over but does stay on the control, cursor at start and you can use delete keys,
No real hardship here but on re-visiting for a different part of my program I thought I would try to once again.

I often use on a search box and want to replace/type a whole new word so don't want to be deleting each character. Actually on this project not so important.

Thanks for all you help.
I've got the same problem. The VBA works as far as selecting the text (stepping through using F8) but as soon as the sub finishes the highlighting (selection) goes too.

I'm using it in a search box to filter customer names and addresses and after hitting Enter I'd like it to stay focussed on the highlighted search box so I can start another search without having to double click to re-select it.

Is there any way around this?
 

Mike Krailo

Well-known member
Local time
Today, 04:20
Joined
Mar 28, 2020
Messages
1,030
Let's see if I can post this now. The forum wasn't letting me post this code earlier in the week. You would need to make sure that KeyPreview property is set to YES in the forms properties so the keydown routine will be in control. I don't know why I cannot post this code in code tags, it just won't let me but here is the screen shot of it.

1615060811043.png
 

Users who are viewing this thread

Top Bottom