Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Learned something new.



    When using an append query in VBA, I've always written a function to retrieve the new PK number.
    When I use the ".AddNew" syntax, I'll grab the PK number before the ".Update" command.
    I'll have to try it. Maybe I can cut down on some code.

    Thanks, Paul

  2. #17
    vicsaccess's Avatar
    vicsaccess is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    451
    i believe it was this article from Allen Browne that i first found and started using @@identity. https://bytes.com/topic/access/answe...ng-insert-into
    as for my where condition, any more thoughts? i did find something that works but i don't think i like it just because it looks messy. i moved my sqlrs2 SQL and set rs2 inside the first loop just under the execute command along with declaring a variable that pulls the circularid from rs1 and then use that variable inside a where clause for my sqlrs2 SQL. not sure if this is making sence so i'll add the code again.

    Code:
    Private Function circular1()
        Dim db As DAO.Database
        Dim rs1 As DAO.Recordset '''circular table'''
        Dim rs2 As DAO.Recordset '''circular craft table'''
        Dim sqlrs1 As String '''circular sql'''
        Dim sqlrs2 As String '''circular craft sql'''
        Dim x As Integer
        Dim n As Date
        Dim c As Integer
        
        sqlrs1 = "SELECT * FROM circularT" '''get circular table'''
        
        
        Set db = CurrentDb
        Set rs1 = db.OpenRecordset(sqlrs1)
        
        
        With rs1 '''loop thru circulart'''
            If Not .BOF And Not .EOF Then '''check to see if on a record'''
                .MoveFirst
                    Do Until .EOF
                    If (!DueDate - Date) <= 0 Then '''check due dates that are due today or past'''
                        '''insert main job into record'''
                        CurrentDb.Execute ("INSERT INTO workqueT(circularid, locid, deptid, systemid, assetid, componentid,workid,summary,detail,statusid) " & _
                        "VALUES(" & !circularid & "," & !LocID & "," & !DeptID & "," & !SystemID & "," & !AssetID & "," & _
                         !ComponentID & "," & !WorkID & ",""" & !Summary & """,""" & !detail & """," & 1 & ") ")
                        c = !circularid
                        n = !DueDate + 5 '''set new due date'''
                        CurrentDb.Execute ("UPDATE circularT SET duedate=#" & Format(n, "mm/dd/yyyy") & "#  WHERE circularid=" & !circularid & " ")
                        
                        x = CurrentDb.OpenRecordset("select @@identity")(0) '''get unique ID'''
                            sqlrs2 = "SELECT * FROM circularcraftT WHERE circularid=" & c '''get circular craft table'''"
                            Set rs2 = db.OpenRecordset(sqlrs2)
                            With rs2 '''loop thru circularcraftt'''
                                If Not .BOF And Not .EOF Then
                                    .MoveFirst
                                    Do Until .EOF
                                    '''insert the crafts for the main jobs'''
                                    CurrentDb.Execute ("INSERT INTO WorkquecraftT(workqueID,craftid,hours,craftnotes,statusid) " & _
                                    "VALUES (" & x & "," & !CraftID & ",'" & !Hours & "',""" & !CraftNotes & """,'" & 5 & "')  ")
                                    '''WHERE CircularID=" & c & "
                            
                                    .MoveNext
                                    Loop
                                    .Close
                                End If
                            End With
                        
                
                        
                    End If
                    
                    .MoveNext
                    Loop
                .Close
            End If
        End With
        
        Set rs2 = Nothing
        Set rs1 = Nothing
        Set db = Nothing
        
    End Function
    so far it works but not sure if its be best way. thanks

  3. #18
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problemo.

    Heads up, if you switch to SQL Server you'll want to grab the PK after the .Update. It won't work before. Note in Allen's code he does it after, which will work in either environment.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #19
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    i moved my sqlrs2 SQL and set rs2 inside the first loop just under the execute command along with declaring a variable that pulls the circularid from rs1 and then use that variable inside a where clause for my sqlrs2 SQL.
    You are absolutely correct. This is the proper place for those two lines.
    -------------------------------------------------------


    You have "Set db = CurrentDb" but then use
    Code:
    CurrentDb.Execute ("UPDATE circularT SET duedate=#" & Format(n, "mm/dd/yyyy") & "#  WHERE circularid=" & !circularid & " ")
    So why have "Set db = CurrentDb"??
    Maybe change "CurrentDb.Execute...." to "db.Execute...."??? (4 places in the code)

    When using "Execute", you should also use ", dbFailOnError"
    In the above update query, you could use "c" in the WHERE clause instead of "!circularid"
    -------------------------------------------------------


    Also, you have programmed in a couple of bombs in your code.
    Let me ask you: "What data type is "!circularid"?
    Answer:In the tables, it is an Autonumber or a Long Integer.

    However, in code, you have:
    Code:
        Dim x As Integer
        Dim c As Integer
    .
    .
    .
    .
        c = !circularid
        x = CurrentDb.OpenRecordset("select @@identity")(0)    '''get unique ID'''
    Here, you have defined "c" and "x" as Integers.
    Access is very forgiving and automatically casts the Long Integers as Integers.
    But what happens when the numbers that are assigned to "c" or "x" exceeds 32,767 (the max for an Integer)?
    Boom!! The code blows up.

    You should have:
    Code:
       Dim x As Long   ' Dim x As Integer
       Dim c As Long  ' Dim c As Integer



    --------------------------------------------------------
    I don't know if you understand why I changed to SQL lines.
    Which would be easier to debug the SQL statements (queries)?
    Code:
       CurrentDb.Execute ("UPDATE circularT SET duedate=#" & Format(n, "mm/dd/yyyy") & "#  WHERE circularid=" & !circularid & " ")
    or
    Code:
             sSQL = "INSERT INTO WorkquecraftT(workqueID,craftid,hours,craftnotes,statusid)"
             sSQL = sSQL & " VALUES (""" & NewIdent & """,""" & !CraftID & """,""" & !Hours & """,""" & !CraftNotes & """,'" & 5 & "')"
            '        Debug.Print sSQL       'for debugging
             db.Execute sSQL, dbFailOnError
    -------------------------------------------------------

    As long as the code does what you want, you can write it any way you want.

    And thanks for the info about the @@identity code.

  5. #20
    vicsaccess's Avatar
    vicsaccess is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    451
    hey i did it correct, yea i'm starting to learn I don't have a good answer for using currentdb, will change, not used to using the shorthand. circularid is a number, its my PK in circular and FK in circularcraftT. good catch on the integer, i hadn't thought that far in the future but i'm sure it will get to the long. i also still need to change the "c" and "x" to a better description. as for the SQL's i thought that was only because you thought it was a SQLserver. yes it does make it easier, i just do it the other way again out of early habits. thanks for all your help. i'll get these changes done in the morning.

  6. #21
    vicsaccess's Avatar
    vicsaccess is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    451
    So far its working great, at least until i change the update duedate part with a real section of code to allow different lengths of reoccurrence. but that would be a new post. Thanks everyone for helping

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 09-23-2015, 02:29 PM
  2. Replies: 1
    Last Post: 11-04-2014, 12:07 PM
  3. Replies: 6
    Last Post: 10-27-2014, 08:05 PM
  4. Replies: 5
    Last Post: 01-29-2014, 02:42 PM
  5. Input Forms - How To Input Multiple Fields/Records?
    By butterbescotch in forum Forms
    Replies: 1
    Last Post: 04-04-2013, 06:30 AM

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