Solved

Sendkeys  to  combobox wont work

Posted on 1998-05-06
20
685 Views
Last Modified: 2012-06-21
Using Sendkeys to a combobox on a another form wont work,
ok for everything else, only first character is shown
following Sendkeys are replace over top of first position
Im currently using a mirrored textbox, but is there a way?
THANK YOU IN ADVANCE SORRY ABOUT THE LOW POINTS
0
Comment
Question by:vern12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 4
  • +1
20 Comments
 
LVL 7

Expert Comment

by:tward
ID: 1442127
It might help if you show some of the code that you are using.  I believe you can sendkeys a whole string if needed and maybe that would work for you if you are doing it one at a time.
0
 

Author Comment

by:vern12
ID: 1442128
Yes I can send strings, nothing wrong with code works everywhere
else, something special about combobox, ive tried various property settings, anybody?
0
 
LVL 18

Expert Comment

by:deighton
ID: 1442129
I had no problems with the following, hope it helps

Private Sub Command1_Click()

    Form1.Combo1.SetFocus
    SendKeys "hello"

End Sub

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 18

Expert Comment

by:deighton
ID: 1442130
Heres a bit more - if you put wait = true as shown it works better

Private Sub Command1_Click()

    Form1.Combo1.SetFocus
    'use wait  = true to ensure success
    SendKeys "hello", True
    Form2.SetFocus

End Sub

0
 

Author Comment

by:vern12
ID: 1442131
thanks deighton, I can send strings too
problem is if you send one character or string with sendkey and then another , the second overwrites the first, this does not happen with a textbox only a combobox.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1442132
Ok i'll set to work on it - answer to follow!
0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442133
vern12,

The following code will add a randome characters to combo1 on form2.

Do the following:

1 - Add form1 and form2 to project1.
2 - Add command1 to form1.
3 - Add combo1 to form2.
4 - Paste this code in form1:

Private Sub Form_Load()
 Form2.Show
End Sub

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys "{END}" & Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub


Best regards
Bin Huwairib
0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442134
vern12,

I forget to mention that one of combo box behavior is that text will be selected automatically when the control got the focus, that is why the next character will always replace the pervious one.

If you would like to get ride of {END} in SendKeys method as another solution, you have to add the following code in the combo box GotFocus event in form2:

Private Sub Combo1_GotFocus()
 Combo1.SelStart = Len(Combo1.Text)
 Combo1.SelLength = Len(Combo1.Text)
End Sub

And form1 code will look like this:

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

Private Sub Form_Load()
 Form2.Show
End Sub


Best regards
Bin Huwairib
0
 

Author Comment

by:vern12
ID: 1442135
Thank you bin huwairib
RIGHT,LEFT,DEL,INS, could not work with your example.
If I use the mouse to click 4th position of comdobox text
focus (and following SendKeys) is sent to end of text
not 4th position. Help.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1442136
Hope this code fragment helps - if you have focus don't go to end


   If Form1.ActiveControl <> "combo1" Then
   
        Form1.combo1.SetFocus
       
    End If
   
    SendKeys "{end}" + "h", True
       
    If Form1.ActiveControl <> "combo1" Then
   
        Form1.combo1.SetFocus

    End If
   
    SendKeys "{END}" & "e"
0
 

Author Comment

by:vern12
ID: 1442137
deighton, this would force combo1 to have focus always
yes... but the same problem persists. thank you
0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442138
vern12,

Do the following:

1 - Add form1 and form2 to project1.
2 - Add module1 to project1.
3 - Add command1 to form1.
4 - Add combo1 to form2.
5 - Add timer1 to form2 then set Interval property to 0 and set Enabled property to False.

===== Paste this code in form1:

Private Sub Form_Load()
 Form2.Show
End Sub

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

===== Paste this code in form2:

Private Sub Combo1_GotFocus()
 Combo1.SelStart = S
 Timer1.Enabled = True
End Sub

Private Sub Combo1_LostFocus()
 Timer1.Enabled = False
End Sub

Private Sub Command1_Click()
 Combo1.SetFocus
 Randomize
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

Private Sub Timer1_Timer()
 S = Combo1.SelStart
End Sub

===== Paste this code in module1:

Public S As Integer


Best regards
Bin Huwairib
0
 

Author Comment

