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

  • Hi,

    I have a form, with a subform on and a subform within that. What I would like to do is as the user is typing in a field (Description) in the subsubform (Technical), the text is automatically displayed in an unbound text box (txtDescription) on the subform (Brainstorm). I have done some code already and have it so that it displays the previous character entered, but this is not what I want and the focus is also staying on txtDescription in Brainstorm. Here is what I have:

    Private Sub Technical_Change()

    strBrainstormingDescription = Me.ExternalTechnical

    Forms!riskform!BrainStorm.Form.txtDescription.SetFocus

    Forms!riskform!BrainStorm.Form.txtDescription = strBrainstormingDescription

    ' Me.ExternalTechnical.SetFocus

    End Sub

    Where strBrainstormingDescription is a global string variable. I have tried setting the focus back to Technical but it doesn't happen.

    Thanks,

    Andrew

  • Hi,

    I think the best you're going to get is using the Keypress event of the text box you are typing into.

    Assuming i've got your hierarchies correct

    [Code]

    Private Sub ExternalTechnical_KeyPress(KeyAscii As Integer)

    Dim StrTxt As String

    StrTxt = Nz(Form_riskform.Form("BrainStorm").Form.txtDescription, "")

    Form_riskform.Form("BrainStorm").Form.txtDescription = StrTxt & Chr(KeyAscii)

    End Sub

    [/Code]

    Hope that helps

    K.

  • Hi,

    I tried the code and it almost works as I want it. Here is the code I have now:

    Private Sub ExternalTechnical_KeyPress(KeyAscii As Integer)

    Dim StrTxt As String

    StrTxt = Nz(Forms!riskform!BrainStorming.Form.txtDescription, "")

    Forms!riskform!BrainStorming.Form.txtDescription = StrTxt & Chr(KeyAscii)

    End Sub

    Private Sub Form_Current()

    Forms!riskform!BrainStorming.Form.txtDescription = Me.ExternalTechnical

    End Sub

    It recognises the key presses, but it displays them after whatever is in the field to begin with. So for example I have the word test in ExternalTechnical, if I wanted to change the word to tegst, then it would display testg in the unbound text box. Similarly, if I delete the s from test (leavin tet) then the word test is still displayed (as it deletes nothing from the end of the word). If I press backspace, a square appears in the unbound text box.

    So is it possible to have the unbound text box constantly updated as to whatever is in ExternalTechnical?

  • I am not sure I've got this right but your current keypress looks like it is on the subform. It is better to do the keypress on the textbox you are typing in. You should get much better results then.

    :-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)

  • Manie Verster (6/11/2008)


    I am not sure I've got this right but your current keypress looks like it is on the subform. It is better to do the keypress on the textbox you are typing in. You should get much better results then.

    The KeyPress event is on the subform as the subform contains the textbox I am typing in, and the unbound text box on the main form is where I would like whatever is typed in the textbox on the subform to be replicated as the user is typing.

  • Can anyone provide any more help on this?

  • skyline666 (6/16/2008)


    Can anyone provide any more help on this?

    😛 In that case the keypress event should be on the textbox where you are typing. It is now 20:03 in South Africa but I will test this for you tommorrow and let you know the results. 😛

    :-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)

  • Manie Verster (6/17/2008)


    skyline666 (6/16/2008)


    Can anyone provide any more help on this?

    😛 In that case the keypress event should be on the textbox where you are typing. It is now 20:03 in South Africa but I will test this for you tommorrow and let you know the results. 😛

    Sorry for taking so long but I had quite a hectic week. The keypress event should be done on the textbox in the subsubform or whatever form you use. I have an event code for you that you can paste in the textbox's event.

    Private Sub MyTextbox_KeyPress(KeyAscii As Integer)

    If IsNull(Forms![MyMainForm]!TextBoxMain) Then

    Forms![MyMainForm]!TextBoxMain = ""

    End If

    Forms![MyMainForm]!TextBoxMain = Forms![MyMainForm]!TextBoxMain & Chr(KeyAscii)

    End Sub

    It works wonderfully, I tested it myself. Just replace the form and textbox names with your own. If you don't come right just put a post on this forum and I'll see if I can help.

    :-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)

  • That is sort of what I had before. The problem is that, it works when the text box is blank. But, if I want to add to the text box before the end of the text, it displays at the end of the text box on the main form. I'll explain:

    Text box on subform is blank, and so text box on main form is blank. I type in 'Test' (without quotes) in the text box on the subform and 'Test' appears on the text box in the main form. That works. If I then go to the start of 'Test' and type 'Hello' (so text box on subform shows 'Hello Test'), then the text box on the main form shows 'TestHello '. So as you see, it does work in that it shows what is typed, but it doesn't display the text in the correct order. Also, if I were to delete 'Test' (using backspace) from the subform text box (so 'Hello' only displays), then the text box on the main form displays 'Test Hello ????' (the question marks being in small boxes as well). Also, if I deleted 'Test' using the delete button instead, then nothing happens on the text box on the main form.

    Here is what I had before:

    Private Sub ExternalTechnical_KeyPress(KeyAscii As Integer)

    Dim strTxt2 As String

    strTxt2 = Nz(Forms!riskform!BrainStorming.Form.txtDescription, "")

    Forms!riskform!BrainStorming.Form.txtDescription = strTxt2 & Chr(KeyAscii)

    End Sub

    and here is what I have now (using your code):

    Private Sub ExternalTechnical_KeyPress(KeyAscii As Integer)

    If IsNull(Forms!riskform!BrainStorming.Form.ExternalTechnical.Form.ExternalTechnical) Then

    Forms!riskform!BrainStorming.Form.txtDescription = ""

    End If

    Forms!riskform!BrainStorming.Form.txtDescription = Forms!riskform!BrainStorming.Form.txtDescription & Chr(KeyAscii)

    End Sub

    Note

    The text box (called ExternalTechnical) on the subform is referenced as: Forms!riskform!BrainStorming.Form.ExternalTechnical.Form.ExternalTechnical

    The text box (called txtDescription) on the main form is referenced as: Forms!riskform!BrainStorming.Form.txtDescription

  • Can anyone help me with this, im still having trouble.

    Thanks.

  • Just curious, but I take it that merely setting the other control to equal the value of the textbox that's being typed into, as part of the keypress event, doesn't work? If that's true, then there's no concievable way to get the desired result, because there's no way to persist the value in the textbox being typed in. There would be no place to find the current value of the textbox being typed into, and event processing would require that you somehow 1.) persist a value across separate events, and 2.) understand exactly what to do with all 255 different possible values for any one keystroke. I'm not sure that's realistic.

    I can guarantee that merely using the individual keystrokes is going to have a positional problem, as you noticed. There, the problem is that you don't really have any way to know where the cursor is positioned within the textbox being typed into. Without that knowledge, any attempt at merely using the keystrokes to update the value in the other textbox is doomed.

    I'm going to have to test this for myself as my curiousity has been piqued. It sounds like an interesting, if perhaps doomed, challenge...

    Steve

    (aka smunson)

    :):):)

  • smunson (8/8/2008)


    Just curious, but I take it that merely setting the other control to equal the value of the textbox that's being typed into, as part of the keypress event, doesn't work? If that's true, then there's no concievable way to get the desired result, because there's no way to persist the value in the textbox being typed in. There would be no place to find the current value of the textbox being typed into, and event processing would require that you somehow 1.) persist a value across separate events, and 2.) understand exactly what to do with all 255 different possible values for any one keystroke. I'm not sure that's realistic.

    I can guarantee that merely using the individual keystrokes is going to have a positional problem, as you noticed. There, the problem is that you don't really have any way to know where the cursor is positioned within the textbox being typed into. Without that knowledge, any attempt at merely using the keystrokes to update the value in the other textbox is doomed.

    I'm going to have to test this for myself as my curiousity has been piqued. It sounds like an interesting, if perhaps doomed, challenge...

    Steve

    (aka smunson)

    :):):)

    Hi Steve,

    I tried setting the control source of the text box on Brainstorming (the main form) to the text box in ExternalTechnical (the sub form), and with taking the keypress event out, it does only show whatever is in ExternalTechnical when you first click the text box. Putting the keypress event back in brings up an error, saying that you can't assign a value (to the Brainstorming text box), which I assume is because the control source has been set.

    I going to play around with it some more and post back what doesn't work. I was going to say what does and doesn't work, but I don't think it will be solveable :hehe:.

    Thanks,

    Andrew

    Update:

    Well I have tried a few things but with no luck. I tried setting the text field on ExternalTechnical (ET) to a variable, with the character appended, and then setting the text box on ET to that variable, and what I thought may work at the time, soon realised it still didn't put the character in the correct place.

    As for the cursor position, is it totally impossible to know where it is? I thought of maybe using InStr somehow to try an find where it is, but I don't think that would work. Well it might work if the character entered only appears once in the string, but as soon as it appears more than once, then I don't think you would be able to use InStr alone to know where the position is.

  • I've just done some testing, and it turns out you CAN know where the cursor is. You use the SelStart property for that. However, the killer is the DELETE key. There's no keypress event fired when that key is pressed. That destroys any chance at using the keypress event. However, if you could somehow ensure the users never pressed delete (yeah, fat chance, right?), you could probably modify the following code to get most of it worked out. I don't have time to finish messing with it, but it did surprise me on how well it did work. I didn't expect to do quite this well with it:

    Private Sub TYPING_BOX_KeyPress(KeyAscii As Integer)

    Debug.Print "TYPING_BOX = " & Chr(34) & Me!TYPING_BOX & Chr(34)

    Debug.Print "Key Pressed was: " & Chr(34) & Chr(KeyAscii) & Chr(34) & " KeyAscii = " & KeyAscii

    Debug.Print "SelStart = " & Me!TYPING_BOX.SelStart

    If KeyAscii <> 13 Then

    If KeyAscii <> 8 And Me!TYPING_BOX.SelStart = Len(Me!TYPING_BOX) Then

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

    Else

    If KeyAscii = 8 Then

    Debug.Print "Backspace Key Pressed"

    Debug.Print "MIRROR_BOX Before = " & Me!MIRROR_BOX

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

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

    Debug.Print "MIRROR_BOX After = " & Me!MIRROR_BOX

    Else

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Me!TYPING_BOX.SelStart - 1) & _

    Right(Me!MIRROR_BOX, Len(Me!MIRROR_BOX) - Me!TYPING_BOX.SelStart)

    Debug.Print "MIRROR_BOX After = " & Me!MIRROR_BOX

    End If

    Else

    If Me!TYPING_BOX.SelStart = 0 Then

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

    Else

    Me!MIRROR_BOX = Left(Me!MIRROR_BOX, Me!TYPING_BOX.SelStart) & Chr(KeyAscii) & _

    Right(Me!MIRROR_BOX, Len(Me!MIRROR_BOX) - Me!TYPING_BOX.SelStart)

    End If

    End If

    End If

    End If

    End Sub

    The two controls are on the same form, but I doubt that has any real impact on things. Anyway, the box I would type in is Me!TYPING_BOX and the other one is Me!MIRROR_BOX. Good luck with this.

    Steve

    (aka smunson)

    :):):)

  • smunson (8/8/2008)


    I've just done some testing, and it turns out you CAN know where the cursor is. You use the SelStart property for that. However, the killer is the DELETE key. There's no keypress event fired when that key is pressed. That destroys any chance at using the keypress event. However, if you could somehow ensure the users never pressed delete (yeah, fat chance, right?), you could probably modify the following code to get most of it worked out. I don't have time to finish messing with it, but it did surprise me on how well it did work. I didn't expect to do quite this well with it:

    ----

    The two controls are on the same form, but I doubt that has any real impact on things. Anyway, the box I would type in is Me!TYPING_BOX and the other one is Me!MIRROR_BOX. Good luck with this.

    Steve

    (aka smunson)

    :):):)

    Thanks Steve. I'll take a look at this at the weekend or Monday when I get back in as I haven't got much time left today.

    You said about the users not using the backspace (which would be hard to make them not use it, damn users :D), what I did think about was as your code finds where the cursor is, I could take that position, and then remove the character to the left of the cursor position, and then update the text box. This is just an idea in my head at the moment, but I think it would be possible, I hope!

    Thanks again,

    Andrew

  • Actually, the BACKSPACE key IS being handled in my code. It's the DELETE key that's the killer :w00t:.

    Steve

    (aka smunson)

    :):):)

Viewing 15 posts - 1 through 15 (of 23 total)

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