Results 1 to 4 of 4
  1. #1
    JWawrzak is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Oct 2016
    Posts
    2

    query not checking for newly created record


    I created form called "frmInputNewRec", to automate the creation of new document numbers. Unfortunately the query attached to this form does not check for the newly created document number. How to fix the code so this will work corectly.
    Below is the code for this form:

    Private Sub Building_AfterUpdate()


    Dim dbs As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim rst As DAO.Recordset
    Dim sNum As String
    Dim nextnum As String
    Dim stDocName As String
    Dim BuildingDef As String
    Dim tdf As DAO.TableDef
    Dim DocNum As String
    BuildingDef = Forms!frmInputNewRec!Building


    Set dbs = CurrentDb
    Set tdf = dbs.TableDefs("DocumentRegistry")
    Set rst = tdf.OpenRecordset()
    Set qdf = dbs.QueryDefs("qryLastNum")
    qdf.Parameters("Forms!frmInputNewRec!Building") = Forms!frmInputNewRec!Building
    Set rst = qdf.OpenRecordset()

    Me.Requery

    MsgBox BuildingDef
    MsgBox rst!DocNum

    nextnum = Val(Right(rst!DocNum, 4)) + 1

    MsgBox nextnum, vbYesNoCancel

    If vbYes Then


    DoCmd.OpenForm "DocumentRegistry", adOpenDynamic, adLockOptimistic
    With rst!DocNum

    DoCmd.GoToRecord , , acNewRec

    Forms!DocumentRegistry!DocNum() = (Forms!frmInputNewRec!Building & (Val(Right(rst!DocNum, 4)) + 1))
    DoCmd.RefreshRecord
    End With
    rst.Close
    Set rst = Nothing

    Else:
    MsgBox "Canceling Entry"
    DoCmd.Close
    End If


    Set qdf = Nothing
    Set rst = Nothing
    Set dbs = Nothing

    And here is the SQL for the query:

    SELECT DocumentRegistry.DocNum
    FROM DocumentRegistry RIGHT JOIN Activity_Registry ON DocumentRegistry.DocNum = Activity_Registry.DocNum
    WHERE (((DocumentRegistry.DocNum) Like [Forms]![frmInputNewRec]![Building] & "-####") AND (([Activity_Registry]![DocNum])=[DocumentRegistry]![DocNum]))
    ORDER BY DocumentRegistry.DocNum DESC , Activity_Registry.DocNum DESC

    Thank for any suggestion that will help me solve this problem.

    Jozef A. Wawrzak

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Is "new document numbers" a new record in a table? Maybe your sub is in the wrong place, try putting it in the form "AfterUpdate".

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Welcome to the forum, Jozef.

    I am confused about what you are trying to do. You have a form "frmInputNewRec", but in the code you open another form??

    Is this a multi-used dB?
    If yes, how many users a one time?
    Is the db split?

    I would probably use the form before uodate event if there are other fields that need to have data entered.
    If it is only enter "building" (name?), then using the control after update event would be OK.

    You could post your dB with just a couple of example records.......


    I did revise your code.. this is what I came up with:
    Code:
    Private Sub Building_AfterUpdate()
    
        Dim dbs As DAO.Database
        Dim rst As DAO.Recordset
    
        Dim iNextNum As Integer
        Dim sBuildingDef As String
        Dim sSQL As String
    
        Set dbs = CurrentDb
    
        sBuildingDef = Forms!frmInputNewRec!Building
    
        sSQL = "SELECT TOP 1 DocumentRegistry.DocNum"
        sSQL = sSQL & " FROM DocumentRegistry RIGHT JOIN Activity_Registry ON DocumentRegistry.DocNum = Activity_Registry.DocNum"
        sSQL = sSQL = " WHERE DocumentRegistry.DocNum Like " & sBuildingDef & "*;"
        sSQL = sSQL = " ORDER BY DocumentRegistry.DocNum DESC , Activity_Registry.DocNum DESC"
        '    Debug.Print sSQL
    
        Set rst = dbs.OpenRecordset(sSQL)
        If Not rst.BOF And Not rst.EOF Then
            MsgBox sBuildingDef
            MsgBox rst!DocNum
    
            iNextNum = Val(Right(rst!DocNum, 4)) + 1
    
            MsgBox iNextNum, vbYesNoCancel
    
            If vbYes Then
                sSQL = "INSERT INTO DocumentRegistry ( DocNum )"
                sSQL = sSQL & " VALUES ('" & (sBuildingDef & iNextNum) & "');"
                '    Debug.Print sSQL
                dbs.Execute sSQL, dbFailOnError
            End If
    
        End If
    
        rst.Close
        Set rst = Nothing
        Set dbs = Nothing
    
    End Sub
    Not saying it will work, but ............

  4. #4
    JWawrzak is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Oct 2016
    Posts
    2
    The form "frmInputNewRecord" should create a new record in the table called "Document Registry" where DocNum field is indexed - no duplicates. The table "DocumentRegistry is linked with another yet table "Activity_Registry" where duplicates for DocNum are OK.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Newly added record Search
    By galadrwin in forum Access
    Replies: 2
    Last Post: 02-10-2015, 06:27 AM
  2. Replies: 2
    Last Post: 04-04-2013, 05:56 PM
  3. Replies: 7
    Last Post: 03-12-2013, 07:18 PM
  4. New Field Name on Newly created query
    By inan25 in forum Queries
    Replies: 3
    Last Post: 05-13-2009, 09:05 PM
  5. Best Way to Return a Newly Created Index?
    By Jerimiah33 in forum Programming
    Replies: 5
    Last Post: 09-06-2006, 12:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums