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!

VB Form Object Referencing

Status
Not open for further replies.

dataman86

Technical User
Oct 11, 2008
104
US
I have created a Form Template to use in creationof several forms. I am trying to reference fields on one form with fields from another form. The first form holds 12 lines and the 13 line shows up on another instance of another form by using a Next Page Button. I have an accounting application that has data on Page 1 with 12 Lines and when I click the Next Page, my second form pops up with Line 13 and totals all 12 fields on the first form and adds the 13 field on the second form. I am trying to write out to a text file all 13 lines, but wghen I try to reference fields for the 13 line I get an error message: Object reference not set to an instance of an object. I know the computer does not know what the fields are, thats the reason for the error. Is there a way to reference a particular field by using the My.Application.OpenForms.Item(FormName)? Or maybe use a My.forms.Form1.textBox00 someway? The field names that I have are Fields created with an array and named TextBox00, 10, 20, 30, 40 and so on.

What is the correct coding to reference a field?
 
By default this works:

Code:
Dim NewForm As NewForm2
NewForm.Show()
MessageBox.Show(NewForm.TextBox00.Text)

But when you reference the other form, you need to make sure you are referencing the correct instance. If your application keeps creating new copies of Form1/Form2 and you close them and try to reference them, then there is a possibility they are gone.
 
Riverguy,

I get the Object reference not set to an instance of an object on: LawsonForm1.Line13.Text = Me.Controls("textBox120").Text.ToString. All I need is to get the data in textbox120 to showup on another form3 when I reference.



If RowCount = 13 Then
LawsonForm1.Show()
LawsonForm1.Line1.Text = Me.Controls("TextBox00").Text.ToString
LawsonForm1.Line2.Text = Me.Controls("TextBox10").Text.ToString
LawsonForm1.Line3.Text = Me.Controls("TextBox20").Text.ToString
LawsonForm1.Line4.Text = Me.Controls("TextBox30").Text.ToString
LawsonForm1.Line5.Text = Me.Controls("TextBox40").Text.ToString
LawsonForm1.Line6.Text = Me.Controls("TextBox50").Text.ToString
LawsonForm1.Line7.Text = Me.Controls("TextBox60").Text.ToString
LawsonForm1.Line8.Text = Me.Controls("TextBox70").Text.ToString
LawsonForm1.Line9.Text = Me.Controls("TextBox80").Text.ToString
LawsonForm1.Line10.Text = Me.Controls("TextBox90").Text.ToString
LawsonForm1.Line11.Text = Me.Controls("TextBox100").Text.ToString
LawsonForm1.Line12.Text = Me.Controls("TextBox110").Text.ToString
LawsonForm1.Line13.Text = Me.Controls("textBox120").Text.ToString
 
From the looks of your code, you need to capitalize "textBox120" ("TextBox120").
 
RiverGuy,

I am still getting the error. LawsonForm1 is not opening and the code never gets past the LawsonForm1.Line1.Text = Me.Controls("TextBox00").Text.ToString

Dataman86
 
When you have debugging issues, it's sometimes best to troubleshoot by process of elimination. That being said, highlight the following block of code
Code:
            LawsonForm1.Line1.Text = Me.Controls("TextBox00").Text.ToString
            LawsonForm1.Line2.Text = Me.Controls("TextBox10").Text.ToString
            LawsonForm1.Line3.Text = Me.Controls("TextBox20").Text.ToString
            LawsonForm1.Line4.Text = Me.Controls("TextBox30").Text.ToString
            LawsonForm1.Line5.Text = Me.Controls("TextBox40").Text.ToString
            LawsonForm1.Line6.Text = Me.Controls("TextBox50").Text.ToString
            LawsonForm1.Line7.Text = Me.Controls("TextBox60").Text.ToString
            LawsonForm1.Line8.Text = Me.Controls("TextBox70").Text.ToString
            LawsonForm1.Line9.Text = Me.Controls("TextBox80").Text.ToString
            LawsonForm1.Line10.Text = Me.Controls("TextBox90").Text.ToString
            LawsonForm1.Line11.Text = Me.Controls("TextBox100").Text.ToString
            LawsonForm1.Line12.Text = Me.Controls("TextBox110").Text.ToString
            LawsonForm1.Line13.Text = Me.Controls("textBox120").Text.ToString
then, press the "comment out selected lines" button on your Visual Studio Toolbar. This button looks like a bunch of little blue horizontal lines. Basically, what you want to do is take out all of that code without deleting it from your form, so that it is easy to add back later on.

Next, run your application and see if LawsonForm1 shows at all.

If it doesn't then you haven't instantiated the LawsonForm1 variable.

If it does open, then uncomment your next line of code
Code:
LawsonForm1.Line1.Text = Me.Controls("TextBox00").Text.ToString

Before you run your application, add a breakpoint on that line of code, and add the following entries to your watch window:
LawsonForm1.Line1
Me.Controls("TextBox00")

The watch window should tell you if these objects evaluate to anything or to nothing. If one of them evaluates to nothing, then you have a problem. Post back your results and we can narrow this down.
 
RiverGuy,

LawsonForm1 Opens Up, but I get the Object Reference Error in Yellow for the First Line after LawsonForm1.Show() when I F11 through the code.

DataMan86
 
Now you need to figure out if the Exceptio nis from

LawsonForm1.Line1

or

Me.Controls("TextBox00")
 
The coding works for Lines 1 through 12 with no problem. It is just when I have a transaction with 13 lines does this exception occur. So the problem I know is on: Me.Controls("TextBox00").Text.ToString. The computer does not know what TextBox00 is on the next form. The form with 12 lines is opening up as Page 1 and the Form with the 13th Line is opening up as well as Page 2 on the Menu Bar. when I try to write out to a text file what is in these fields on Page 1 and 2 that I get the error in trying to do so. The computer knows what the 12 lines are but does not have a clue as to what I trying to reference with Page 2 for the 13th line.
 
Did you try coding

OtherFormInstance.Controls("TextBox00").Text.ToString



OtherFormInstance being the variable for your other form?
 
I think RiverGuy already showed you the problem. When your 12 lines get filled and you hit your Next Page Button, the click event (of the Next Page Button) should instaniate your new form. Then show the form. Then on your second form (LawsonForm1) you can populate text boxes, or do whatever.

IOW, you need to instaniate any new form before you can do anything with it. Here is an example:

Dim LawsonForm1 As New LawsonForm1
LawsonForm1.Show()

Just my thoughts....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top