Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening to specific record: Code not working, Please HELP 1

Status
Not open for further replies.

jasonmac

Programmer
Nov 14, 2002
175
US
Hello everyone. I must say this has completely frustrated me. I'm using acces '97 and I'm trying to open a form to a specific record.

This is the code that I am using to initially open the form

Code:
Private Sub Command18_Click()
    If Me.NewRecord Then
        MsgBox "Please enter a tool number to continue.", vbOKOnly, "APQP Warning"
        Exit Sub
    End If
    
    DoCmd.OpenForm "sbfrmCommentsTooling", acNormal, , , , , Forms!sbfrmActionItems!sbfrmTooling!txtID.Value

End Sub

And this is the code I'm using when the form opens to find the specific record.
Code:
Private Sub Form_Open(Cancel As Integer)
Dim rec As Recordset
DoCmd.Restore
    
    Set rec = Me.RecordsetClone
    
    If rec.RecordCount < 1 Then
        DoCmd.GoToRecord , , acNewRec
        Me!txtIDvalue.Value = Me.OpenArgs
    Else
    
        rec.FindFirst [ID] = Me.OpenArgs

        If rec.NoMatch Then
            DoCmd.GoToRecord , , acNewRec
            Me!txtIDvalue.Value = Me.OpenArgs
        Else
            Me.Bookmark = Me.RecordsetClone.Bookmark
        End If
    End If
    
End Sub

The Problem is that even when there is a matching record it still creates a new record. Can anyone tell me what I'm doing wrong here??
 
It might have to do with having the code in the form open event, where the form recordset might not be fully loaded yet.

You might try using the forms on load event in stead. I'd also try adding some quotes on the .FindFirst:

[tt]rec.FindFirst "[ID] =" & Me.OpenArgs[/tt]

and change the last line to:

[tt]Me.Bookmark = rec.Bookmark[/tt]

I'm a bit careful myself, and like to also perform a check on the openargs, if not isnull(me.openargs) then..., and perhaps some conversion:

[tt]rec.FindFirst "[ID] =" & clng(Me.OpenArgs)[/tt]

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top