Avatar of PeteyBgood
 asked on

Using VBAs SendKeys in a Multiline Text Box {HOME} doesn't go to the beginning of the line

Instead it goes to the beginning of the field like Ctl -Home. I'm trying to do some fancy editing with shortcut keys, but didn't get far. Hitting the HOME key does as expected but
     SendKeys "{HOME}"
does not ... what gives? And how do I work around it. This is driving me nuts.
Microsoft Access

Avatar of undefined
Last Comment

8/22/2022 - Mon

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Well that seems to work, so I'll work with it a while. I still don't understand why SendKeys' behavior is different from a macro that sending the keys from VBA..  Wait a sec, I'm using ^Y to do a {HOME+{END} (select line) but because  the control key is down when the keys are sent, it thinks it's getting a ^{HOME}^+{END}. THAT'S why it selects the whole document, so if I move the code to the KeyUp event... I'll be back
Scott McDaniel (EE MVE )

Also be aware that Sendkeys is notorious fickle and prone to failure. Most professional applications avoid it like the plague.
Jeffrey Coachman


" Hitting the HOME key does as expected..."
If hitting the Home key works, then why even bother with Sendkeys or an autokeys macro?

As far as "why" sendkeys does not work, is probably becaues it is looking for Line feeds or Carrige Returns to signify the start of a line.
If your text has none, it simply goes to the start of the field.

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

Hitting the 'Home' key would be easier, but then that's not what automation is all about. I need to define procs that can do things like "Delete to EOL" or determine if I'm on the first or last lines. Since Access doesn't provide full access to the text box's info like "Current Line", "Line Count" , word wrap info etc. I'm trying to work around it with SendKeys. I've played with it and you're right, the macro approach works. I found something in the dox about how only macros can cancel keyboard events, so my DoCmd.CancelEvents in the KeyDown event handler did nothing which explains why {HOME} was processed as ^{HOME} when ^{whatever } is pressed.

Oh yeah, and it doen't have to be an autokeys macro, SendKeys in any macro can be called from VBA without the current keyboard state interfering.

IN FACT You don't even have to send any keys, using "" as the SendKeys argument in the macro does the same thing (you can't just leave the argument blank you have to deliberately tell it to send nothing. This seems a little kludgey, but I've been guilty of lots worse. I consider this matter closed, and you'll have your points by day's end, unless somebody else can up with something better.So to tell ^Y to blank a line without remove the CRLF:
Private Sub ..... _KeyDown (...)
   Select Case KeyCode
      Case vbKeyY
         DoCmd.RunMacro "Skeys.FlushKbd"
         SendKeys ( "{HOME}+{END}{DEL}")
where Skeys is my macro group and FlushKbd is a macro that does SendKeys "",False
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.

I think I expect too much out of software that says it ca do it all.