docmd.searchforrecord

M

Mark Andrews

I borrowed some code from a Microsoft template basically to have a combobox
at the top of the form so if the user want to jump to a different record
they can by selecting a combobox choice. The following code is used:

Private Sub cboGoToPledge_AfterUpdate()
Dim strPledgeID As String

If (IsNull(Me.cboGoToPledge)) Then
Exit Sub
End If
On Error Resume Next
If (Me.Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
strPledgeID = CStr(Me.cboGoToPledge)
If (Me.Form.FilterOn) Then
DoCmd.RunCommand acCmdRemoveFilterSort
End If
DoCmd.SearchForRecord , "", acFirst, "[PledgeID]=" & strPledgeID

End Sub

It works fine when the form is opened normally to an existing record.

However if I open the form as:
DoCmd.OpenForm "frmPledge",,,,acFormAdd, acDialog

the code to jump to a different record does not work.

Anyone know how I could tweak it to make it work?

Mark
 
D

Douglas J. Steele

I believe that opening a form with the Data Mode set to acFormAdd is
equivalent to setting the form's DataEntry property to True, which doesn't
actually determine whether records can be added; it only determines whether
existing records are displayed.
 
D

Dirk Goldgar

Mark Andrews said:
I borrowed some code from a Microsoft template basically to have a combobox
at the top of the form so if the user want to jump to a different record
they can by selecting a combobox choice. The following code is used:

Private Sub cboGoToPledge_AfterUpdate()
Dim strPledgeID As String

If (IsNull(Me.cboGoToPledge)) Then
Exit Sub
End If
On Error Resume Next
If (Me.Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
strPledgeID = CStr(Me.cboGoToPledge)
If (Me.Form.FilterOn) Then
DoCmd.RunCommand acCmdRemoveFilterSort
End If
DoCmd.SearchForRecord , "", acFirst, "[PledgeID]=" & strPledgeID

End Sub

It works fine when the form is opened normally to an existing record.

However if I open the form as:
DoCmd.OpenForm "frmPledge",,,,acFormAdd, acDialog

the code to jump to a different record does not work.

Anyone know how I could tweak it to make it work?


Using acFormAdd for the DataMode argument, you have opened the form to
display only new records. The record you're seeking doesn't even exist in
the form's recordset. If that wasn't your intention, remove that argument.

If it was your intention, set the form's DataEntry property to False before
calling DoCmd.SearchForRecord:

If (Me.FilterOn) Then
DoCmd.RunCommand acCmdRemoveFilterSort
End If
If (Me.DataEntry) Then
Me.DataEntry = False
End If
DoCmd.SearchForRecord , "", acFirst, "[PledgeID]=" & strPledgeID
 
M

Mark Andrews

Thanks! Switching the dataentry property back to false did the trick.

Mark

Douglas J. Steele said:
I believe that opening a form with the Data Mode set to acFormAdd is
equivalent to setting the form's DataEntry property to True, which doesn't
actually determine whether records can be added; it only determines whether
existing records are displayed.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Mark Andrews said:
I borrowed some code from a Microsoft template basically to have a
combobox at the top of the form so if the user want to jump to a different
record they can by selecting a combobox choice. The following code is
used:

Private Sub cboGoToPledge_AfterUpdate()
Dim strPledgeID As String

If (IsNull(Me.cboGoToPledge)) Then
Exit Sub
End If
On Error Resume Next
If (Me.Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
strPledgeID = CStr(Me.cboGoToPledge)
If (Me.Form.FilterOn) Then
DoCmd.RunCommand acCmdRemoveFilterSort
End If
DoCmd.SearchForRecord , "", acFirst, "[PledgeID]=" & strPledgeID

End Sub

It works fine when the form is opened normally to an existing record.

However if I open the form as:
DoCmd.OpenForm "frmPledge",,,,acFormAdd, acDialog

the code to jump to a different record does not work.

Anyone know how I could tweak it to make it work?

Mark
 
M

Mark Andrews

yep that works great.

Dirk Goldgar said:
Mark Andrews said:
I borrowed some code from a Microsoft template basically to have a
combobox at the top of the form so if the user want to jump to a different
record they can by selecting a combobox choice. The following code is
used:

Private Sub cboGoToPledge_AfterUpdate()
Dim strPledgeID As String

If (IsNull(Me.cboGoToPledge)) Then
Exit Sub
End If
On Error Resume Next
If (Me.Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
strPledgeID = CStr(Me.cboGoToPledge)
If (Me.Form.FilterOn) Then
DoCmd.RunCommand acCmdRemoveFilterSort
End If
DoCmd.SearchForRecord , "", acFirst, "[PledgeID]=" & strPledgeID

End Sub

It works fine when the form is opened normally to an existing record.

However if I open the form as:
DoCmd.OpenForm "frmPledge",,,,acFormAdd, acDialog

the code to jump to a different record does not work.

Anyone know how I could tweak it to make it work?


Using acFormAdd for the DataMode argument, you have opened the form to
display only new records. The record you're seeking doesn't even exist in
the form's recordset. If that wasn't your intention, remove that
argument.

If it was your intention, set the form's DataEntry property to False
before calling DoCmd.SearchForRecord:

If (Me.FilterOn) Then
DoCmd.RunCommand acCmdRemoveFilterSort
End If
If (Me.DataEntry) Then
Me.DataEntry = False
End If
DoCmd.SearchForRecord , "", acFirst, "[PledgeID]=" & strPledgeID


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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