Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automation of VB enter key

Status
Not open for further replies.

krieg313

Technical User
May 5, 2011
22
0
0
US
I'd like to have the code press enter after a certain amount of characters have been entered in a textbox.

This is what i have but it doesn't work.

Private Sub Text0_KeyDown(Keycode As Integer, shift As Integer)

'Dim str As String
Dim str As Object
Set str = Text0
'str = "1234567"
If Len(str) >= 7 Then
Keycode = vbreturnkey
End If


End Sub
 
Do you have a button on the form that the user clicks (acts as 'Enter') or is it just validation? What exactly happens when you click 'Enter'?

In this case you cannot set a keycode. Keycode is used to detect which button has been pressed ... ie. If Keycode = VBReturnKey Then ...

If you have a button that acts as on OK button on the form, you could use something like:
Code:
Private Sub Text0_KeyDown(Keycode As Integer, shift As Integer)

   Dim str As Object
   Set str = Trim$(Text0)  ' Remove any leading or trailing spaces.

   If Len(str) = 7 Then
      btnOK_Click
   End If
End Sub

Or if you do not have a button, then you can call the text box validation code. Please note that usually when the 'Enter' button is pressed, the focus is set to something else. If this is the case, then set the focus to something else and the validation code is automatically called.

As mentioned above, it depends on what you want the enter button to do.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Thanks for the reply.
I've tried the snippet above but it gives off error Object Required, As for Calling the validation Text box code, well i dont know how to that.
what im looking for is right after the 7th key is pressed BtnOk would be called.

 
The reason I was asking what the enter key is doing is because that would depend on what happens next.

When the enter key is pushed:
- Are you saving the value?
- Are you manipulating the value?
- Are you loading or updating information in a database?
- Are you closing a form or opening a new form?
- Are you passing this value to something else?
- Are you pasting this value in another text box?
...

The validation is another event that is associated with the textbox just like the key down event. I primarily use this when someone types in a number (say a custom invoice number) and presses enter or tab. I then use that information to verify that the invoice number exists in the database and load all associated information pertaining to that invoice onto the form.

Code:
Private Sub txtCustomer_KeyPress(KeyAscii As Integer)
   If KeyAscii = vbKeyReturn Then  '// Enter key pressed.
      txtCustomer_Validate False
      KeyAscii = 0
   End If
End Sub

Private Sub txtCustomer_Validate(Cancel As Boolean)
   On Error GoTo ERR_Handler
   
   If Trim$(txtCustomer.Text) <> ""  Then
      ...
   Else
      Cancel = True
   End If

...
End Sub

If at first you don't succeed, then sky diving wasn't meant for you!
 
Sorry for not making my post clear.
Ok,
- Are you saving the value? Yes, I am saving it to String LInum.
- Are you manipulating the value? No.
- Are you loading or updating information in a database? No. The Input from the user is used to query a website.
- Are you closing a form or opening a new form? No.
- Are you passing this value to something else? To Sub Command3()

- Are you pasting this value in another text box? No.


* User inputs 7 numbers (0-9)
* After the 7th key is pressed Command3 is called.
** Also is there a way for Access to detect the keystrokes in real time?

Private Sub Command3_Click()

Call LogIn ' Logs in to Website
Dim LInum As String

Set ie2 = CreateObject("InternetExplorer.Application")
ie2.navigate " & LInum & "&Type=Job"
'LInum is used above to query an specific page.
ie2.Visible = True
While ie2.Busy
DoEvents
Wend

'...

End Sub



Sub LogIn()

Const strUsr_c As String = "qcc04"
Const strPwd_c As String = "qc"


Dim ABC As MSHTML.HTMLInputElement
Dim DEF As MSHTML.HTMLInputElement
Dim ABC1 As MSHTML.HTMLInputElement
Dim DEF2 As MSHTML.HTMLInputElement
Dim btnSubmit As MSHTML.HTMLInputElement
Dim btnSubmit1 As MSHTML.HTMLInputElement

Set iekickoff = CreateObject("InternetExplorer.Application")
iekickoff.navigate "
While iekickoff.Busy
DoEvents
Wend

Set ABC1 = iekickoff.Document.all.Item("loginidtextbox")
Set DEF2 = iekickoff.Document.all.Item("passwordtextbox")
Set btnSubmit1 = iekickoff.Document.all.Item("logonButton")

ABC1.Value = strUsr_c
DEF2.Value = strPwd_c
btnSubmit1.Click

While iekickoff.Busy
DoEvents
Wend

Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "
While ie.Busy
DoEvents
Wend

Set ABC = ie.Document.all.Item("loginidtextbox")
Set DEF = ie.Document.all.Item("passwordtextbox")
Set btnSubmit = ie.Document.all.Item("logonButton")

ABC.Value = strUsr_c
DEF.Value = strPwd_c
btnSubmit.Click

ie.Visible = 1

End Sub
 
I noticed a few things. First of all, the value of the textbox is not being saved and you are passing a blank value in the navigation address.

There are a couple of ways you can approach this. You can save the value then call Command3_click and use the value (for this you must declare the value outside of Command3_Click
Code:
'At the top of the code'
Option Explicit

Private m_strLInum as string
...

Private Sub Text0_KeyPress(KeyAscii As Integer)

   If Len(Trim$(Text0.text)) >= 7 Then
      m_strLInum = trim$(m_strLInum)
      Command3_Click
   End If
End Sub


Private Sub Command3_Click()
   ...
   ie2.navigate ... & m_strLINum ...
End Sub

Another method would be to pass the text box value as a parameter:
Code:
Private Sub Text0_KeyPress(KeyAscii As Integer)

   If Len(Trim$(Text0.text)) >= 7 Then
      Command3_Click Trim$(Text0.text)
   End If
End Sub


Private Sub Command3_Click(pLINum as String)
   ...
   ie2.navigate ... & pLINum...
End Sub

Or you can obtain the text box value in the command click event
Code:
Private Sub Text0_KeyPress(KeyAscii As Integer)

   If Len(Trim$(Text0.text)) >= 7 Then
      Command3_Click
   End If
End Sub


Private Sub Command3_Click()
   ...
   ie2.navigate ... & Trim$(Text0.Text) ...
End Sub

I would also suggest naming your textboxes and command buttons to make it easier to know what you are referencing.

If at first you don't succeed, then sky diving wasn't meant for you!
 
I forgot to mention that if you want to have Access detect the keystrokes, you would then need to use VBA programming.

Here is the VBA forum:
forum707

If at first you don't succeed, then sky diving wasn't meant for you!
 

Have you tried:

Code:
Option Explicit

Private Sub Text1_Change()

If Len(Trim(Text1.Text)) = 7 Then
    Text1.Text = Text1.Text & vbNewLine
    Text1.SelStart = Len(Text1.Text)
End If

End Sub

Set the Text1 MultiLine to True

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top