Solved

Form requery not firing current event

Posted on 2003-11-07
6
593 Views
Last Modified: 2008-01-16
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
Comment
Question by:triplediamond
6 Comments
 
LVL 9

Expert Comment

by:svenkarlsen
ID: 9704209
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
 
LVL 2

Expert Comment

by:ericmca
ID: 9704642
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
 
LVL 2

Expert Comment

by:ericmca
ID: 9704705
OOps.  That should be    rst.AddNew
-- Eric
0
 

Author Comment

by:triplediamond
ID: 9718778
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
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 9743494
PAQed, with points refunded (250)

SpazMODic
EE Moderator
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now