Textbox in TabPage not always loading data from table

Paul Goldstein 116 Reputation points
2021-07-19T18:53:47.9+00:00

I have a form linked to an Access table.
The table contains Student information.
Because of the vast amount of information for each student, I have created a TabControl to handle information about the student’s parents.
After listing the parents name as two separate textboxes on the form: txtParent1 and txtParent2, the TabControl "tcParentContacts" is directly to the right of the two TextBoxes.
There are 4 TabPages in tcParentContacts:
tabpgCellPhone, which contains: mtxtParent1Cell (MaskedTextBox), and mtxtParent2Cell (MaskedTextBox)
tabpgHomePhone, which contains: mtxtParent1Home (MaskedTextBox), and mtxtParent2Home (MaskedTextBox)
tabpgWorkPhone, which contains: mtxtParent1Work (MaskedTextBox), and mtxtParent2Work (MaskedTextBox)
and tabpgEmail, which contains: txtParent1Email (TextBox), and txtParent2Email (TextBox)

116006-tcparentcontacts.png

For some reason…which I haven’t as yet figured out…when navigating to a different record in the table, the Email fields are not updating to the information in the currently displayed record. This field is used by another form that is opened when the current record is displayed to extract communications with the parents. The problem is that the sub-form is using the wrong Email address to pull in Exchange info… it’s using the Email address from a previously displayed Student that was navigated to before the current student.

Any ideas would be greatly appreciated

Thank you for your time in advance,
Paul Goldstein

Developer technologies | Windows Forms
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2021-07-20T10:31:35.403+00:00

    Question, are you reading data from the database into a DataSet then setting the DataSet to a BindingSource then data binding to form controls? What happens when changing records without the other form open?

    If using a BindingSource you can subscribe to PositionChanged event and look at the raw data by casting Current property of the BindingSource to (if working with a DataTable) a DataRowView then access the Row followed by properties of the row.

    0 comments No comments

  2. Paul Goldstein 116 Reputation points
    2021-07-20T21:42:03.797+00:00

    Hi Karen (@Karen Payne MVP ),

    Thank you for writing.  
    This is what I am doing:  
    
    1. I have an Access Table: tblBMList
    2. The database contains a query, qryBMList, that sorts the table by LastName, FirstName and also adds a field FirstName & " " & LastName as StudentName for other purposes.
    3. I dragged qryBMList onto a blank form, frmBMList which then created a BindingNavigator and a DataGridView
    4. I deleted the DataGridView since I want to view table one record at a time…and since each record contains vast amounts of data and also uses sub-Tables that contain other "transactional" data.
    5. I then either: A) Dragged individual fields from the Query onto the form, or used the Toolbox for: DateTimePickers, MaskedTextBoxes, CheckBoxes, etc. and then assigned them corresponding fields in tblBMList by using the DataBindings in the Properties window. This is how I created the fields in the TabControl.
    6. In addition to the MoveFirst, MovePrevious, MoveNext, MoveLast Item buttons in the BindingNavigator, I also added a ToolStripComboBox: cboBMList to jump directly to a specific Student’s record. cboBMList is initialized by using the following code: Me.cboBMList.ComboBox.DataSource = Me.QryBMListBindingSource Me.cboBMList.ComboBox.DisplayMember = "StudentName" Me.cboBMList.ComboBox.ValueMember = "RecordKey"
      I use the Sub QryBMListBindingSource_CurrentChanged to populate the non-Bound fields in the form.

    I placed a debugging loop in that Sub to dump out the values of the fields in the TabControl:

    		Dim tabCurrent As TabPage  
    		Dim ctlCurrent As Control  
    		Debug.Print("Debugging Me.tcParentContacts")  
    		For Each tabCurrent In Me.tcParentContacts.TabPages  
    			Debug.Print($"In: {tabCurrent.Name}")  
    			For Each ctlCurrent In tabCurrent.Controls  
    				Debug.Print($"In: {ctlCurrent.Name}, {ctlCurrent.Text}")  
    			Next  
    		Next  
    

    This is what I found:
    The only fields that always get printed, are the two fields in the first tab: tabpgCellPhone.
    The other fields only get printed if I pre-Select one of the other TabPages before navigating to it (the fields of tabpgCellPhone always get printed regardless of which TabPage is selected.

    So, I modified the routine and added two lines of code before the loop:

    		Dim tabSelected As TabPage  
    		tabSelected = Me.tcParentContacts.SelectedTab  
    

    and inserted another line withing the loop:

    			Me.tcParentContacts.SelectedTab = tabCurrent  
    

    and then, one more line after the loop to go back to the originally displayed TabPage:

    		Me.tcParentContacts.SelectedTab = tabSelected  
    

    This had the desired effect of displaying all of the Parent1 and Parent2 information fields.

    In addition, I cleaned up some code that was being fired inadvertently when some of the Date fields were being updated by the when the navigation changed, but that had independent TextChanged properties that would update other non-Bound fields in the form. Once I did that, then the Parent information…specifically the Email field problem went away.

    So I am left with one question: when I call the sub-Form that does the email inquiry, I do have the code select the Email TabPage in order to get to the Email fields.
    Is there any other way of getting to the data…other than: QryBMListBindingSource.Current("<Email Field Name>").ToString ?

    Again, thank you for your time in advance.

    Paul Goldstein


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.