Link to home
Start Free TrialLog in
Avatar of Naylin
Naylin

asked on

Passing textbox name

I need to be able to pass the name of a text box on form2 to a click event on form1 so I can do stuff with it:

With TextBoxName
    .text ="stuff"
End With

I have several text boxes on form2 and my click event on form1 needs to know what textbox it's supposed to put text into.

Thanx in advance, and if you need any more info, let me know!

--Naylin
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

You need to pass the control as a parameter to a sub:

Private Sub Command1_Click()
    Call DoSomethingWithTextBox(Text1)
End Sub

Public Sub DoSomethingWithTextBox(ByRef pTB As TextBox)
    pTB.Text = "Something new"
End Sub
Avatar of Naylin
Naylin

ASKER

I probably should've been a little more specific:

IN FORM1:

Private Sub Command1_Click()
    With TextBoxName 'How do I get TextBoxName
        .text ="stuff"
    End With
End Sub

--Naylin
Avatar of Naylin

ASKER

Again, I probably should've been a little more specific:

The text box I need the name of needs to be whatever text box had focus last.

--Naylin
I don't really understand what is the relation between form1 and form2, which form is active, ... Can you re-explain it?

There are no other ways other then passing the required textbox in parameters somehow!
Avatar of Naylin

ASKER

Ok, form1 is a background form. Form2 is a form for filling out info. Form2 is transparent to the users eyes as it looks exactly like part of form1. I need so if a button is pressed on form1 it does stuff to the last active textbox on form2. Therefore I need to pass which textbox was last active to the button on form1.
ASKER CERTIFIED SOLUTION
Avatar of stefri
stefri
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
On Form1:

Option Explicit

Private Sub Command1_Click()
    Form2.gctlCurrentTextBox.Text = "something new"
End Sub

Private Sub Form_Load()
    Form2.Show
End Sub



On Form2:

Option Explicit

Public gctlCurrentTextBox As TextBox

Private Sub Text1_GotFocus()
    Set gctlCurrentTextBox = Text1
End Sub

Private Sub Text2_GotFocus()
    Set gctlCurrentTextBox = Text2
End Sub

Private Sub Text3_GotFocus()
    Set gctlCurrentTextBox = Text3
End Sub
Avatar of Naylin

ASKER

This worked. Of course I had tried several other things other than making the variable as object and none of them worked.

Thank you.

--Naylin
Add Module1, Form1 and Form2.
Add 5 textboxes named Text1 to Form2 as a control array. (Index values: 0,1,2,3,4)



'Module1 code:

Option Explicit

Public LastTextBox As Integer



' Form1 code:

Option Explicit

Private Sub Form_Click()
  Form2.Show
End Sub

Private Sub Form_GotFocus()
  Me.Caption = LastTextBox
End Sub

Private Sub Form_Load()
  LastTextBox = 0
End Sub



' Form2 code:

Option Explicit

Private Sub Text1_GotFocus(Index As Integer)
  LastTextBox = Index
End Sub


This will give you the index value of the textbox that had focus last.