Reset Form and Tab to start new record after a save

B

Belinda

How can I reset My Form and rest the Tab to the beginning after the form has
been used to insert a new record into the database?
Also, how can I get a form to restart to enter another ID for a record search?
 
J

Jack Leach

How can I reset My Form

If you mean go to a fresh record, put this line in the event procedure for
the button you're using...

DoCmd.GotoRecord , , acNewRec
and rest the Tab to the beginning

force the focus to be on a specific control by using the SetFocus method:

Me.ControlName.SetFocus



In the OnCurrent event (whenever access mores to a record of any sort), you
will need to check if its a new record and set the focus accordingly:

If Me.NewRecord Then Me.ControlName.SetFocus


So between your command button and the current record you'll have something
like this:

Private Sub cmdNewRec_Click
DoCmd.GotoRecord , , acNewRec
End Sub

Private Sub Form_Current()
If Me.NewRecord = True Then
Me.ControlName.Setfocus
End If
End Sub




Also, how can I get a form to restart to enter another ID for a record search?

This is a bit trickier. You need an unbound control (not tied to a field of
the table) for the user to search by. I usually include this in a standard
header bar of the form. Drag-n-drop a textbox from the toolbar to your form
(no controlsource) and you will have an unbound control.

Then you need a button (or read the key changes) to determine when the
record is to try and go to the entered ID. I use the enter key event and a
button (btnGo) next to it for this.

Then you need to find the record in the code, probably by using the Bookmark
event of the form's recordsetclone. Search the recordset clone for a match
and go to it if there's one. I'll code this behind the click event of btnGo,
to have it work for the enter key you'll want the KeyDown event (If KeyCode =
## Then ... I forget the the keycode for Enter off the top of my head)

This assumes your ID datatype is a long integer. If it's a string you'll
have to add quotes accordingly (noted after the code)



Private Sub btnGo_Click()
Dim rs As DAO.Recordset 'for the recordsetclone to search
Dim strCriteria As String 'what to search for

'Set the focus to your text control (ctlGo) so you can read the text
property
Me.ctlGo.SetFocus

'Make sure they entered something in the search control
If Len(Nz(Me.ctlGo.Text, "")) = 0 Then
MsgBox "Please enter a search value"
Exit Sub
End If

'Set the Criteria
strCriteria = "[IDField] = " & Me.ctlGo.Text

'Set the Recordset
Set rs = Me.RecordsetClone

'Find the record
rs.FindFirst strCriteria
If rs.NoMatch Then
MsgBox "Record Not Found"
Else
'Set the form's bookmark to the clone's bookmark
'this sets the active record to the match found
Me.Bookmark = rs.Bookmark
End If

'Close the recordset object
rs.Close
Set rs = Nothing

'Clear the search field
Me.ctlGo.SetFocus
Me.ctlGo.Text = ""

End Sub






if the ID happens to be a string, change the criteria so it's enclosed in
quotes, like so:

strCriteria = "[IDField] = """ & Me.ctlGo.Text & """"





hth
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
B

Belinda

Jack
Thank you for your help. :)
--
Many thanks, Belinda


Jack Leach said:
How can I reset My Form

If you mean go to a fresh record, put this line in the event procedure for
the button you're using...

DoCmd.GotoRecord , , acNewRec
and rest the Tab to the beginning

force the focus to be on a specific control by using the SetFocus method:

Me.ControlName.SetFocus



In the OnCurrent event (whenever access mores to a record of any sort), you
will need to check if its a new record and set the focus accordingly:

If Me.NewRecord Then Me.ControlName.SetFocus


So between your command button and the current record you'll have something
like this:

Private Sub cmdNewRec_Click
DoCmd.GotoRecord , , acNewRec
End Sub

Private Sub Form_Current()
If Me.NewRecord = True Then
Me.ControlName.Setfocus
End If
End Sub




Also, how can I get a form to restart to enter another ID for a record search?

This is a bit trickier. You need an unbound control (not tied to a field of
the table) for the user to search by. I usually include this in a standard
header bar of the form. Drag-n-drop a textbox from the toolbar to your form
(no controlsource) and you will have an unbound control.

Then you need a button (or read the key changes) to determine when the
record is to try and go to the entered ID. I use the enter key event and a
button (btnGo) next to it for this.

Then you need to find the record in the code, probably by using the Bookmark
event of the form's recordsetclone. Search the recordset clone for a match
and go to it if there's one. I'll code this behind the click event of btnGo,
to have it work for the enter key you'll want the KeyDown event (If KeyCode =
## Then ... I forget the the keycode for Enter off the top of my head)

This assumes your ID datatype is a long integer. If it's a string you'll
have to add quotes accordingly (noted after the code)



Private Sub btnGo_Click()
Dim rs As DAO.Recordset 'for the recordsetclone to search
Dim strCriteria As String 'what to search for

'Set the focus to your text control (ctlGo) so you can read the text
property
Me.ctlGo.SetFocus

'Make sure they entered something in the search control
If Len(Nz(Me.ctlGo.Text, "")) = 0 Then
MsgBox "Please enter a search value"
Exit Sub
End If

'Set the Criteria
strCriteria = "[IDField] = " & Me.ctlGo.Text

'Set the Recordset
Set rs = Me.RecordsetClone

'Find the record
rs.FindFirst strCriteria
If rs.NoMatch Then
MsgBox "Record Not Found"
Else
'Set the form's bookmark to the clone's bookmark
'this sets the active record to the match found
Me.Bookmark = rs.Bookmark
End If

'Close the recordset object
rs.Close
Set rs = Nothing

'Clear the search field
Me.ctlGo.SetFocus
Me.ctlGo.Text = ""

End Sub






if the ID happens to be a string, change the criteria so it's enclosed in
quotes, like so:

strCriteria = "[IDField] = """ & Me.ctlGo.Text & """"





hth
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



Belinda said:
How can I reset My Form and rest the Tab to the beginning after the form has
been used to insert a new record into the database?
Also, how can I get a form to restart to enter another ID for a record search?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top