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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Key combination to auto enter text 3

Status
Not open for further replies.

VickyC

Technical User
Sep 25, 2010
206
CA
hello to all

Lets say users frequently have to enter a text string (say, "Acme Products Incorporated of America") into a textbox. (for various reasons, I don't want to use a combo box here.) I'd like the key combination SHIFT + ALT + "A" to automtically enter this string at the cursor's location.

How could this be coded?

Thank you
Vicky
 
Try the KeyUp event. This picks up the code for the key as one parameter and the state of Ctrl, Shift and Alt as the second. Your Shift+Alt+A will give 65 as KeyCode and 5 as Shift (1 for Shift + 4 for Alt). The easiest way to get started might be to use Debug.Print to see what codes are being detected:
Code:
Private Sub txtCompany_KeyUp(KeyCode As Integer, Shift As Integer)
Debug.Print KeyCode, Shift
End Sub

Geoff Franklin
 
How are ya VickyC . . .

In form design view:
[ol][li]Set the [blue]Key Preview[/blue] property (on the events tab) to [blue]Yes[/blue].[/li]
[li]Copy/paste the following to the [blue]On Key Down[/blue] event:
Code:
[blue]   If Shift = 5 And KeyCode = vbKeyA Then [green]'Shift + Alt + A[/green]
      Me![purple][B][I]TextboxName[/I][/B][/purple] = "Acme Products Incorporated of America"
   End If[/blue]
[/li][/ol]

[blue]Your Thoughts? ...[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks to TheAceMan1 and alvechurchdata for very informative answers! I put the following code into the form's On KeyDown event, and it worked great:

Code:
If Shift = 5 And KeyCode = vbKeyA Then 'Shift + Alt + A      
   Me!TextboxName = "Acme Products Incorporated of America"
End If

I'd like to ask a variation of the same question: Can this same approach be used to auto enter text into the textbox of an INPUT box? (I'd try, but I don't know how to reference the INPUT Box's textbox??? How would I find its name when Design View in not available?)

 
Well, perhaps it doesn't work with an input box either but using Autokeys can help you maintain all of it in one place instead of the key down events.

Bob Larson
Free Access Tutorials and Samples:
 
No but you can roll your own input box.
1)build a dialog form with a text box, label, OK button, close button.
2)Code on the form
Code:
Private Sub cmdCancel_Click()
  DoCmd.Close acForm, Me.Name
End Sub
Private Sub cmdOK_Click()
  Me.Visible = False
End Sub
Private Sub Form_Load()
  If IsNull(Me.OpenArgs) Then
    MsgBox "Requires Openargs. Closing form."
  Else
     Me.lblMessage.Caption = Split(Me.OpenArgs, ";")(0)
     If Split(Me.OpenArgs, ";")(1) <> "" Then
       Me.Caption = Split(Me.OpenArgs, ";")(1)
     End If
     Me.txtBxInput = Split(Me.OpenArgs, ";")(2)
  End If
End Sub

3)Put a function in a standard module
Code:
Public Function myInputBox(Prompt As String, Optional Title As String = "", Optional Default As Variant = "") As Variant
   Dim strArgs As String
   Dim frm As Access.Form
   strArgs = Prompt & ";" & Title & ";" & Default
   DoCmd.OpenForm "frmInputBox", , , , , acDialog, strArgs
   If CurrentProject.AllForms("frmInputBox").IsLoaded Then
     Set frm = Forms("frmInputBox")
     myInputBox = frm.txtBxInput
     DoCmd.Close acForm, frm.Name
   End If
End Function

Public Sub testMyInput()
  Debug.Print myInputBox("Prompt Message", "My Caption", "Default Value")
End Sub
 
VickyC . . .
VickyC said:
[blue] ... Can this same approach be used to auto enter text into the textbox of an INPUT box?[/blue]
Considering that the entry in an inputbox is typically stored in a VBA variable, it would be all possible to [blue]assign a value[/blue] to the variable instead!

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top