by:vern12
ID: 1442139
bin
string ends up inverted, right , left, tab does not work
also can`t know position of last mouse click, say to start at
position3
I've tried looking for combo1.setposition or something
Thank YOU  so much ,
vern, hey I got 5 xtra point today,


0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442140
vern12,

What are you talking about?

Don't get confused, check my recent answer.

This example is working fine with me I'm sure that you miss somthing, please tell me exactly what you did.


Bin Huwairib
0
 

Author Comment

by:vern12
ID: 1442141
Ok Bin , using your code  if I sendkeys {right} instead of rnd
pointer does not move right, thanks,
0
 
LVL 5

Accepted Solution

by:
bin_huwairib earned 30 total points
ID: 1442142
vern12,

By mistake I include Private Sub Command1_Click() event twice in form1 and form2, please remove the one in form2 and change the one in form1 to the following:

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys "{Right}"
End Sub

This will do you job definitely.


Best regards
Bin Huwairib
0
 

Author Comment

by:vern12
ID: 1442143
bin any keys sent by your solution always go to the end of the text string within the combo box and not my last pointed position from either a mouse click or direction keys (be it the keyboard or by Senkeys), that is I cannot add text to the center of the combot box string, as i can when sending the keys to a textbox.  

please note in the code to follow form2.combo1.setfocus
is replaced with form2.setfucus to remember our last focus,
add a text box to the second form. Now we can click on the combo box or a textbox to send keys, and see the difference for
yourself. Ive tried variations of {end} but I cant correct this.
 
Using a keyboard I can add  text to the 3rd position within a string of a combo box, but not with a mouse click and sendkey
which works with the text box perfectly. I enclosed the code
for you to see. Please check this behaviour.
Thank Kindly vern12

Private Sub Form_Load()
 Form2.Show
End Sub
Private Sub Command1_Click()
 'Form2.Combo1.SetFocus
 Form2.SetFocus
 Randomize
 'SendKeys "{END}" & Chr(Int((72 - 50 + 1) * Rnd + 50))
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

Private Sub Command2_Click()
 'Form2.Combo1.SetFocus
 Form2.SetFocus
 'SendKeys "{END}" & "{Right}"
 SendKeys "{Right}"
End Sub

Private Sub Command3_Click()
 'Form2.Combo1.SetFocus
 Form2.SetFocus
 'SendKeys "{END}" & "{Left}"
 SendKeys "{Left}"
End Sub

Private Sub Command4_Click()
 'Form2.Combo1.SetFocus
 Form2.SetFocus
 Randomize
 'SendKeys "{END}" & Chr(Int((72 - 50 + 1) * Rnd + 50))
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

'form2
Private Sub Combo1_GotFocus()
 Combo1.SelStart = Len(Combo1.Text)
 Combo1.SelLength = Len(Combo1.Text)
End Sub
   
0
 

Author Comment

by:vern12
ID: 1442144
Adjusted points to 30
0
 
LVL 5

Expert Comment

by:bin_huwairib
ID: 1442145
vern12,

I think that you are really confused, becuase if you had looked at my second answer you will notice that I've added a Timer1_Timer and Combo1_GotFocus and Combo1_LastFocus events in form2 to keep track for cursor position on combo1, I will repost my answer again, so please read it carefully this time.

== Paste this code in form1:
Private Sub Form_Load()
 Form2.Show
End Sub

Private Sub Command1_Click()
 Form2.Combo1.SetFocus
 Randomize
 SendKeys Chr(Int((72 - 50 + 1) * Rnd + 50))
End Sub

Private Sub Command2_Click()
 Form2.Combo1.SetFocus
 SendKeys "{Right}"
End Sub

Private Sub Command3_Click()
 Form2.Combo1.SetFocus
 SendKeys "{Left}"
End Sub

== Paste this code in form2:
Private Sub Combo1_GotFocus()
 Combo1.SelStart = S
 Timer1.Enabled = True
End Sub

Private Sub Combo1_LostFocus()
 Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
 S = Combo1.SelStart
End Sub

== Paste this code in module1:
Public S As Integer


Best regards
Bin Huwairib
0
 

Author Comment

by:vern12
ID: 1442146
bin, sorry about my confusion, thanks for you patience and repost, and most of all the excellent answer, vern
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using web browser with BING 40 150
VBA Shell can't Find Word document 11 155
TT Auto Dashboard 13 104
VBA - If Bookmark = "XXBOOKMARKXX" then 15 67
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question