gisman2000
New member
- Joined
- Nov 2, 2009
- Messages
- 1
- Programming Experience
- 1-3
I'm still new to VB programming. I built a sample Contacts database with associated forms. I have two tables; one called Family and the other called Member. It's a parent/child (one-to-many) relationship. The family table contains the family name and general information while the member table contains detailed information about each member of the family.
I built a form using textboxes at the top of the form which shows the family information and a datagridview at the bottom of the form listing all the family members. I built a custom "Search" for the form where you can double-click the textbox for Family name and it clears the form. You type in the family name you want and hit "Enter" and it querys the database and populates the Family portion of the form and the datagridview with the correct family members.
I have run into two problems though. I can't clear the datagridview and I get a "ConstraintException was unhandled" error. Specifically "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints".
I read on another forum where I can set my enforceconstraints to false, which I do and it works but I really don't like doing that because when I try to reset it back to true I get another error.
My questions;
1) How do I clear the entire form, including the DataGridView?
2) How do I reset the enforceconstraints to True?
Below is the code I use for the search when you hit "Enter":
Private Sub FamilyNameTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FamilyNameTextBox.KeyDown
If e.KeyCode = Keys.Enter And FamilySearch = 1 Then
btnSearch.Text = "Enable Search"
btnSearch.UseVisualStyleBackColor = True
'Me.FamilyandFriendsDataSet.EnforceConstraints = False
Me.FamilyTableAdapter.FillByLastName(Me.FamilyandFriendsDataSet.Family, FamilyNameTextBox.Text)
Me.MemberTableAdapter.Fill(Me.FamilyandFriendsDataSet.Member)
FamilySearch = 0
lblSearch.Text = "(Double-Click to enter Search Mode)"
lblSearch.BackColor = Color.Empty
End If
End Sub
Below is the code I use when you double-click the textbox to clear the form:
Private Sub FamilyNameTextBox_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles FamilyNameTextBox.DoubleClick
If btnSearch.Text = "Enable Search" Then
lblSearch.Text = "(Press Enter to search for Family)"
lblSearch.BackColor = Color.LightPink
FamilySearch = 1
resetForm()
btnSearch.Text = "Search"
btnSearch.BackColor = Color.LightPink
End If
End Sub
Private Sub resetForm()
Dim x As Integer
For x = 0 To Me.Controls.Count - 1
If TypeOf Me.Controls.Item(x) Is TextBox Or TypeOf Me.Controls.Item(x) Is MaskedTextBox Then
Me.Controls.Item(x).Text = ""
End If
Next
End Sub
This form is still an early draft, work in progress.
I appreciate any advice.
Thanks.
I built a form using textboxes at the top of the form which shows the family information and a datagridview at the bottom of the form listing all the family members. I built a custom "Search" for the form where you can double-click the textbox for Family name and it clears the form. You type in the family name you want and hit "Enter" and it querys the database and populates the Family portion of the form and the datagridview with the correct family members.
I have run into two problems though. I can't clear the datagridview and I get a "ConstraintException was unhandled" error. Specifically "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints".
I read on another forum where I can set my enforceconstraints to false, which I do and it works but I really don't like doing that because when I try to reset it back to true I get another error.
My questions;
1) How do I clear the entire form, including the DataGridView?
2) How do I reset the enforceconstraints to True?
Below is the code I use for the search when you hit "Enter":
Private Sub FamilyNameTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FamilyNameTextBox.KeyDown
If e.KeyCode = Keys.Enter And FamilySearch = 1 Then
btnSearch.Text = "Enable Search"
btnSearch.UseVisualStyleBackColor = True
'Me.FamilyandFriendsDataSet.EnforceConstraints = False
Me.FamilyTableAdapter.FillByLastName(Me.FamilyandFriendsDataSet.Family, FamilyNameTextBox.Text)
Me.MemberTableAdapter.Fill(Me.FamilyandFriendsDataSet.Member)
FamilySearch = 0
lblSearch.Text = "(Double-Click to enter Search Mode)"
lblSearch.BackColor = Color.Empty
End If
End Sub
Below is the code I use when you double-click the textbox to clear the form:
Private Sub FamilyNameTextBox_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles FamilyNameTextBox.DoubleClick
If btnSearch.Text = "Enable Search" Then
lblSearch.Text = "(Press Enter to search for Family)"
lblSearch.BackColor = Color.LightPink
FamilySearch = 1
resetForm()
btnSearch.Text = "Search"
btnSearch.BackColor = Color.LightPink
End If
End Sub
Private Sub resetForm()
Dim x As Integer
For x = 0 To Me.Controls.Count - 1
If TypeOf Me.Controls.Item(x) Is TextBox Or TypeOf Me.Controls.Item(x) Is MaskedTextBox Then
Me.Controls.Item(x).Text = ""
End If
Next
End Sub
This form is still an early draft, work in progress.
I appreciate any advice.
Thanks.