Link to home
Start Free TrialLog in
Avatar of NEW_INDIA
NEW_INDIA

asked on

Get data from one form in another

Hi, I use VB6
This is the situation,
I have form (A) there I have some label boxes I will put text in to them by reading a txt file, I will select 1 of those labels at the time and I would like that on form (B) in a specific label the same text to apear there.
So if I select labell 3 from FORM (A) to get the same text in label 1 in FORM (B) is this possible, what is the code please

Thank you
Theo
Avatar of vinnyd79
vinnyd79

On Form B

Label1.Caption = FormA.Label3.Caption
or on Form A

FormB.Label1.Caption = Label3.Caption
Assuming the form was loaded.If not,then

Load FormB
FormB.Label1.Caption = Label3.Caption
FormB.Show
Avatar of NEW_INDIA

ASKER

Vinnyd79,
This looks great but one more question, the label on FORM (B) should know which label was selected in form A and then bring the caption.

Thank you  
ASKER CERTIFIED SOLUTION
Avatar of Ralf Klatt
Ralf Klatt
Flag of Germany 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
Or you could create a public sub on Form B:
public sub updateLabel(byref alabel as label)
   thelabel.caption = alabel.caption
end sub

and on Form A, for the click event of each label:
Private sub label1_click()
FormB.update label1
end sub

(or if using a control array)
Private sub label1_click(byval index as integer)
FormB.update label1(index)
end sub

which will do the same thing.
sory it did not work :
what I have iin form (A) are labels that will be dinamicaly generated and only one of the check box will be marked as selected, then on the other form = FORM (B) always on one designated label the caption of that label should show.
so in FORM (A) I have Label25 is cheked then on FORM (B) always Label1 should show the caption in that label and if:
    in FORM (A) I have Label3 is cheked then on FORM (B) again on Label1 should show the caption in that label

Sorry for being so hmmm.
'Code For FormA only

Dim Pass1 As Boolean        'used for checking only one checkbox

Private Sub Check1_Click()
    If Pass1 Then Exit Sub
    If Check2 = vbChecked Then
        Pass1 = True
        Check2.Value = vbUnchecked
    End If
    Check1.Value = vbChecked
    FormB.Label1.Caption = FormA.Label25.Caption
    Pass1 = False
End Sub

Private Sub Check2_Click()
    If Pass1 Then Exit Sub
    If Check1 = vbChecked Then
        Pass1 = True
        Check1.Value = vbUnchecked
    End If
    Check2.Value = vbChecked
    FormB.Label1.Caption = FormA.Label3.Caption
    Pass1 = False
End Sub

Private Sub Form_Load()
    Pass1 = False
    Check1.Value = vbChecked
    If Check1 = vbChecked Then
        FormB.Label1.Caption = FormA.Label25.Caption
    ElseIf Check2 = vbChecked Then
        FormB.Label1.Caption = FormA.Label3.Caption
    End If
    FormB.Show
End Sub

------
elmer88techteam
Hi,

Well, I've tested this one and, having made some minor changes to the first version, it does what you were looking for:

'===================
'Start File: EECaption.vbp
'===================
Type=Exe
Form=FormA.frm
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\WINDOWS\System32\stdole2.tlb#OLE Automation
Form=FormB.frm
Module=Module; Module.bas
IconForm="FormA"
Startup="Sub Main"
HelpFile=""
Title="EECaption"
Command32=""
Name="Project1"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="xxxxxxx"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1

[MS Transaction Server]
AutoRefresh=1
'===================
'End File: EECaption.vbp
'===================

'===================
'Start File: Module.bas
'===================
Attribute VB_Name = "Module"
Public frmA As New FormA
Public frmB As New FormB

Public ActiveLabel As Integer

Private Sub main()
    Load frmA
    Load frmB
    frmA.Show
    frmB.Show
    frmA.Text1.SetFocus
End Sub
'===================
'End File: Module.bas
'===================

