Transferring Data Between UserForms

In Excel VBA, I have a user form similar to the following where the user enters an identification number and then the data is displayed in the user form:

Private Sub btnIDNo_Click()
Dim IDNo As Long
If txtIDNo.Text <> "" Then
    If IsNumeric(txtIDNo.Text) = True Then
        lblError.Caption = ""
        IDNo = txtIDNo.Text
        Worksheets("Details").Activate
        Range("B4").Select
        While ActiveCell.Value <> "" And ActiveCell.Value <> IDNo
            ActiveCell.Offset(1, 0).Select
        Wend
        If ActiveCell.Value = IDNo Then
            txtName.Value = ActiveCell.Offset(0, 1).Value
            txtPhone.Value = ActiveCell.Offset(0, 2).Value
        Else
            lblError.Caption = "Cannot find ID nummber"
        End If
    Else
        lblError.Caption = "Please enter the ID Number in numeric form"
    End If
End If
End Sub

In the user details form, I have a "Edit" button. Pressing the “Change” button will open another user form in which the user can change the data of this identification number, but obviously not the identification number itself. To do this, I need to transfer the identification number from the "User Information" form to "Edit User Form". Is there any way to do this?

The bottom of the Custom Show Details form to open the Edit Custom Form is similar to the following:

Private Sub CommandButton1_Click()
Dim IDNo As Long
If txtIDNo.Text <> "" Then
    If IsNumeric(txtIDNo.Text) = True Then
        lblError.Caption = ""
        IDNo= txtIDNo.Text
        ufmEditDetails.Show
        ufmShowDetails.Hide
    Else
        lblError.Caption = "Please enter the ID Number in numeric form"
    End If
Range("B4").Select
End If
End Sub

, , , :

http://www.mrexcel.com/forum/excel-questions/671964-visual-basic-applications-pass-variables-between-user-forms.html

http://gregmaxey.mvps.org/word_tip_pages/userform_pass_data.html

http://peltiertech.com/Excel/PropertyProcedures.html

+4
3

... ...

1

  • Public
  • Userform1, Userform2. .

Userform1

Private Sub CommandButton1_Click()
    MyVal = "Sid"
    UserForm2.Show
End Sub

Userform2

Private Sub CommandButton1_Click()
    MsgBox MyVal
End Sub

Public MyVal

2

.Tag

Userform1

Private Sub CommandButton1_Click()
    UserForm2.Tag = "Sid"
    UserForm2.Show
End Sub

Userform2

Private Sub CommandButton1_Click()
    MsgBox Me.Tag
End Sub

3

Label Userform2 False

Userform1

Private Sub CommandButton1_Click()
    UserForm2.Label1.Caption = "Sid"
    UserForm2.Show
End Sub

Userform2

Private Sub CommandButton1_Click()
    MsgBox Label1.Caption
End Sub
+8

. , ,

:

Public commonVariable As String

. userform1:

Private Sub btnIDNo_Click()
    commonVariable = "UserId"
End Sub

UserForm2:

Private Sub CommandButton1_Click()
    me.txtIDNo.Text = commonVariable 
End Sub
+1

:

UserForm2.TxtIDNo.Text = UserForm1.txtIDNo.Text

+1

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


All Articles