Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316

    Unanswered: Assigning control references to variables

    How can I set the full reference of a control to a variable in vb? Lets say I currently have the focus on a textbox called txtCustName, in form frmDataEntry. I want to be able to assign the reference name of the field which has the focus to a variable so I can use that reference in a Function.

  2. #2
    Join Date
    Feb 2003
    Location
    Auckland, NZ
    Posts
    150

    Lightbulb

    Hope this code helps

    Comments are included

    Code:
        Dim ctlCurrentControl As Control
        Dim strControlName As String
        Dim strControlSource As String
        
        'Get the Active Control or Control that has the focus
        Set ctlCurrentControl = Screen.ActiveControl
    
        'Get The name of the control if needed
        strControlName = ctlCurrentControl.Name
    
        'Get the control source of the control or field linked to
        strControlSource = ctlCurrentControl.ControlSource
    Hope this helps

    Machado

    Last edited by machado; 02-28-03 at 16:30.

  3. #3
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316
    Thanks for the reply. I tried the code, but I get an error:

    Run-time error '2474':

    The expression you entered requires the control to be in the active window.


    I am trying to run the code in a modal pop-up form on an On-Enter event for a textbox. Any help much appreciated.

  4. #4
    Join Date
    Feb 2003
    Location
    Auckland, NZ
    Posts
    150
    This error is usually generated if you try to put the focus on a control that is not on the modal form, while the modal form is open.

    Machado

  5. #5
    Join Date
    Feb 2003
    Location
    Auckland, NZ
    Posts
    150
    Tell ne exactly what it is that is happening then I will be able to give you the work around.
    JJ Kennedy
    Double J IT Solutions
    www.doublejit.co.nz

    VB 6, VB.NET, ASP.NET, MS SQL Server, MySQL, MS Access

  6. #6
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316
    I only have the 1 form open. The first control on the form which receives the focus when you load it up is a text box called txtPatchID. Here is the code I have put on its OnEnter event:

    Code:
    Private Sub txtPatchID_Enter()
    txtContextHelp = "Enter the Patch ID for the Patch you are making this request."
    
    Dim ctlCurrentControl As Control
    Dim strControlName As String
    Dim strControlSource As String
        
    'Get the Active Control or Control that has the focus
    Set ctlCurrentControl = Screen.ActiveControl
    
    'Get The name of the control if needed
    strControlName = ctlCurrentControl.Name
    
    'Get the control source of the control or field linked to
    strControlSource = ctlCurrentControl.ControlSource
    
    End Sub

  7. #7
    Join Date
    Feb 2003
    Location
    Auckland, NZ
    Posts
    150
    If you have just the one form open and the function is called from within the form then use:

    me.ActiveControl

    instead of Screen.ActiveControl
    JJ Kennedy
    Double J IT Solutions
    www.doublejit.co.nz

    VB 6, VB.NET, ASP.NET, MS SQL Server, MySQL, MS Access

  8. #8
    Join Date
    Jul 2002
    Location
    Island of Dots
    Posts
    316
    Superb, that works perfectly. Many thanks.

    I don't suppose you know how I can obtain the current form name in the same way?

Posting Permissions

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