• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

Form requery not firing current event

Hello,
I have a main form in Access 2000 with a query as a recordsource. The query simply obtains the PK of tblStart (PK is "AS nRespondentUID", which is different than in the table). The main form has several subforms (all 1 to 1), notably a subform based on tblStart. I know that's odd, and I could add another table if necessary but somehow I don't think that's the problem.

I want to have a textbox (Text53) in the main form (default value null) where one can enter a PK value and that will be used to determine the current record. So if the textbox is null (as it will be when the form is first opened) it doesn't open any record; when the textbox matches an existing PK, it goes to that record; and when the textbox doesn't match an existing PK, one is created (using qryNewRowAllQ) then it goes to that newly created record.

Sounds simple but I've had the hardest time. I've gotten a similar thing to work with a combobox.

Using the following code, if I enter a new PK value in the text box, a row is created, then the requery event fires and the number of records in the navigation bar is updated, but the Form_Current() procedure doesn't run. I think it should run, which should then navigate to the appropriate record. But Form_Current doesn't run at all after the requery.

I get a non fatal (meaning that even if I don't trap it nothing stops) "Current error" message from my MsgBox when the form is opened for the first time, but not after the requery.

So after the requery, the textbox appears to hold the new PK value I entered, but the record is still the first record in the form recordsource.

Private Sub Form_Current()
    On Error GoTo ErrorHandler
    MsgBox "Current"
    If Not IsNull([Text53]) Then
        ' Find the record that matches the control.
        Dim rsc As DAO.Recordset
        Set rsc = Me.Recordset.Clone
        rsc.FindFirst "[nRespondentUID] = " & Str(Me![Text53])
        If (Not rsc.NoMatch) Then
            Me.Bookmark = rsc.Bookmark
        End If
        rsc.Close
    Text53.Value = [nRespondentUID]
    End If
    DoCmd.GoToControl "Text53"
    ErrorHandler:
        MsgBox "Current error"
End Sub

Private Sub Text53_AfterUpdate()
    On Error GoTo ErrorHandler
    ' Find the record that matches the control.
    Dim rs As DAO.Recordset
    Set rs = Me.Recordset.Clone
    rs.FindFirst "[nRespondentUID] = " & Str(Me![Text53])
    If (rs.NoMatch) Then
        DoCmd.OpenQuery "qryNewRowAllQ"
        Me.Requery
        rs.FindFirst "[nRespondentUID] = " & Str(Me![Text53])
    End If
    Me.Bookmark = rs.Bookmark
    rs.Close
    ErrorHandler:
        MsgBox "afterupdate error"
End Sub


triplediamond
0
triplediamond
Asked:
triplediamond
1 Solution
 
svenkarlsenCommented:
I'd drop the two events and put the code under the click event of a button, - that'd be more obvious to new users, and for skilledusers you could just place the button after the textbox-control in the tab-sequence so that you could just hit enter twice to execute.

Regards,
Sven
0
 
ericmcaCommented:
Triplediamond,
Are you aware that there is no way around your ErrorHandler code in both your subs?  You will get the error message every time the sub runs (your "nonfatal error").  Better put an Exit Sub before ErrorHandler:

In the Textbox after_update code, I'm not sure what query you are opening when there is a NoMatch.  It seems you are still looking for the UID you couldn't find earlier.  Can you just use a rst.MoveNew here?

-- Eric
0
 
ericmcaCommented:
OOps.  That should be    rst.AddNew
-- Eric
0
 
triplediamondAuthor Commented:
Here is the code that works for me. I disabled the navigation buttons because they don't work together with this code. The other comments were helpful, especially the one about Exit Sub, but my solution really has nothing to do with the comments so I'd like to request a refund if no one strongly objects.

Actually I suspect there's a better way to do this and if someone wants to post it here I'd be more than happy to award them the points. It seems like a pretty general purpose problem.

Private Sub Form_Current()
    On Error GoTo ErrorHandler
    'Text53.Value = Str(Nz(Me![nRespondentUID], 0))

    DoCmd.GoToControl "Page1"
    Exit Sub
ErrorHandler:
    MsgBox "current error"
End Sub

Private Sub Text53_AfterUpdate()
    On Error GoTo ErrorHandler
    'Find the record that matches the control.
    Dim rs As DAO.Recordset
    Set rs = Me.Recordset.Clone
    rs.FindFirst "[nRespondentUID] = " & Str(Me![Text53])
    If (rs.NoMatch) Then
        DoCmd.GoToRecord , , acNewRec
        Me![nRespondentUID] = CInt(Nz(Me![Text53], 0))
        DoCmd.OpenQuery "qryNewRowAllQExceptTblQStart"
        Me.Refresh
        Else
            Me.Bookmark = rs.Bookmark
    End If
    rs.Close
    Exit Sub
ErrorHandler:
    MsgBox "afterupdate error"
End Sub


triplediamond

0
 
SpazMODicCommented:
PAQed, with points refunded (250)

SpazMODic
EE Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now