• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Pass values from one form to another

Hi

I am building a quiz in VB in which I am using multiple forms...

Form1 has two questions if users answers them rigth

variable marks = 2 and Form2 screen shows up...

I want to pass "marks" value to the form2 so I can calculate total marks...

How is that possible?

thanks
0
janjuama84
Asked:
janjuama84
2 Solutions
 
SethiCommented:
Add a standard module to your project. Declare a Global variable in the standard module. Populate the global variable in Form1 and use it in Form2.
0
 
SethiCommented:
OR
Declare a Global variable in Form1 and populate it.
Public strGlobal as String
strGlobal="Testing"

Then use it in Form2 this way:
Msgbox Form1.strGolbal
0
 
SethiCommented:
OR
Declare a Public procedure in Form2 that takes a parameter as value that you need to pass and call the procedure in Form1. Example:
In Form2:
Public Sub GlobalSub(strValue as String)
 Msgbox strValue
End Sub

In Form1:
Form2.GlobalSub "Testing"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
munzi79Commented:
i agree with Sethi,

The easiest way is to declare a global varible in a module,

do you need examples?

Regards,
Munzi
0
 
MN_DaveCommented:
If you don't want to use global variables you can create private variables on Form2 and access them using public properties.

For example Form2 might have a private variable myVar:
Private myVar AS String

Then you would create a property to access it:

Public Property Let myVariable(newValue As String)
        myVar= newValue
End Property

Then on form1 you would use the myVariable property:
Form2.myVariable = text1.text
Form2.Show 1, Me
0
 
leclairmCommented:
Also, you could use a custom event in your form that has the questions.  Here's a small example

Form1:
Public WithEvents frm As Form2

Private Sub Form_Load()
Set frm = New Form2
frm.Show
End Sub

Private Sub frm_GetMarks(intTotal As Integer)
MsgBox intTotal
End Sub


Form2:
Public Event GetMarks(intTotal As Integer)

Private Sub cmdSendTotal_Click()
RaiseEvent GetMarks(txtTotalMarks)
Unload Me
End Sub

When you press the button in form2, the total marks will be passed to form1.

Just trying to show alternatives...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now