'===================
'Start File: FormA.frm
'===================
VERSION 5.00
Begin VB.Form FormA
   Caption         =   "Form A"
   ClientHeight    =   2730
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3510
   LinkTopic       =   "Form1"
   ScaleHeight     =   2730
   ScaleWidth      =   3510
   StartUpPosition =   3  'Windows Default
   Begin VB.CheckBox Check1
      Caption         =   "Check1"
      Height          =   255
      Index           =   4
      Left            =   3120
      TabIndex        =   11
      Top             =   1080
      Width           =   255
   End
   Begin VB.CheckBox Check1
      Caption         =   "Check1"
      Height          =   255
      Index           =   3
      Left            =   3120
      TabIndex        =   10
      Top             =   840
      Width           =   255
   End
   Begin VB.CheckBox Check1
      Caption         =   "Check1"
      Height          =   255
      Index           =   2
      Left            =   3120
      TabIndex        =   9
      Top             =   600
      Width           =   255
   End
   Begin VB.CheckBox Check1
      Caption         =   "Check1"
      Height          =   255
      Index           =   1
      Left            =   3120
      TabIndex        =   8
      Top             =   360
      Width           =   255
   End
   Begin VB.CheckBox Check1
      Caption         =   "Check1"
      Height          =   255
      Index           =   0
      Left            =   3120
      TabIndex        =   7
      Top             =   120
      Width           =   255
   End
   Begin VB.CommandButton Command1
      Caption         =   "Change Label"
      Height          =   375
      Left            =   840
      TabIndex        =   2
      Top             =   2040
      Width           =   1455
   End
   Begin VB.TextBox Text1
      Height          =   285
      Left            =   120
      TabIndex        =   0
      Top             =   1560
      Width           =   2895
   End
   Begin VB.Label Label1
      BorderStyle     =   1  'Fixed Single
      Height          =   255
      Index           =   4
      Left            =   120
      TabIndex        =   6
      Top             =   1080
      Width           =   2895
   End
   Begin VB.Label Label1
      BorderStyle     =   1  'Fixed Single
      Height          =   255
      Index           =   3
      Left            =   120
      TabIndex        =   5
      Top             =   840
      Width           =   2895
   End
   Begin VB.Label Label1
      BorderStyle     =   1  'Fixed Single
      Height          =   255
      Index           =   2
      Left            =   120
      TabIndex        =   4
      Top             =   600
      Width           =   2895
   End
   Begin VB.Label Label1
      BorderStyle     =   1  'Fixed Single
      Height          =   255
      Index           =   1
      Left            =   120
      TabIndex        =   3
      Top             =   360
      Width           =   2895
   End
   Begin VB.Label Label1
      BorderStyle     =   1  'Fixed Single
      Height          =   255
      Index           =   0
      Left            =   120
      TabIndex        =   1
      Top             =   120
      Width           =   2895
   End
End
Attribute VB_Name = "FormA"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Check1_Click(Index As Integer)
    If Check1(Index).Value = 1 Then ActiveLabel = Index
End Sub

Private Sub Command1_Click()
    Dim i As Integer
    For i = 0 To 4
        If Check1(i).Value = 1 Then
            Label1(i) = Text1.Text
            Check1(i).Value = 0
        End If
    Next i
    frmB.Label1 = frmA.Label1(ActiveLabel)
End Sub

Private Sub Form_Load()
    Me.Left = 0
    Me.Top = 0
End Sub
'===================
'End File: FormA.frm
'===================

'===================
'Start File: FormB.frm
'===================
VERSION 5.00
Begin VB.Form FormB
   Caption         =   "Form B"
   ClientHeight    =   510
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3135
   LinkTopic       =   "Form1"
   ScaleHeight     =   510
   ScaleWidth      =   3135
   StartUpPosition =   3  'Windows Default
   Begin VB.Label Label1
      BorderStyle     =   1  'Fixed Single
      Height          =   255
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   2895
   End
End
Attribute VB_Name = "FormB"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Form_Load()
    Me.Left = frmA.Left + frmA.Width
    Me.Top = 0
End Sub
'===================
'End File: FormB.frm
'===================

The difference to the first version is obvious
- 5 labels and 5 checkboxes (indexed!)
- a variable on module level ("ActiveLabel")
- no more timer on form B


Best regards,
Raisor
Elmer this is fine but
Dim Pass1 As Boolean        'used for checking only one checkbox

Private Sub Check1_Click()
    If Pass1 Then Exit Sub
    If Check2 = vbChecked Then
        Pass1 = True
        Check2.Value = vbUnchecked
    End If
    Check1.Value = vbChecked
    FormB.Label1.Caption = FormA.Label25.Caption       (Can the label25.caption be changed to something like  anyoneselected.caption) ?????/
    Pass1 = False
