Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Sep 2002
    Location
    London, England
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Access text in bound textbox (2000)

    I'm having trouble getting to the text that is currently displayed in a bound text box. I'm trying to provide a keyboard shortcut for users to insert @ signs into email addresses on PCs that do not have full keyboards. I'm using onKeyPress to trap a control + key combination (I've selected Ctl+E as all the obvious ones have gone). The control in question is called txtEmail and is bound to a text field. In the KeyPress event I use

    strEmail = Me.txtEmail
    strEmail = strEmail & "@"
    Me.txtEmail = strEmail
    Me.txtEmail.SelStart = Len(Me.txtEmail)

    This adds the @ sign, deselects the text then places the insertion point at the end of the new string ready for the rest of the email. However, if there is already an entry in this field, which has to be changed, when the user deletes the previous entry, enters the new address and presses Clt+E, the old entry reappears and the new entry is lost.

    Obviously for fields that already contain data Me.txtEmail is looking at the saved entry in the table. Is there a way of referencing the current contents of the textbox rather than the contents of the bound field?

    Ian

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Access text in bound textbox (2000)

    <hr>referencing the current contents of the textbox rather than the contents of the bound field<hr>
    When you are editing, you are addressing the current contents rather than the field. The changes aren't written back to the field until the AfterUpdate event of the control. You only posted a snippet of code. Is there more after that?
    Charlotte

  3. #3
    3 Star Lounger
    Join Date
    Sep 2002
    Location
    London, England
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Access text in bound textbox (2000)

    Not much more, this is the lot.

    Private Sub txtEmail_KeyPress(KeyAscii As Integer)
    ' Provide @ sign. About the only spare Ctl-Key combination left that makes
    ' any sense is Ctl-E (for e-mail, geddit)

    Const TrapKey = 5
    Dim strEmail As String

    If KeyAscii = TrapKey Then
    strEmail = Me.txtEmail
    strEmail = strEmail & "@"
    Me.txtEmail = strEmail
    Me.txtEmail.SelStart = Len(Me.txtEmail)
    End If

    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Access text in bound textbox (2000)

    When you refer to Me.txtEmail without specifying a property, Access silently uses the default property of the text box, i.e. the stored Value. To refer to the uncommitted text currently being entered, use the Text property:

    Me.txtEmail.Text

    But you can't use Ctrl+E, for the KeyPress event only reacts to "character" keys, not to function keys or combinations with Ctrl etc. You must use the KeyDown (or KeyUp) event to handle Ctrl+key combinations. Try this:

    Private Sub txtEmail_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyE And (Shift And acCtrlMask) > 0 Then
    ' Insert @
    txtEmail.Text = txtEmail.Text & "@"
    txtEmail.SelStart = Len(txtEmail.Text)
    ' Prevent further handling of keystroke
    KeyCode = 0
    End If
    End Sub

  5. #5
    3 Star Lounger
    Join Date
    Sep 2002
    Location
    London, England
    Posts
    294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Access text in bound textbox (2000)

    Thanks for the tip Hans, the Text property did the trick.

    However (1) I think you're wrong about KeyPress, which does allow combinations of letters with the Control key.

    However (2) It won't allow combinations with the Alt key. To put in an @ sign it would make more sense from a user's point of view to use something like Alt-A. How do I modify your code to do that? Does

    If KeyCode = vbKeyE And (Shift And acCtrlMask) > 0 Then

    become

    If KeyCode = vbKeyA And (Shift And acAltMask) > 0 Then

    ?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Access text in bound textbox (2000)

    Hi Ian,

    You're quite right about KeyPress being able to handle Ctrl+letter combinations. Somehow, it escaped my attention - it is mentioned in the online help.

    However, KeyPress can't handle Alt, Ctrl+Alt, Shift+Alt, Ctrl+Shift etc. You need KeyDown or KeyUp for that. The Shift parameter can consist of any combination of acShiftMask, acAltMask and acCtrlMask. To test if one of these applies, test if (Shift and ac...Mask) > 0.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •