Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hoe can I Pass values from one form to another 2

Status
Not open for further replies.

sk8er1

Programmer
Jan 2, 2005
229
US
I have a dynamic sql statement that is built by query by form and the end result it is used as the datasource (dataset) for a datagrid. I want to use the same sql statement to create a crystal report. Problem is, how do I pass that string value to another form. This used to be easy.
My form which has the query by form...is frmCriteria
There is a function called BuildSQLString

My sql statement is sql = "SELECT * From tbl"
sql = sql & " WHERE " & sqlString

My form which has the report frmWalkList
I need to use the sql string to build the dataset for the
crystal report....I can't get it to work.

Dim CForm as frmCriteria
CForm.BuildString(sql)
 
I have a similar problem with 2 forms shown below.
Form1 has a button that opens Form2.
Form2 has a button that (I wish) puts text in a textbox on Form1

This is about as bare bones as it gets. No one I've talked to knows what to do. It appears that everytime you click the button on Form2 it creates another form. This can't be that difficult, I must be missing something simple. I can do this in VB6.
Code:
'==============
' FORM 1
'==============
Public Class Form1
    Inherits System.Windows.Forms.Form

+Region " Windows Form Designer generated code "

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm2 As New Form2
        frm2.ShowDialog()

    End Sub
End Class
Code:
'==============
' FORM 2
'==============

Public Class Form2
    Inherits System.Windows.Forms.Form

+Region " Windows Form Designer generated code "

       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm1 As New Form1
        frm1.TextBox1.Text = " TEST "
    End Sub
End Class
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

If I do this..........
Code:
        Dim frm1 As Testform.Form1
        frm1.TextBox1.Text = " TEST "
I get this error:

An unhandled exception of type 'System.NullReferenceException' occurred in Testform.exe
Additional information: Object reference not set to an instance of an object
 
This is the type of thing that Microsoft does that drives me crazy. I think they are not making it easier to program.
 
I tryed setting an object variable
Dim CForm as New frmCriteria

This gave me access to the function on the other form which is what I want. When I watch whats going on in the debugger, one of the values should be true, is now false.
Its looking at a checkbox.checked value, which is true...
when you look at it from CForm.BuildSQLString, its value is false.....Can this be explained....

Thanks
 
I am using the object variable to set a reference to the form...
Dim CForm as New frmCriteria

I now have access to the values in the form. I have a checkbox that is definitely checked...when I look at the value from the other form via x = CForm.chkCity.Checked
the value of x is false...I thought it should be true

Can someone explain what is happening....
Thanks
 
Ok, modify my code above in form2 to the following.
Code:
        Dim frm1 As New Form1
        frm1.TextBox1.Text = " TEST "
        frm1.Show()
what you will see is that you have created another instance of the form Form1 and that the text is there. In your case the box will be checked. I did not "Show" the form..... so frm1.TextBox1.Text = " TEST " does not refer to the first form as intended. There has to be a way to ref the first form.
 
Declarations:
Dim frm1 As Form1

Sub Routine:
If frm1 Is Nothing = True Then frm1 = New Form1
frm1.TextBox1.Text = " TEST "
frm1.Show()

 
Exactly what I was looking for thanx. Now on to my next crisis..;-)
 
Hey RG, is there any reason why you use

Code:
If frm1 Is Nothing = True Then frm1 = New Form1

as opposed to

Code:
If frm1 Is Nothing Then frm1 = New Form1

?

-Rick

----------------------
 
Although I don't code in it now, the first language I learned in college was Java, so I've always been used to explicitly testing against a boolean value.
 
I am still having an issue..

The checkbox value on my form frmCriteria is true
when i do the following i get a false value...

dim CForm as New frmCriteria

CForm.ChkCity.CheckState ' false

should be true....I dont get it.
 
Value is still false....anyone have code that works
 
dim CForm as New frmCriteria
CForm.ChkCity.CheckState ' false

Are you setting it to true in the properties window at design time or in the frmCriteria constructor? If not, then it will always be false.

-Rick

----------------------

[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Ok...maybe we are onto something.
Let me explain again. I have a form named frmCriteria. There are checkboxes with associated text box and values.
Zip: _____
x City: New York
Age: __ To: Age2: __

In this case, I have clicked the checkbox for city and entered New York in the textbox. When you click the checkbox, it enables the textbod. So I build a sql statement depending on what was entered into the text boxes. If they select city and age of 50 to 70....I build an SQL string and use that to fill a dataset for a grid which works fine. Then I want the ability to print a report depending on that sql statement. So I have another form with the crystal report. This crystal report needs the sql string to fill its dataset. SO I thought I could read the values of the checkboxes and call the function from the frmCriteria again and have the sql string for the dataset for the report.

If there is a better way...I am all ears.
Its the value of the checkboxes which is false...But I clicked it to enter city...IT SHOULD BE TRUE!!!!!
I DON'T GET IT.
 
if you have a form with a Crystal viewer on it. and you have the main form that has a string containing SQL. then you can add a parameter to the constructor of the CR form to take the sql string.

Add a private variable to the CR form, call it m_SQL or what ever you like.

in the "Window Generated Code" area there is a sub new(). change that to: Sub New(SQLString as string)

At the end of the sub new add a line: m_SQL = SQLString

then in your form_load event, or where ever you are initialising the Crystal Report, have it use m_SQL.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
WOW! That worked. It's amazing that you dont see that in any books. It worked great. Originally I wanted to just pass the sql string, but the old VB6 way does not work.
Any books have good stuff like this that one can learn from.

Thanks so much....A STAR FOR YOU.
 
The technical answer is in books. I'm pretty sure it's mentioned in WROX "Teach yourself VB.Net in [absurdly few] days!"

The theory behind it is not covered so well. Atleast not in books I have read. But once you run into a few technical issues like this, the theory of OO design starts making more sence.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top