End Sub

Private Sub Check2_Click()
    If Pass1 Then Exit Sub
    If Check1 = vbChecked Then
        Pass1 = True
        Check1.Value = vbUnchecked
    End If
    Check2.Value = vbChecked
    FormB.Label1.Caption = FormA.Label3.Caption
    Pass1 = False
End Sub

Private Sub Form_Load()
    Pass1 = False
    Check1.Value = vbChecked
    If Check1 = vbChecked Then
        FormB.Label1.Caption = FormA.Label25.Caption
    ElseIf Check2 = vbChecked Then
        FormB.Label1.Caption = FormA.Label3.Caption
    End If
    FormB.Show
End Sub
The question I am pondering about is since the checkboxes on the form are created dynamically, is there some way to have dnyamic code created at the same time which will be pretty much the same except you will make the code use the variable i or x or whatever you use in the for next loop so that it is something like this :

for i = 1 to LineCount

for each d in Label
If chk(i).value = vbchecked then
d.caption = FormB.Label(i).caption

loop

Next

or something to that effect, so the checkboxes which are checked next to each label will go from Form A to Form B. Not sure if this is possible, maybe another expert can help you there :)
Hi,

@gecko_au2003 ... i appreciate very much that you are obviously at the same track as I've been ... you may have a look at this peace of code (and run it in VB6) : https://www.experts-exchange.com/questions/21340920/Get-data-from-one-form-in-another.html#13484111 ...


Best regards,
Raisor
I looked at that code but also know that he is trying to create the checkboxes at runtime from a text file and there could be any amount of users in the text file so the amount of checkboxes isnt set to 5 or 10, it depends on how many users are in the text file. Hence if there are 20 users then the Visual Basic application will create 20 check boxes which are indexed, etc so basically it will do a line count of the text file to determine how many checkboxes it requires. So he needs some way of being able to insert the code into the checkboxes dynamically at runtime if that is at all possible. So that when he checks any of the checkboxes it will make certain labels on the next form equivalent to the ones which were checked.

Yes that code that you have done works, but only for 5 labels and 5 checkboxes.  What happens when the text file has 6 or more users and requires more labels and checkboxes ?? or even if it is less then 5 labels and 5 checkboxes ??
Hi,

Thanks gecko_au2003 for having tested the provided peace of code ... I'm sure you'll agree that there are ways of creating labels and checkboxes programmatically ["on the fly"] ... having indexed them the way I proposed makes sure that the provided code would work having 0 labels & checkboxes or having 10.000 labels & checkboxes ... the labels and checkboxes can even be placed in a structured way on a form, depending on their size. As for the basic function I've provided you may take this modified example and see, that "NumberOfControls" is variable and may even depend on the number of lines found in a read text file:

'Example:
'======
Public NumberOfControls As Long
Private Sub Command1_Click()
    Dim i As Long
    For i = 0 To NumberOfControls
        If Check1(i).Value = 1 Then
            Label1(i) = Text1.Text
            Check1(i).Value = 0
        End If
    Next i
    frmB.Label1 = frmA.Label1(ActiveLabel)
End Sub

If I had to deal with "if I select labell 3 from FORM (A) to get the same text in label 1 in FORM (B) is this possible, what is the code please" in a project of mine I would surely start prototyping the way I've shown ... so all I can do here is to provide an example and to explain what I'd do and why I'd do it ... if I'm asked ...


Best regards,
Raisor
Hi Raisor,

Your welcome, I just wanted to explain that part just so you understood exactly what NEW_INDIA required because I have answered a couple of his previous questions :) and have a good idea of what he is trying to achieve. Obviously there are some parts that I was stuck on myself but had a good idea of the concepts but couldnt put them into code :)

Anyway I hope that helps him out and I am sure he can apply the coding to his project and move to the next phase.
Hi,

If this question was not about an internal relationship, I'd believe that I've provided a working solution in this case ... I had no more feedback from the asker, explaining what was missing to him to make it work on his systems ... since this question was unfortunately abandoned by the asker I'd suggest "forced accepted: Raisor{https://www.experts-exchange.com/questions/21340920/Get-data-from-one-form-in-another.html#13484111}" ...


Best regards,
Raisor