VBA input value from another UserFormB to TextBox from UserFormA

I have a userForm (mappingGuide) that allows the user to select smartyTag from a list of more convenient names.

I have a second user form (conditionalBuilder) that I would call this userForm by double-clicking the text box so that the user can find which smartyTag to apply (in case they don't know).

So the logic is:

  • open conditionalBuilder
  • double-click the Field field
  • mappingGuide opens
  • select smartytag from the list.
  • enter smartytag value in the text field of the field in the conditional buffer
  • unload mappingGuide

The problem that I encounter in fulfilling this requirement is that when loading the forms themselves, I cannot find a way to set the text of the text field fieldName of the loaded instance of conditionalBuilder (see the last block of code below). I searched around but can't understand.

Here is the relevant code:

conditionalBuilder is loaded from the user interface ribbon

Sub RunCode(ByVal Control As IRibbonControl)

    Select Case Control.ID

        Case Is = "mapper": LoadMappingGuide
        Case Is = "conditional": LoadConditionalBuilder

    End Select

End Sub

Sub LoadConditionalBuilder()

    Dim conditionalForm As New conditionalBuilder
    conditionalForm.Show False

End Sub

double-click the fieldName event, then load mapGuide

Private Sub fieldName_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

    Me.hide
    Dim pickField As New mappingGuide
    pickField.Show False

End Sub

smartTag listbox click event then tries to put the selection in fieldName (or the choice if the form is not loaded)

Private Sub smartTagList_Click()

    If smartTagList.ListIndex > -1 And smartTagList.Selected(smartTagList.ListIndex) Then

        Dim smartyTag As String
        smartyTag = smartTagList.List(smartTagList.ListIndex, 2)

        If isUserFormLoaded(conditionalBuilder.Name) Then
            '*** ---> below is my issue how to reference instance of form 
            conditionalBuilder.fieldName.Text = smartyTag
            conditionalBuilder.Show
        Else
            Selection.Range.Text = smartyTag
        End If

    End If

    Unload Me

End Sub

If there is a more efficient setting, it will also be very useful. I have separate forms because there are several levels that a user can create with tags.

+4
source share
3 answers

, , .

1:

 Option Explicit

    Sub test()
        frmMaster.Show False
    End Sub

1: frmMaster:

Option Explicit
'/ Declare with events
Dim WithEvents frmCh As frmChild

Private Sub TextBox1_DblClick(ByVal cancel As MSForms.ReturnBoolean)
    handleDoubleClick
End Sub
Sub handleDoubleClick()

    If frmCh Is Nothing Then
        Set frmCh = New frmChild
    End If

    frmCh.Show False

End Sub

'/ Handle the event
Private Sub frmCh_cClicked(cancel As Boolean)
    Me.TextBox1.Text = frmCh.bChecked
End Sub

2: frmChild:

Option Explicit

Event cClicked(cancel As Boolean)
Private m_bbChecked As Boolean

Public Property Get bChecked() As Boolean
    bChecked = m_bbChecked
End Property

Public Property Let bChecked(ByVal bNewValue As Boolean)
    m_bbChecked = bNewValue
End Property

Private Sub CheckBox1_Click()
    Me.bChecked = Me.CheckBox1.Value
    '/ Raise an event when something happens.
    '/ Caller will handle it.
    RaiseEvent cClicked(False)
End Sub
+3

, . - , .

Presenter.
, , , ConditionalBuilder. UserForms.

Private WithEvents CB As ConditionalBuilder
Private MG As MappingGuide

Public Sub ShowCB()
    Set CB = New ConditionalBuilder
    CB.Show vbModal
End Sub

Private Sub CB_ShowMappingGuide()
    Set MG = New MappingGuide
    MG.Show vbModal
    CB.UpdateTB1 Value:=MG.SmartTag
End Sub

ConditionalBuilder.
, , .

Public Event ShowMappingGuide()

Public Function UpdateTB1(Value As String)
    TextBox1.Value = Value
End Function

Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    RaiseEvent ShowMappingGuide
End Sub

MappingGuide.
Type Property , , - .

Private Type TView
    Tag As String
End Type
Private this As TView

Public Property Get SmartTag() As String
    SmartTag = this.Tag
End Property

Private Sub UserForm_Initialize()
    Tags.List = Array("a", "b", "c")
End Sub

Private Sub Tags_Click()
    this.Tag = Tags.List(Tags.ListIndex, 0)
    Me.Hide
End Sub

, . , .

Public Sub ShowProject()
    With New Presenter
        .ShowCB
    End With
End Sub

1 ( )

enter image description here


2 ( "b" )

enter image description here


3 ()

enter image description here

+3

I really solved it by placing the block below IF, where I check the form loading, and I will stay open for better answers, if any.

    Dim uForm As Object
    For Each uForm In VBA.UserForms
        If uForm.Name = conditionalBuilder.Name Then
            uForm.fieldName.Text = smartyTag
            uForm.Show
        End If
    Next
+1
source

Source: https://habr.com/ru/post/1693202/


All Articles