Link to home
Start Free TrialLog in
Avatar of PeteyBgood
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.
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeteyBgood
PeteyBgood

ASKER

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
Also be aware that Sendkeys is notorious fickle and prone to failure. Most professional applications avoid it like the plague.
PeteyBgood,

" 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.

JeffCoachman
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
 
I think I expect too much out of software that says it ca do it all.