Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004

    Unanswered: question about an onscreen keyboard



    i'm building a on screen keyboard with has to adjust fields on a subform.
    When pressing a key a letter is added in a certain field like :

    me!subform!field1= me!subform!field1 & "Q"

    How can i detect which field has the focus on the subform and the code is only editting this field ?

    I've tried to set a global variable ( veld ) when a certain field got the focus and use this value in the code like :

    me!subform(veld)= me!subform(veld) & "Q"

    This is not working .... what should be the correct code ?

    Thanks in advance
    bye erwin
    Bye Erwin

  2. #2
    Join Date
    Feb 2004
    Chicago, IL
    To get the name of the current control, use:


  3. #3
    Join Date
    Nov 2003
    First don't really need to build a keyboard since Microsoft already provides one (Start button | Programs | Accessories | Accessibility | On-Screen Keyboard). It already contains all the hooks you need for exploring a Form and data entry. Just fire it up via code.

    But...if you're still bent on doing this then we have to assume that your Parent (main) Form contains the buttons for your keyboard and the Sub Form or nested Sub Form is contained within this Parent Form. If this is not the case then we would have to be playing with a different ball of wax than what I am about to show here with the provided simple function.

    The function below {TypeCharacter()} should be placed within the code module for your Parent Form. You could place it into a Database code module if you like but be sure to make sure it is declared as Public. To use this function simply place the call directly into the OnClick event of your Form’s Keyboard Command Buttons like this:


    or you can place it into the Parent Form Code Module for your keyboard buttons OnClick event like this:

    Call TypeCharacter(Me.Button_A.Caption, Me.SubFormControlName)

    Button_A is the Control Name for your Form Keyboard letter “A” button. This parameter is needed so as to distinguish which character you actually want placed into the Sub Form field that you last gave focus to. This means then that (naturally) your keyboard button would contain only one letter in the Caption for that button. Of course this wont always be the case because some buttons may contain more than one character such as, the Space Bar. It will most likely contain the word “Space”. You will need to do something a little fancier with your function or how you pass the parameter. For the sake of the Space Bar for example you will perhaps place a space in the caption before you placed the word “Space” so that it really would be “ Space”. Then the function Call for this button might be:


    Because the Asc() function only returns the ASCII code for the first character in a string which in this case is the Button caption and we placed a space at the beginning of the caption, the ASCII code 32 is returned. This is then handled by the Chr$() function which converts this ASCII 32 to a string space.

    Of course all this can be handled directly within the function itself if you want to modify the code (which is ultimately what you are going to do anyway).

    SubFormControlName is the name of the Sub Form control on your Parent Form. It is currently home for the Sub Form your keyboard selections will relate to.

    Here is the TypeCharacter Function:
    Private Function TypeCharacter(Character As String, SubCtrl As Control) As Long
       ' This function will return 0 if successfull or the
       ' number of the Error that may have occured. If a
       ' non-character element was passed to this function
       ' then -1 is returned.
       ' Declare this Variable to hold the control which
       ' has focus on the SubForm.
      Dim Ctrl As Control
       ' Trap Errors. Note: You will get an error if you
       ' select a control on the Parent (Main) Form then
       ' select one of the keyboard buttons which is to
       ' supply data to the SubForm. Be sure to have:
       ' "Break On Unhandled Errors" checked in the Options
       ' section of the Visual Basic Editor (VBE) - Tools |
       ' Options | General Tab).
      On Error GoTo TypeCharacter_Error
       ' Check for actual character elements. If a non-
       ' character element was passed to this function then
       ' exit without processing and a return -1 Error.
       If Asc(Character) < 32 And Asc(Character) > 126 Then
          TypeCharacter = -1
          GoTo TypeCharacter_Exit
       End If
       ' This is the little puppy that pulls out the object which
       ' currently has focus in our SubForm and puts its' reference
       ' into our Ctrl control variable.
       Set Ctrl = Forms("Form1")("SubFrmControl1").Form.ActiveControl
       ' Now...before we can insert our supplied character, we need
       ' to know if the SubForm control we are dealing with is in
       ' fact a TextBox or ComboBox. If it isn't then whats the sense.
       ' This function will return a -2 if this is the case.
      If Ctrl.ControlType = acTextBox Or Ctrl.ControlType = acComboBox Then
          ' Yup...It's a TextBox or ComboBox so let's add
          ' the character to the SubForm control.
         Ctrl.Value = Ctrl.Value & Character
          ' Nope...Not a TextBox or ComboBox so
          ' let's return -2 Error and exit.
          TypeCharacter = -2
          GoTo TypeCharacter_Exit
       End If
       ' SUCCESS - We return 0 for success. The character
       ' was inserted into the SubForm's control which
       ' had focus and no Errors were encountered.
       TypeCharacter = 0
       ' free memory
       Set Ctrl = Nothing
       Exit Function
       ' An Error was encountered so let's return
       ' the error then clear the instance of it
       ' so we don't have to worry about it anymore
       ' then get outta here.
      TypeCharacter = Err.Number
       Resume TypeCharacter_Exit
    End Function
    {Replace items above in Red with names to suit your application}

    Because we are accessing the last field focused in a Sub Form we need to use something a little more in depth than the good old:


    since all this does is display the name of the button you selected on the Parent Form. We need to go deeper than that. We can use:

    Application.Screen.PreviousControl.Form.ActiveCont rol.Name

    To test this, place the following code in a command button’s OnClick Event on the Parent Form:

    Msgbox Application.Screen.PreviousControl.Form.ActiveCont rol.Name

    then set focus to a field in your Sub Form. Now Click the button on the Parent Form. The name of the control which had focus in the Sub Form is displayed within the Message Box.

    Another thing the function deals with is to restore focus back to the Sub Form field that originally had focus to begin with so that multiple characters can be entered within that field from the Parent Form Keyboard buttons. Details of how to set focus to controls on a Sub Form (or nested Sub Forms) is provided in the Function code above.

    Last edited by CyberLynx; 05-23-06 at 18:49.
    Self Taught In ALL Environments.....And It Shows!

  4. #4
    Join Date
    Feb 2004

    Thumbs up

    Hi Cyberlinx,

    thanks for your great answer , i coud not quit folow your explanation. I will save this for later. I got another answer in topic .
    This helpt me get futher.

    I did the following :
    When a field is getting foucus i change a global variable with its fieldname.
    When using the onscreen keyboard i used the following code :

    Forms("artikelen")("artikels").Form(veld) = Forms("artikelen")("artikels").Form(veld) & "U"

    "Veld" being the global variable.

    Everthing works fine except placing the decimal sign ( in our case the , ) in a numeric ( single ) field.

    Do you have any ideas how to do that ??

    Bye Erwin

  5. #5
    Join Date
    Nov 2003
    Try utilizing the Format function.

    Forms("artikelen")("artikels").Form(veld) = Format(Forms("artikelen")("artikels").Form(veld) & "U", "###,###,###,###")

    Self Taught In ALL Environments.....And It Shows!

Posting Permissions

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