473,624 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A sendkeys/setfocus glitch?

44 New Member
Hi folks;
I spent a little while some time ago building a touchscreen data entry interface into an Access 2003 form. I'm not too experienced in VBA at all so I was quite happy when I came up with a reasonably elegant solution, especially given that there's multiple text boxes involved. Each of the "keys" on the screen keyboard is a cmd button with its letter as a nice big caption on the front. The Mouse Down event triggers a bit of code:

Function addletter()
Screen.Previous Control.SetFocu s
Screen.ActiveFo rm.ActiveContro l.SelStart = 0
SendKeys "({END})" & Screen.Previous Control.Caption
End Function

The whole SelStart and "({END})" thing is a workaround to remove the highlighting you get when you switch focus to a filled text box, since we all know what happens if you highlight a bunch of text and then press "a"....

The Spacebar button was a bit trickier, I worked the Mouse Down event to run the following:

Function addspace()
Screen.Previous Control.SetFocu s
Screen.ActiveFo rm.ActiveContro l = Screen.ActiveFo rm.ActiveContro l & " "
Screen.ActiveFo rm.ActiveContro l.SelStart = Len(Screen.Acti veForm.ActiveCo ntrol)
End Function

Still not quite sure how it works but the space at the end of the text stays there, which is the main thing.

BUT... I have a problem. Under normal typing it's fine, you can type words followed by a single space followed by another word and it works great. But if the user accidentally presses space twice then sometimes the focus goes back to the text box where it belongs but sometimes it stays on cmdSpacebar.. As far as I can tell it's totally random, there's no telling when it might do what. And when it does stay on cmdSpacebar you click a letter key it tries to do a SendKeys on the PreviousControl (being, in this case, cmdSpacebar) and the whole thing comes crashing down. Does anybody know what's going on here and how I can program this little bug out of my database?

Thanks!
Mar 16 '09 #1
6 4476
OldBirdman
675 Contributor
This sounds kinda complex. I've noticed many threads with comments about SendKeys, and that they should be avoided. Sometimes can't, but sometimes you can.

Here's an idea that seems to solve all your issues.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4.  
  5. Dim tbx As TextBox
  6. Dim strInputSpace As String
  7.  
  8. Private Sub cmdA_Click()
  9.     strInputSpace = strInputSpace & "A"
  10.     tbx = strInputSpace
  11. End Sub
  12.  
  13. Private Sub cmdB_Click()
  14.     strInputSpace = strInputSpace & "B"
  15.     tbx = strInputSpace
  16. End Sub
  17.  
  18. Private Sub cmdC_Click()
  19.     strInputSpace = strInputSpace & "C"
  20.     tbx = strInputSpace
  21. End Sub
  22.  
  23. Private Sub cmdSpace_Click()
  24.     strInputSpace = strInputSpace & " "
  25.     tbx = strInputSpace
  26. End Sub
  27.  
  28. Private Sub txtTextBox1_GotFocus()
  29.     cmdSmallControl.SetFocus
  30.     Set tbx = txtTextBox1
  31.     strInputSpace = Nz(txtTextBox1, "")
  32. End Sub
  33.  
  34. Private Sub txtTextBox2_GotFocus()
  35.     cmdSmallControl.SetFocus
  36.     Set tbx = txtTextBox2
  37.     strInputSpace = Nz(txtTextBox2, "")
  38. End Sub
cmdSmallControl is a command button 1 pixel square, visible, in the upper left corner of the form. It is used for SetFocus to keep any other control from appearing to have the focus.

Of course, you would standardize the keys into a function or subroutine. You can have as many textboxes as needed. A "Shift" key would be easy to impliment. Whole keyboard could change to UCase for one "keypress".

Don't know yet how to select portions of text, or position cursor, but arrow keys and double-click in textbox would be my start.
Mar 16 '09 #2
Whizzo
44 New Member
Sounds like it might work, but I wanted above all to avoid having a separate bit of code unique to each key on the on-screen keyboard. That way, if you have to make one change you have to make 26! The way I do it (with the key caption) means I can have the same macro on every key that runs the same bit of code. As a whole it works perfectly apart from that damned spacebar bug! I think I'll see if I can do it with straight coding on cmdSpace rather than going via a macro, maybe that's the problem... Thanks though, it certainly helps to look in different directions!.
Mar 16 '09 #3
OldBirdman
675 Contributor
I had figured that you would write a subroutine and each letter would call that common subroutine. Same code for all keys. Or you could capture the event through key preview for the form.

