Pass values from one form to another

Posted on 2006-05-03
Last Modified: 2010-05-01

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?

Question by:janjuama84
    LVL 18

    Assisted Solution

    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.
    LVL 18

    Expert Comment

    Declare a Global variable in Form1 and populate it.
    Public strGlobal as String

    Then use it in Form2 this way:
    Msgbox Form1.strGolbal
    LVL 18

    Expert Comment

    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"

    Expert Comment

    i agree with Sethi,

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

    do you need examples?

    LVL 1

    Expert Comment

    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
    LVL 11

    Accepted Solution

    Also, you could use a custom event in your form that has the questions.  Here's a small example

    Public WithEvents frm As Form2

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

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

    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...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now