How to go to new record when opening form from command if no link

D

doyle60

The following code works. It opens a form and goes to the
corresponding record if one exists (where CompPOID = CompPOID). The
code is built from the wizard's code but is augmented by me so it
doesn't filter the form it goes to. (I always hated the fact that the
wizard does that.) Anyway, the annoying thing is that it simply goes
to the first record if there is no link, that is, when the form being
opened does not have a corresponding CompPOID. I'd rather it go to a
new record. How can I amend the code to do that?

Here is the code:
___________________________________

On Error GoTo Err_Command5425_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim rsFind As Recordset

stDocName = "CompPOfrm"

stLinkCriteria = "[CompPOID]=" & Me![CompPOID]
DoCmd.OpenForm stDocName
Set rsFind = Forms(stDocName).RecordsetClone
rsFind.FindFirst stLinkCriteria
Forms(stDocName).Bookmark = rsFind.Bookmark

Exit_Command5425_Click:
Exit Sub

Err_Command5425_Click:
MsgBox Err.Description
Resume Exit_Command5425_Click
___________________________________

Thanks,

Matt
 
M

Marshall Barton

The following code works. It opens a form and goes to the
corresponding record if one exists (where CompPOID = CompPOID). The
code is built from the wizard's code but is augmented by me so it
doesn't filter the form it goes to. (I always hated the fact that the
wizard does that.) Anyway, the annoying thing is that it simply goes
to the first record if there is no link, that is, when the form being
opened does not have a corresponding CompPOID. I'd rather it go to a
new record. How can I amend the code to do that?

Here is the code:
___________________________________

On Error GoTo Err_Command5425_Click

Dim stDocName As String
Dim stLinkCriteria As String
Dim rsFind As Recordset

stDocName = "CompPOfrm"

stLinkCriteria = "[CompPOID]=" & Me![CompPOID]
DoCmd.OpenForm stDocName
Set rsFind = Forms(stDocName).RecordsetClone
rsFind.FindFirst stLinkCriteria
Forms(stDocName).Bookmark = rsFind.Bookmark


Try this:

stLinkCriteria = "[CompPOID]=" & Me![CompPOID]
DoCmd.OpenForm stDocName
With Forms(stDocName).RecordsetClone
.FindFirst stLinkCriteria
If .NoMatch Then
DoCmd.GoToRecord acDataForm, "stDocName", acNewRec
Else
Forms(stDocName).Bookmark = .Bookmark
End If
 
D

dymondjack

I would use a recordset clone to check for the existence of a record, and if
..NoMatch = True Then

DoCmd.GoToRecord, acNewRec


--
Jack Leach
www.tristatemachine.com

- "First, get your information. Then, you can distort it at your leisure."
- Mark Twain
 
D

doyle60

Marshall,
I am trying to do you method. But I got an error:

"The object 'stDocName' isn't open.

"The macro you are running (directly or indirectly) contains a
GoToRecord, ReparintObject, or SelectObject action, but the Object
Name argument names an object that is closed."

Thanks,

Matt
 
D

doyle60

I forgot to mention that it works just fine when there is a link but
fails when there is not a link and should go to a new record.

Matt
 
M

Marshall Barton

I am trying to do you method. But I got an error:

"The object 'stDocName' isn't open.

"The macro you are running (directly or indirectly) contains a
GoToRecord, ReparintObject, or SelectObject action, but the Object
Name argument names an object that is closed."


Sorry, that line should not have quotes:

DoCmd.GoToRecord acDataForm, stDocName, acNewRec

You should seriously consider using something like what Linq
posted if you want to set the new record's linking field's
value. Even if it wasn't your question, I believe that in
many ways, it is better for the form to do things to itself
instead of calling forms/code doing things to it.
 

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