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
LVL 1
janjuama84Asked:
Who is Participating?
 
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
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.