Shodin

Hi all,

What's the best way to clear a record that's currently being displayed on a form so I can enter in a new record


Re: Visual Basic Express Edition Clearing a currently displayed record to enter a new one

ReneeC

What kind of record




Re: Visual Basic Express Edition Clearing a currently displayed record to enter a new one

Shodin

I'm basically pulling a record from the DB and displaying the data in text boxes, combo boxes, check boxes, etc. on the form. What I'm wanting to do is have the user click "New Record" from the File menu and have it clear all the data in those controls and release any temporary data that was stored when the previous record was pulled.




Re: Visual Basic Express Edition Clearing a currently displayed record to enter a new one

Shodin

Is this what I'm looking for

Me.BindingSourceName.Clear()


-= EDIT =-

Okay I think I figured this out. I needed to do:
Me.DataSetName.Clear()

instead of:
Me.BindingSourceName.Clear()

However, I'm not sure why the BindingSourceName.Clear option is even there. Can anyone explain what it actually does and/or how to use it properly





Re: Visual Basic Express Edition Clearing a currently displayed record to enter a new one

Solitaire

The code for Button1 should clear all the text, etc. from the controls on your form. If you have other controls that should not be cleared, then add a Tag property to those controls that should (or shoud not) be cleared and use the code for Button2. If you have other controls not mentioned here, then add them to the If block.

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each ctl As Control In Me.Controls

If TypeOf ctl Is TextBox Then

CType(ctl, TextBox).Clear()

ElseIf TypeOf ctl Is Label Then

CType(ctl, Label).Text = ""

ElseIf TypeOf ctl Is CheckBox Then

CType(ctl, CheckBox).Checked = False

ElseIf TypeOf ctl Is RadioButton Then

CType(ctl, RadioButton).Checked = False

ElseIf TypeOf ctl Is ComboBox Then

CType(ctl, ComboBox).Items.Clear()

End If

Next ctl

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

For Each ctl As Control In Me.Controls

If ctl.Tag Is "record" Then

'If Not ctl.Tag Is "record" Then

If TypeOf ctl Is TextBox Then

CType(ctl, TextBox).Clear()

ElseIf TypeOf ctl Is Label Then

CType(ctl, Label).Text = ""

ElseIf TypeOf ctl Is CheckBox Then

CType(ctl, CheckBox).Checked = False

ElseIf TypeOf ctl Is RadioButton Then

CType(ctl, RadioButton).Checked = False

ElseIf TypeOf ctl Is ComboBox Then

CType(ctl, ComboBox).Items.Clear()

End If

End If

Next ctl

End Sub






Re: Visual Basic Express Edition Clearing a currently displayed record to enter a new one

Shodin

Thanks, that's interesting. It doesn't clear the recordset though which is really the most important thing of all to make sure that the user truly is dealing with a brand new slate.

The edit I made to the post prior to yours I think is what I'm really needing. It not only clears the recordset, but clears all the data in the controls as well. From there I just had to paste the various Fill commands after the Clear command in order to repopulate default infomation in some of the combo boxes.




Re: Visual Basic Express Edition Clearing a currently displayed record to enter a new one

Shodin

Solitaire wrote:

The code for Button1 should clear all the text, etc. from the controls on your form. If you have other controls that should not be cleared, then add a Tag property to those controls that should (or shoud not) be cleared and use the code for Button2. If you have other controls not mentioned here, then add them to the If block.

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

For Each ctl As Control In Me.Controls

If TypeOf ctl Is TextBox Then

CType(ctl, TextBox).Clear()

ElseIf TypeOf ctl Is Label Then

CType(ctl, Label).Text = ""

ElseIf TypeOf ctl Is CheckBox Then

CType(ctl, CheckBox).Checked = False

ElseIf TypeOf ctl Is RadioButton Then

CType(ctl, RadioButton).Checked = False

ElseIf TypeOf ctl Is ComboBox Then

CType(ctl, ComboBox).Items.Clear()

End If

Next ctl

End Sub




Bringing up an oldie here that I'm wanting to get clarified. When I use this method to simply clear the tab on a form, it gets hung up on groupboxes. It doesn't see any of the controls in any groupbox. If I tell it to clear the controls in a specific groupbox, it doesn't go into any of the child groupboxes in the parent groupbox.


So short of doing a 'For Each' for each groupbox, is there a way to code it to where it'll include all groupboxes in that original 'For Each'





Re: Visual Basic Express Edition Clearing a currently displayed record to enter a new one

Shodin

Nevermind, I figured it out. Not much of a better solution than doing a few 'For Each''s in a row, but I guess it's really the only way to do it. You can keep doing this for as many nexted groupboxes as you have.

Code Block

For Each ctl As Control In Me.tabMainContact.Controls
If TypeOf ctl Is TextBox Then
CType(ctl, TextBox).Clear()
ElseIf TypeOf ctl Is ComboBox Then
CType(ctl, ComboBox).SelectedValue = vbEmpty
ElseIf TypeOf ctl Is GroupBox Then
For Each gctl As Control In ctl.Controls
If TypeOf gctl Is TextBox Then
CType(gctl, TextBox).Clear()
ElseIf TypeOf gctl Is ComboBox Then
CType(gctl, ComboBox).SelectedValue = vbEmpty
ElseIf TypeOf gctl Is GroupBox Then
For Each sgctl As Control In gctl.Controls
If TypeOf sgctl Is TextBox Then
CType(sgctl, TextBox).Clear()
ElseIf TypeOf gctl Is ComboBox Then
CType(sgctl, ComboBox).SelectedValue = vbEmpty
End If
Next
End If
Next
End If
Next








Re: Visual Basic Express Edition Clearing a currently displayed record to enter a new one

js06

I haven't tried it but i think this is basically what you are looking for

For Each ctl As Control In Me.Controls

If TypeOf ctl Is GroupBox Then

If ctl.HasChildren Then

For Each gbctl As Control In ctl.Controls

If TypeOf gbctl Is TextBox Then

CType(gbctl, TextBox).Clear()

End If

Next

End If

ElseIf TypeOf ctl Is TextBox Then

CType(ctl, TextBox).Clear()

End If

Next ctl

EDIT -----------------------------------------

I see you got it already