Populate a form
-
Hi, I wanted to know how to populate a form once you have done a search on a record. What I am trying to do is that when I search for a record in the access database, once it finds the record I want it to go to another form where I have textboxes and it should populate the information in that form. The search is on a different form, so I have to switch between forms. If anybody can guide me on the right track I will appreciate it.
-
Hi, I wanted to know how to populate a form once you have done a search on a record. What I am trying to do is that when I search for a record in the access database, once it finds the record I want it to go to another form where I have textboxes and it should populate the information in that form. The search is on a different form, so I have to switch between forms. If anybody can guide me on the right track I will appreciate it.
I don't do alot of database interaction but I think I can help you with the interaction between the two forms. You will need to create a Public object reference from Form2 to Form1. Below is a short VB.NET sample that copies the text from a TextBox1 in Form1 to a TextBox2 in Form2. This sample will assume that you have already created two forms named: Form1 and Form2. In Form2, insert the following code right below the initial class/inherits declaration: Public Class Form2 Inherits System.Windows.Forms.Form 'Insert this code Public myObject as Object Now save the changes to Form2. One thing you may or may not know is that when Objects are passed to one another, the receiving object does not get a copy of the the passing object, it gets the memory reference to it! That is great news for us. We will now create code to pass Form1's object reference to Form2. Go to Form1 and it's code view. I will assume that you are showing Form2 by clicking a button of some sort, therefore I will use the sample name Button1. Private Sub Button1_Click(..) Handles Button1.Click 'sample form2 declaration Dim pForm2 as new Form2 ' here's where the object reference is passed! ' Me is the command for the current form pForm2.myObject = Me pForm2.Show End Sub The two forms are now linked based on the object reference. Now to illustrate this, create a textbox in each of the forms. (TextBox1 in Form1 and TextBox2 in Form2) Also, add a button to: FORM2. Form2 will be sending the text from TextBox2 to TextBox1. In Form2's designer mode double click on the newly created Button1 to open the code view and .net will automatically create the click handler sub. In that click handler, insert the following code: Private Sub Button1_Click(..) Handles Button1.Click 'Note: This button1 code should be in form2 not form1! 'this the code. "Ctype" is casting an unknown object 'to a known object aka. Form1 CType(myObject, Form1).Textbox1.Text = Textbox2.Text End Sub There you go! Now run the program and test the code. Form2's textbox should send text to Form1's textbox. You should be able to go from there. Good Luck! Richard Blythe
-
I don't do alot of database interaction but I think I can help you with the interaction between the two forms. You will need to create a Public object reference from Form2 to Form1. Below is a short VB.NET sample that copies the text from a TextBox1 in Form1 to a TextBox2 in Form2. This sample will assume that you have already created two forms named: Form1 and Form2. In Form2, insert the following code right below the initial class/inherits declaration: Public Class Form2 Inherits System.Windows.Forms.Form 'Insert this code Public myObject as Object Now save the changes to Form2. One thing you may or may not know is that when Objects are passed to one another, the receiving object does not get a copy of the the passing object, it gets the memory reference to it! That is great news for us. We will now create code to pass Form1's object reference to Form2. Go to Form1 and it's code view. I will assume that you are showing Form2 by clicking a button of some sort, therefore I will use the sample name Button1. Private Sub Button1_Click(..) Handles Button1.Click 'sample form2 declaration Dim pForm2 as new Form2 ' here's where the object reference is passed! ' Me is the command for the current form pForm2.myObject = Me pForm2.Show End Sub The two forms are now linked based on the object reference. Now to illustrate this, create a textbox in each of the forms. (TextBox1 in Form1 and TextBox2 in Form2) Also, add a button to: FORM2. Form2 will be sending the text from TextBox2 to TextBox1. In Form2's designer mode double click on the newly created Button1 to open the code view and .net will automatically create the click handler sub. In that click handler, insert the following code: Private Sub Button1_Click(..) Handles Button1.Click 'Note: This button1 code should be in form2 not form1! 'this the code. "Ctype" is casting an unknown object 'to a known object aka. Form1 CType(myObject, Form1).Textbox1.Text = Textbox2.Text End Sub There you go! Now run the program and test the code. Form2's textbox should send text to Form1's textbox. You should be able to go from there. Good Luck! Richard Blythe
will this work if I have a search textbox in form2 and when I hit the search button on form2 it should go to the database and pull that record out and display all the information on form1 filling in all the textboxes on form1. Please let me know. Thanks for all your help so far.
-
will this work if I have a search textbox in form2 and when I hit the search button on form2 it should go to the database and pull that record out and display all the information on form1 filling in all the textboxes on form1. Please let me know. Thanks for all your help so far.
I took a class in database interaction but I my memory on that topic is foggy. Here is some simple code that will search through an employee table for the first name and update form1 accordingly. This code will assume that you have linked your textboxes in Form1 to the database. It also assumes the you have created the object reference mentioned in the last email. In form2, create the two following items: a button named btnSearch and a textbox named Textbox1. Below is click handler for btnSearch: Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click With CType(myObject, Form1) ' create a boolean var Dim blnFoundMatch As Boolean 'initialize a for loop to search all records (if necessary) For intX As Integer = 0 To .DsEmployees1.tblEmployees.Count - 1 'See if the current fldFirstName = Textbox1's text If .DsEmployees1.tblEmployees.Item(intX).fldFirstName = TextBox1.Text Then blnFoundMatch = True 'update form1 with the contents of row num: intX .BindingContext(.DsEmployees1, "tblEmployees").Position = intX Exit For End If Next If blnFoundMatch = False Then MessageBox.Show("No match was found on " & TextBox1.Text, "No Match", MessageBoxButtons.OK, MessageBoxIcon.Information) Else MessageBox.Show(TextBox1.Text & " was found and form1 has been updated.", "MatchFound", MessageBoxButtons.OK, MessageBoxIcon.Information) End If End With End Sub Hope this helps you. I'll be leaving on a business trip after tommorrow but feel free to write anytime. Richard
-
Hi, I wanted to know how to populate a form once you have done a search on a record. What I am trying to do is that when I search for a record in the access database, once it finds the record I want it to go to another form where I have textboxes and it should populate the information in that form. The search is on a different form, so I have to switch between forms. If anybody can guide me on the right track I will appreciate it.
The simplest way I have found to communicate between classes (aka forms) is with public PO Boxes in a module. I'm not sure what form you data is taking after being extracted from the database, but, for example, a string object can be passed between forms with a string PO Box. The PO Box may look like this: Module M_Common Public strPOBox01 As String End Module In the form that extracts the data from the database, place the text needed by the second form in the PO Box thusly: strPOBox01 = myTextData In the form that populates the controls, extract the text from the PO Box just as simply: myTextBox.Text = strPOBox01 You can see more complex examples of this technique in this article where entire records are being exchanged via one PO Box. George
-
I took a class in database interaction but I my memory on that topic is foggy. Here is some simple code that will search through an employee table for the first name and update form1 accordingly. This code will assume that you have linked your textboxes in Form1 to the database. It also assumes the you have created the object reference mentioned in the last email. In form2, create the two following items: a button named btnSearch and a textbox named Textbox1. Below is click handler for btnSearch: Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click With CType(myObject, Form1) ' create a boolean var Dim blnFoundMatch As Boolean 'initialize a for loop to search all records (if necessary) For intX As Integer = 0 To .DsEmployees1.tblEmployees.Count - 1 'See if the current fldFirstName = Textbox1's text If .DsEmployees1.tblEmployees.Item(intX).fldFirstName = TextBox1.Text Then blnFoundMatch = True 'update form1 with the contents of row num: intX .BindingContext(.DsEmployees1, "tblEmployees").Position = intX Exit For End If Next If blnFoundMatch = False Then MessageBox.Show("No match was found on " & TextBox1.Text, "No Match", MessageBoxButtons.OK, MessageBoxIcon.Information) Else MessageBox.Show(TextBox1.Text & " was found and form1 has been updated.", "MatchFound", MessageBoxButtons.OK, MessageBoxIcon.Information) End If End With End Sub Hope this helps you. I'll be leaving on a business trip after tommorrow but feel free to write anytime. Richard
I tried your suggestion and for some reason I am not able to get information to the other form. Is there anyway you can write me a small program, so I can have a visual presentation of what you are talking about. I would really appreciate that. Please let me know. Thanks
-
The simplest way I have found to communicate between classes (aka forms) is with public PO Boxes in a module. I'm not sure what form you data is taking after being extracted from the database, but, for example, a string object can be passed between forms with a string PO Box. The PO Box may look like this: Module M_Common Public strPOBox01 As String End Module In the form that extracts the data from the database, place the text needed by the second form in the PO Box thusly: strPOBox01 = myTextData In the form that populates the controls, extract the text from the PO Box just as simply: myTextBox.Text = strPOBox01 You can see more complex examples of this technique in this article where entire records are being exchanged via one PO Box. George
I tried your suggestion, but with no luck. I was not able to get the information to the other form. I have a search textbox on form2 and when I hit the search button on form2 it should find that record and bring up form1 where there are textboxes and datetimepicker that should be updated automatically according to the record information and I am unable to do that. Any ideas
-
I tried your suggestion, but with no luck. I was not able to get the information to the other form. I have a search textbox on form2 and when I hit the search button on form2 it should find that record and bring up form1 where there are textboxes and datetimepicker that should be updated automatically according to the record information and I am unable to do that. Any ideas
You've posted exactly the same comment and question in response to two very different responses to your original question. Which suggestion did you try? If you tried the other person's idea, I can't help you. If you tried my suggestion, did you first read the article I referenced in my first response? The demo project in that article provides a couple examples of passing entire records back and forth between forms using a technique that has been proven over and over to work.
-
You've posted exactly the same comment and question in response to two very different responses to your original question. Which suggestion did you try? If you tried the other person's idea, I can't help you. If you tried my suggestion, did you first read the article I referenced in my first response? The demo project in that article provides a couple examples of passing entire records back and forth between forms using a technique that has been proven over and over to work.
well I tried both. yes i read the article, but that article is bringing up the records in a datagrid and then you select a record and then hit change, but what I want is to put a search field for my example it will be barcodeID and then i hit the search button that is when it should go to the other form and bring up the information. hope that helps. thanks
-
well I tried both. yes i read the article, but that article is bringing up the records in a datagrid and then you select a record and then hit change, but what I want is to put a search field for my example it will be barcodeID and then i hit the search button that is when it should go to the other form and bring up the information. hope that helps. thanks
Let me try one last time. I guess I'm not being very clear. This is what I think should be happening. (This is pretty much identical to what's happening in the demo project in the article you read.) 1. In Form 1, the Search button is clicked. 2. In Form 1, the Search_Click event places the barcode ID into a public PO string variable that's located in a module. 3. In Form 1, the Search_Click event displays Form 2 as a dialog box. 4. In Form 2, the Form_Load event retrieves the barcode ID from the public PO string variable and then uses that barcode ID to retrieve the associated record from the database. 5. In Form 2, the Form_Load event then displays the associated data from the retrieved record. If you can't make this work, please don't ask me again because I can't get any simpler except to write your program and I'm not willing to do that.
-
Let me try one last time. I guess I'm not being very clear. This is what I think should be happening. (This is pretty much identical to what's happening in the demo project in the article you read.) 1. In Form 1, the Search button is clicked. 2. In Form 1, the Search_Click event places the barcode ID into a public PO string variable that's located in a module. 3. In Form 1, the Search_Click event displays Form 2 as a dialog box. 4. In Form 2, the Form_Load event retrieves the barcode ID from the public PO string variable and then uses that barcode ID to retrieve the associated record from the database. 5. In Form 2, the Form_Load event then displays the associated data from the retrieved record. If you can't make this work, please don't ask me again because I can't get any simpler except to write your program and I'm not willing to do that.
Alright it makes sense now that you explained it. I will try to take what you said and make the changes and see what happens. I don't want you to write my program, just wanted to know how it worked with my situation, since I could not figure it out. Thanks for all your help.