Automatically insert text from one text box on a subform to an unbound text box on a form as typing

  • Oh right, sorry, I misread your post. So I wouldn't be able to do something like I said then as no event is fired? Thats rather annoying!

  • In the same vein as "No ticky, no washy" ...

    "No keypress event for the DELETE key, no way to keep other text box the same."

    Oh well...:w00t:

    Steve

    (aka smunson)

    :):):)

  • Maybe I shouldn't give up so easily. Turns out the Key Down event DOES FIRE for the DELETE key, so there's likely to be a solution using the following code as a starting point:

    Private Sub TYPING_BOX_KeyDown(KeyCode As Integer, Shift As Integer)

    Debug.Print "KeyCode = " & KeyCode

    Debug.Print "Shift = " & Shift

    End Sub

    Private Sub TYPING_BOX_KeyPress(KeyAscii As Integer)

    Dim Pos As Integer

    Pos = Me!TYPING_BOX.SelStart

    Me!MIRROR_BOX.SetFocus

    Me!TYPING_BOX.Enabled = False

    Me!TYPING_BOX.Enabled = True

    Me!TYPING_BOX.SetFocus

    If KeyAscii > 27 Then

    If IsNull(Me!MIRROR_BOX) Then

    Me!MIRROR_BOX = Chr(KeyAscii)

    Else

    If Pos = 0 Then

    Me!MIRROR_BOX = Chr(KeyAscii) & Me!MIRROR_BOX

    Else

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Pos) & Chr(KeyAscii) & Right(Me!MIRROR_BOX, Len(Me!MIRROR_BOX) - Pos)

    End If

    End If

    Else

    If KeyAscii = 8 Then

    If Me!TYPING_BOX.SelStart = Len(Me!MIRROR_BOX) Then

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Len(Me!MIRROR_BOX) - 1)

    Else

    If Pos = 0 Then

    Me!MIRROR_BOX = Me!TYPING_BOX

    Else

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Pos - 1) & Right(Me!MIRROR_BOX, Len(Me!MIRROR_BOX) - Pos)

    End If

    End If

    End If

    End If

    If IsNull(Me!TYPING_BOX) <> True Then

    Me!TYPING_BOX.SelStart = Len(Me!TYPING_BOX.Value)

    End If

    End Sub

    The key down event is just showing you what the variable values are for right now, so you may just have to split your code for the DELETE key into that procedure instead of the other one. There's something fishy going on when you add a space to the control, though, and I haven't figured that out yet. It seems to be adding the space correctly to the MIRROR_BOX, but the TYPING_BOX isn't seeing it in the right place, and I'm pretty sure it's a problem with what's happening with the SelStart for it, so that may need to be changed or eliminated.

    Steve

    (aka smunson)

    :):):)

  • Once again, thanks Steve, with your code and some modifications, I think I may have done it :D. I haven't fully tested it, as it getting a little late (1:30am, update - actually 1:45am now), but some tests (adding text, deleting and backspacing from various cursor positions) that I have done seem to suggest it is working. I will fully test this later today. Here is the code I have now, with some highlighted points (in the form of vba comments):

    Option Compare Database

    Option Explicit

    Dim Pos As Integer

    Private Sub DeleteButton()

    'Check to see if there is any text in both boxes.

    'Might need to check to see if both text boxes are the same length?

    If Len(Me!TYPING_BOX) <> 0 And Len(Me!MIRROR_BOX) <> 0 Then

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Pos) & Right(Me!MIRROR_BOX, (Len(Me!MIRROR_BOX) - Pos) - 1)

    End If

    End Sub

    Private Sub Form_Open(Cancel As Integer)

    'Have this Form_Open procedure as I made a table and set TYPING_BOX to a field in that table.

    'The table simply has a field called id (unique, number data type) and a field called typingbox (text data type).

    If Len(Me.TYPING_BOX) > 0 Then

    Me.MIRROR_BOX = Me.TYPING_BOX

    End If

    End Sub

    Private Sub TYPING_BOX_KeyDown(KeyCode As Integer, Shift As Integer)

    Pos = Me!TYPING_BOX.SelStart

    'See if KeyCode is the Delete button

    If KeyCode = 46 Then

    DeleteButton

    Exit Sub

    End If

    Debug.Print "KeyCode = " & KeyCode

    Debug.Print "Shift = " & Shift

    End Sub

    Private Sub TYPING_BOX_KeyPress(KeyAscii As Integer)

    Pos = Me!TYPING_BOX.SelStart

    Me!MIRROR_BOX.SetFocus

    Me!TYPING_BOX.Enabled = False

    Me!TYPING_BOX.Enabled = True

    Me!TYPING_BOX.SetFocus

    If KeyAscii > 27 Then

    If IsNull(Me!MIRROR_BOX) Then

    Me!MIRROR_BOX = Chr(KeyAscii)

    Else

    If Pos = 0 Then

    Me!MIRROR_BOX = Chr(KeyAscii) & Me!MIRROR_BOX

    Else

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Pos) & Chr(KeyAscii) & Right(Me!MIRROR_BOX, Len(Me!MIRROR_BOX) - Pos)

    End If

    End If

    Else

    If KeyAscii = 8 Then

    'Added in Pos <> 0, as when backspacing whilst at the start of the text box, an error appeared stating

    '"Invalid procedure call or arguement".

    If Pos <> 0 And Me!TYPING_BOX.SelStart = Len(Me!MIRROR_BOX) Then

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Len(Me!MIRROR_BOX) - 1)

    Else

    If Pos = 0 Then

    Me!MIRROR_BOX = Me!TYPING_BOX

    Else

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Pos - 1) & Right(Me!MIRROR_BOX, Len(Me!MIRROR_BOX) - Pos)

    End If

    End If

    End If

    End If

    'Changed below to equal Pos, as it was setting the cursor position to the end, when need to set it to the same place.

    If IsNull(Me!TYPING_BOX) <> True Then

    Me!TYPING_BOX.SelStart = Pos 'Len(Me!TYPING_BOX.Value)

    End If

    End Sub

    Well I hope you will be able to make use of this, I know I will make use of this other than in the current project I am working on at work :).

    If only there was some way of rewarding you with some bonus SQL Server Central Points or something. Will a :beer: (imaginary beer icon needed Steve Jones :hehe: ) suffice ;).

    Cheers (pun totally intended 😎 ),

    Andrew

  • Ok, I have it working now for the subform/main form. I have two problems which I will separate below, as it has turned out to be quite lengthy, which I apologise for!

    SPACE PROBLEM:

    The SPACE is proving to be a pain! At the moment, if I type in (note: the quotes following aren't entered, just to show what was entered) 'test', and then set a breakpoint on the keypress event and press space, and then I remove the breakpoint and type in 'this', 'test this' appears in both the TYPINGBOX and MIRRORBOX correctly. But if I type in 'test' and then a space, when space has been pressed 'test' disappears and I am left with a space.

    I did have it before as you said where if you press a space, it seems to appear in the MIRRORBOX but not the TYPINGBOX.


    DELETE PROBLEM

    For the delete button, I thought of a scenario where a user may hold the shift button down to select multiple characters to delete, but I am as yet to get that working. What I have done at the moment is set up a global variable to store the cursor position, and then another to store the previous cursor position, but what I came to realise is that when you are pressing the shift button and the left button for example, the left button is making the previous cursor variable change.

    I don't think I can easily test if the user has pressed shift and is selecting text, as if I were to set up a boolean variable to see if the shift button is pressed, set this to true and set the previous variable to the previous cursor position, then as the user will be holding the shift button down, it will continue to change the previous cursor position. I don't think it will be possible to do this with some sort of timer either, because as soon as the user presses the right or left button, it breaks the shift, recognises the right/left press, and then starts the shift process again.


    What do you think will be the best way to solve this, if at all? If you want me to post the latest bit of code, let me know.

    Thanks,

    Andrew

  • As for what to do about the space problem, I think you'll have no choice but to make that the first thing you check for on the KeyDown event, and then just don't try to move the SelStart within the TYPING_BOX, as that will occur BEFORE the space actually appears. However, you might try also changing the SelLength to 0 and then maybe that solves the problem because returning focus to the field selects it all by default, so that's probably why the text is getting wiped by the space. I'm trying that now, but not having much success with it, as now the space in the TYPING_BOX is getting wiped on the very next character, and I can't quite figure out why.

    Steve

    (aka smunson)

    :):):)

  • smunson (8/11/2008)


    As for what to do about the space problem, I think you'll have no choice but to make that the first thing you check for on the KeyDown event, and then just don't try to move the SelStart within the TYPING_BOX, as that will occur BEFORE the space actually appears. However, you might try also changing the SelLength to 0 and then maybe that solves the problem because returning focus to the field selects it all by default, so that's probably why the text is getting wiped by the space. I'm trying that now, but not having much success with it, as now the space in the TYPING_BOX is getting wiped on the very next character, and I can't quite figure out why.

    Steve

    (aka smunson)

    :):):)

    Ok, im going to try the check to see if its a space on the KeyDown event.

    A quick question, does the KeyDown event always fire before the KeyPress event?

    EDIT

    I think the Keydown event does fire before.

    I have found something quite puzzling though, I typed in 'test' and then I set a breakpoint on the KeyDown event. When I pressed a space, I found that the TYPINGBOX seems to think that there is only 'tes' there, whereas the MIRRORBOX correctly reports 'test'. Why do you think that is?

  • I don't know, but you have to remember that there are three separate events for every key pressed. KeyDown, KeyPress, and KeyUp. They occur IN THAT ORDER. There are also some keys for which there is no KeyPress event - only KeyDown and KeyUp events - such as the Delete key and the CAPS LOCK key.

    So, now that I think about it, it may be best to use the KeyUp event as opposed to the KeyDown event. You'll have to take into account that the KeyDown event fires multiple times in a row if you hold down the shift key. KeyUp can only really fire once per key press, kind of by definition.

    Steve

    (aka smunson)

    :):):)

  • Sorry for not having come back again and, smunson thanks for taking on the challenge. I have never before had to write this type of program and to me this is also a challenge. I will definitely look tommorrow (08:00 GMT+02:00) and let youknow my findings.:w00t::w00t::w00t::w00t::w00t::w00t::w00t:

    :-PManie Verster
    Developer
    Johannesburg
    South Africa

    I can do all things through Christ who strengthens me. - Holy Bible
    I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)

Viewing 9 posts - 16 through 23 (of 23 total)

You must be logged in to reply to this topic. Login to reply