I do remember somewhere a tricky way to have Access treat multiple commands as an array of controls. Maybe someone else remembers that if you are interested.
Mar 16 '09 #4
Whizzo
44 New Member
All input gratefully received! I have managed to draw a bit more of a bead on the problem. If you click the cmdSpacebar slowly (once a second) you can keep going 'til the cows come home. The problem only crops up if you click it rapidly, a bit faster than a normal double-click that is. Is there a way of maybe Calling the event or using DoEvents?
Mar 16 '09 #5
FishVal
2,653 Recognized Expert Specialist
@OldBirdman
Here was one.

Regards,
Fish.
Mar 16 '09 #6
Whizzo
44 New Member
Fixed! I just changed the event that fires the procedure from On Mouse Down to Got Focus. As a bonus, I no longer get that nasty flicker on the button as it's clicked then suddenly has the focus snatched away from it. Cheers folks!
Mar 16 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2997
by: Ted Mayett | last post by:
OK. Here is a glitch, sorry if this has been mentioned before. This is an erratic glitch. I am now up to three other people besides myself who have been able to see this glitch. It seems it only happens in IE. And the <hr> causes this thing to happen. Now, I have ~tried~ to make this glitch, and that is tough to do. I had did it that one time, and I should have saved the work. But I didn't save the work :(
0
460
by: Mr. Bungle | last post by:
I would like to send email automatically via a command button. I have accomplished this just fine through the following code: (Outlook should already be open for reliable results) Private Sub Command6_Click() ReturnValue = Shell("Outlook.EXE", 1) ' Run Outlook SendKeys "%(F)", True SendKeys "W", True SendKeys "M", True 'Open New Message SendKeys "randomemail@harbornet.com", True '<-- Email Address
2
5690
by: RBohannon | last post by:
I need to create a report in MS Word populated with data from A2K. I have been asked to create the report in Word so that parts of it can be edited as necessary later. The data in the report are in a table with headings for each column, so converting an Access report to a Word doc in RTF doesn't work because only the text is converted, not the table. What I have tried so far, with some success, is to write the report directly in Word....
1
9535
by: George | last post by:
Every time I used the Sendkeys command in my application the "Numlock" turned off and I couldn't use the keypad to hit numbers...... The old code was: Private Sub Command1_Click() SendKeys "{DOWN}" SendKeys "^{END}" SendKeys "{TAB}" End Sub
5
8749
by: Wayne Gibson | last post by:
Hi, Was wondering if somebody could help.. I'm trying to use Sendkeys on a Windows forms. I have entered the following command to simulate a CTRL+ALT+1.. System.WindowsForms.SendKeys.Send("^%1"); This didn't work, so I then tried..
1
7713
by: Bryan | last post by:
I am writing a C# Windows App that updates out Excel reports' modules. The app is complete, but has a problem. The only way MS allows you to unprotect the VBA code in Excel is to do it by hand or sendkeys. I hate using SendKeys, but I am forced to use them. Anyway, I have written a function that will unprotect the VBA code in the current work book. It works about 60% of the time. For some reason it gets out of sync and the SendKeys don't...
1
27235
by: GrantS | last post by:
I need to use a sendkeys key combination to automate the "accept files" that a remote user wants to send to me via Windows messenger. I am using automation to work with Windows Messenger client in a Winforms C# application. I have tried a number of code segment combinations for ALT T combination without success. The combinations might be correct but it might be failing for some other reason. My Attempts:
6
3315
by: Michael Maes | last post by:
Hello, I'm invoking successive SendKeys.SendWait("{BACKSPACE}") SendKeys.SendWait("{DELETE}") in a loop. The issue is that it causes a Beep on every Pass. Setting the 'Handled = True' on the Control does prevent the Beep, but
0
2757
by: dtshedd | last post by:
I have a database with hundreds of embedded photos (Microsoft Photo 3.0) Many are larger than 1 MB. I tried Stephen Lebans macro but it did not work probably for the aforementioned reasons Now I am trying to use the copy portion of Stephens macro and then open MS Paint using Sendkeys to Paste and save. The Sendkeys method is opening the Paint app but not executing the paste command. Wondering what I am doing wrong. I have only ever...
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.