Passing Variables without making them Global

Morn

Active member
Joined
Dec 4, 2006
Messages
40
Programming Experience
Beginner
I feel that I am forever asking questions in this place and never able to help answer other peoples.

Alas:

I want to pass a variable from form 1 to form 2, the information that the variable is to contain is a customer number that a SQL statement has pulled out of a database for me.

Having searched for 2 hrs solid and only being able to pass a variable with nothing in it to the new form I have surrendered.

I need the code or a step by step to get it, please help, I'm on the edge.

Here's the code that I have already:

To retrieve the data (code in form 1)

VB.NET:
'retieves the customerID for next form
            Dim da As New OleDb.OleDbDataAdapter("SELECT customerID from tblCustomer WHERE customerfirstname = firstname AND customersurname = surname AND postcode = postcode", connect)

            Dim ds As New DataSet

            da.Fill(ds)

            Dim f2 As New bookingfrmPt1
            f2.custID = da
To store variable in form 2:

VB.NET:
 Public custID
followed by a sql insert statement later in the form.

Thanks in advanced for any help, and how long does it take to understand this stuff. I am still so lost its scary.

Graham
 
This what i have in the first form, it now is saying that the custID is not declared however it is.

VB.NET:
 'Selects that last entry that was inserted into the database
            Dim custID As Integer
            Dim sqlsearch As String = "SELECT TOP 1 customerID FROM tblCustomer WHERE postcode = '" & postcode & "' ORDER BY customer DESC"

            Dim command1 As New System.Data.OleDb.OleDbCommand(sqlsearch)

            custID = command1.ExecuteScalar()
            command1.Dispose()

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'closes(connection)
            connect.Close()
        End Try


        MsgBox("The Connection to the Database is now Closed")

        Dim bookingfrmPt1 As bookingfrmPt1 = New bookingfrmPt1(custID)
        bookingfrmPt1.Show()

        Me.Close()
 
Try:

VB.NET:
Dim bookingfrmPt1 As bookingfrmPt1 = New bookingfrmPt1()
bookingfrmPt1.custID = custID
bookingfrmPt1.Show()

if it doesn't work...

Does custID contain the correct value just before this line?
Dim bookingfrmPt1 As bookingfrmPt1 = New bookingfrmPt1(custID)

Where exactly does the error occur?
 
Put your custID declaration outside of the try-end try block.


VB.NET:
        [B]Dim custID As Integer[/B]
 
        Try
            ' Some other codes
            ..............................

            'Selects that last entry that was inserted into the database
            Dim sqlsearch As String = "SELECT TOP 1 customerID FROM tblCustomer WHERE postcode = '" & postcode & "' ORDER BY customer DESC"

            Dim command1 As New System.Data.OleDb.OleDbCommand(sqlsearch)

            custID = command1.ExecuteScalar()
            command1.Dispose()

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            'closes(connection)
            connect.Close()
        End Try


        MsgBox("The Connection to the Database is now Closed")

        Dim bookingfrmPt1 As bookingfrmPt1 = New bookingfrmPt1(custID)
        bookingfrmPt1.Show()

        Me.Close()
 
Problem is resolved.

Thanks for the help that everyone gave me on this, the main problem was in the sql statement. I was trying to do a query that would return the value 0 every time that it ran hence the problem.

Also I had the sql in the catch statement and as a result this wouldn’t run unless the first part of the statement failed.

Thanks guys,

Very grateful.

Graham
 
Back
Top