Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2011
    Posts
    18

    Unanswered: Decalring Textbox in another FORM

    hello im having trouble declaring my variable for my textbox on another form

    heres how it is set up:
    formA has a subform of formB

    formB has two textboxes only

    formA has one textbox

    I want it so that if a user clicks on an empty textbox in formA's subform (formB), a messagebox should pop up if the one textbox in forma is empty.

    basically a user MUST fill in the value for textbox in formA before writing anything in the subform.

    this is what i tried:

    declaring public in formA
    public lanID as textbox
    lanid = me.lanid

    in formb:
    beforeupdate property of the two textboxes:

    if isnull(lanid) then
    msgbox ("LAN ID is empty")

    the problem is, it does not recognize the "lanid" variable even though i declared it as public in formA.

    i also tried creating a module:
    dim lanid as textbox
    lanid = forms!formA!lanid

    but, it does not recognize "forms"

    please help. thank you!

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    When you declare an object, such as:
    Code:
    Dim MyTextBox as TextBox
    you must then initialize it (i.e. assign a reference to it). You cannot write:
    Code:
    MyTextBox = SomeOtherTextBox
    This would assign the default property (Value in this case) of SomeOtherTextBox to MyTextBox . It is then equivalent to:
    Code:
    MyTextBox.Value = SomeOtherTextBox.Value
    To actually initialize it (i.e. MyTextbox holds a reference, = is a pointer in C, to SomeOtherTextBox ) you must use:
    Code:
    Set MyTextBox = SomeOtherTextBox
    Now you can access the properties and methods (if any) of SomeOtherTextBox through MyTextbox.
    Have a nice day!

  3. #3
    Join Date
    Nov 2011
    Posts
    18
    that didnt work, it still says it doesnt recognize the variable.

    when i dim it (textbox of formA), do i dim it in formA or formB or in the module?

  4. #4
    Join Date
    Nov 2011
    Posts
    18
    i put this at the top of the code in formA

    Public lanid As TextBox
    Set lanid = Me.LAN_ID


    didnt work?

  5. #5
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    I don't see why you would declare a variable on a control in the form where the control resides, as you could simply use Me.Control to reference it.

    Here's an example (and it works):
    Code:
    Private Sub Form_Open(Cancel As Integer)
    '
    ' This is the event handler for the Current event
    ' in the FormA (main or parent) form.
    '
        Dim MyTextBoxVariable As TextBox
    '
    ' Child_FormB is the name of the control of type SubForm/SubReport
    ' that has FormB (the subform) as it's Source Object.
    '
    ' Initialize the local variable MyTextBoxVariable
    '
        Set MyTextBoxVariable = Me.Child_FormB.Form.TextBoxOnFormB
    '
    ' Display "Hello world!" in the TextBox control TextBoxOnFormB
    ' using the local variable that has a reference to it.
    '
        MyTextBoxVariable.Value = "Hello world!"
        
    End Sub
    Have a nice day!

  6. #6
    Join Date
    Nov 2011
    Posts
    18
    i think you are misunderstanding. I have no problem referring to the textbox of subformB when i am on FormA. what im saying is how do i refer to the "click" event of the textbox in subformB? when on formA and i click on the subform, there is no "click" on the drop down list when typing in the code. i can only see the "click" property when i am on formB.

    basically if the user does not type in anything for textbox 1 and he goes onto textbox2 (located on subformB), then a message box will pop up saying he must fill in textbox1 on formA first

  7. #7
    Join Date
    Nov 2011
    Posts
    18
    here is my form so you can see what i mean. if the user starts typing in Channel ID before typing anything in LAN ID, an error message should pop up

    http://i130.photobucket.com/albums/p...rmeasytrak.jpg

  8. #8
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    OK, now I see.

    In order to achive what you want, you have to use a mechanism called "Dynamic External Event Procedures" (or DEEP for short). It consists in creating a class module that will be "attached" (or "linked", or "bound") to FormB. This class will act as a relay to transform a Private Event (TextBoxOnFormB_Click) into a Public Event (TextBoxClick).

    Here's an example based on the same objects as in my first post.

    Here is the code in the module of FormA (the parent form):
    Code:
    '
    ' This is the class module of FormA, the main (or parent) form.
    '
    Option Compare Database
    Option Explicit
    '
    ' Declare, then initialize a private instance of the class bound to FormB
    ' that will raise the public event TextBoxClick.
    '
    Private WithEvents clsFormB As Cls_FormB
    
    Public Sub Hook(Child As Object)
    
        Set clsFormB = Child
        
    End Sub
    
    Public Sub clsFormB_TextBoxClick()
    '
    ' This is the event handler for the public event TextBoxClick raised by clsFormB.
    '
        MsgBox "Click event detected for TextBoxOnFormB", vbInformation, "OnClick"
        
    End Sub
    Here is the code in FormB (the SubForm):
    Code:
    '
    ' This is the class module of FormB, the subform (or child form).
    '
    Option Compare Database
    Option Explicit
    '
    ' Declare an instance of the DEEP class.
    '
    Private m_ClsForm As Cls_FormB
    
    Private Sub Form_Open(Cancel As Integer)
    '
    ' Bind the instance of the DEEP class.
    '
        Set m_ClsForm = New Cls_FormB
        m_ClsForm.DEEP_Attach Form
    
    End Sub
    And the code for the DEEP class module Cls_FormB:
    Code:
    '
    ' This is the DEEP class module attached to FormB, the subform (or child form).
    '
    Option Compare Database
    Option Explicit
    '
    ' Declare the public event that will be handled in FormA.
    '
    Public Event TextBoxClick()
    
    Private Form As Form    ' Reference to FormB. The variable name MUST be Form!
    Private WithEvents m_TextBox As TextBox    ' Reference to the TextBox on FormB.
    
    Public Function DEEP_Attach(ByRef frm As Form)
    '
    ' Called when FormB is open to bind the form 
    ' with the instance of its DEEP class.
    '
        Set Form = frm
        Set m_TextBox = Form.TextBoxOnFormB
        m_TextBox.OnClick = "[Event Procedure]"
        Form.Parent.Hook Me
    
    End Function
    
    Private Sub m_TextBox_Click()
    '
    ' The Click event for TextBoxOnFormB is now handled here.
    '
    ' Raise the public event that will be handled by FormA.
    '
        RaiseEvent TextBoxClick
        
    End Sub
    For more information on the DEEP concept, see:http://www.vb123.com/kb/index.html?199901_ss_event.htm
    Have a nice day!

  9. #9
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    After investigating a little bit more, I found that there is a simpler solution that does not require a DEEP class. You simply declare the TextBox variable on FormA (the parent form) With Events, like this:
    Code:
    '
    ' This is the class module of FormA, the main (or parent) form.
    '
    Option Compare Database
    Option Explicit
    
    Private WithEvents MyTextBox As TextBox
    
    Private Sub Form_Open(Cancel As Integer)
    
        Set MyTextBox = Me.Child_FormB.Form.TextBoxOnFormB
        MyTextBox.OnClick = "[Event Procedure]"
    
    End Sub
    
    Private Sub MyTextBox_Click()
    
        MsgBox "Click event detected for TextBoxOnFormB", vbInformation, "OnClick"
        
    End Sub
    Have a nice day!

Posting Permissions

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