Results 1 to 15 of 15
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191

    Insert Into Table

    Hi Guy's would you be kind enough to tell me where this is wrong please ?

    I am outputting 1 single record into tblJobSheets2 (this is to combine the quantity into 1 new record) there maybe 7 records for DelNo

    Then trying to run an Insert SQL in the tblJobSheets

    All is looking good apart from a refresh in the table (tblJobSheets), the data isn't transferring ?



    The iJS (Job Sheet Number) should match in both tables

    Code

    Code:
    If Forms!frmMainMenu!frmIndex3!txtCurrentSheet <> "" TheniJS = Forms!frmMainMenu!frmIndex3!txtCurrentSheet
    If Not IsNull(strVehicle = DLookup("Vehicle", "tblJobSheets2", "[Driver] = '" & strDriver & "' And [DelDate] = #" & dtDelDate & "# And [DelNo] = " & intDelNo)) Then
        DoCmd.CancelEvent
        Else
        Set rs2 = CurrentDb.OpenRecordset("Select * From tblJobSheets2")
        strItems = DCount("DelTo", "tblAssign", "[PostCode] = '" & strPC & "' And [DeliveryDate] = #" & dtDelDate & "#")
            If strItems >= 1 Then
            strAddDel = DCount("DelTo", "tblAssign", "[PostCode] = '" & strPC & "' And [DeliveryDate] = #" & dtDelDate & "#") & " " & "Lifts"
            End If
            If strItems = 1 Then
            strAddDel = DCount("DelTo", "tblAssign", "[PostCode] = '" & strPC & "' And [DeliveryDate] = #" & dtDelDate & "#") & " " & "Lift"
            End If
            If IsNull(strEntCode = DLookup("EntryCode", "tblDealers", "[PostCode] = '" & strPC & "'")) Then
            strEntCode = ""
            Else
            strEntCode = DLookup("EntryCode", "tblDealers", "[PostCode] = '" & strPC & "'")
            End If
            If IsNull(strExtCode = DLookup("ExitCode", "tblDealers", "[PostCode] = '" & strPC & "'")) Then
            strExtCode = ""
            Else
            strExtCode = DLookup("ExitCode", "tblDealers", "[PostCode] = '" & strPC & "'")
            End If
        With rs2
        .AddNew
        !IDNumber = iJS
        !DelDate = dtDelDate
        !Driver = strDriver
        !Vehicle = strVehicle
        !DelNo = intDelNo
        !DelTo = Me.DelTo & "-" & Me.Town & "-" & Me.PostCode
        !Items = strAddDel
        !EntCode = strEntCode
        !ExtCode = strExtCode
        .Update
        .Close
        End With
        
        Forms!frmMainMenu!frmIndex3!txtDropNo = Me.DelNo
        
        
        strSQL = "INSERT INTO tblJobSheets ( Driver, DelDate, Del" & intDelNo & ", CartonQty" & intDelNo & ", EntCode" & intDelNo & ", ExCode" & intDelNo & " ) " _
            & "SELECT tblJobSheets2.Driver, tblJobSheets2.DelDate, tblJobSheets2.DelTo, tblJobSheets2.Items, tblJobSheets2.EntCode, tblJobSheets2.ExtCode " _
            & "From tblJobSheets2 " _
            & "WHERE (((tblJobSheets2.IDNumber)= " & iJS & ") AND ((tblJobSheets2.DelNo) = " & intDelNo & "));"
        
        DoCmd.SetWarnings False
        DoCmd.RunSQL strSQL
        Debug.Print strSQL
        DoCmd.SetWarnings True
        
    End If
    End If
    Immediate Window

    Code:
    INSERT INTO tblJobSheets ( Driver, DelDate, Del7, CartonQty7, EntCode7, ExCode7 ) SELECT tblJobSheets2.Driver, tblJobSheets2.DelDate, tblJobSheets2.DelTo, tblJobSheets2.Items, tblJobSheets2.EntCode, tblJobSheets2.ExtCode From tblJobSheets2 WHERE (((tblJobSheets2.IDNumber)= 5219) AND ((tblJobSheets2.DelNo) = 7));

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Does the select work on it's own?

    I wish your indentation was a lot better.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Thanks WGM, what would be the best way to test that ?

    Do another query using select without append ?

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Im thinking is it the WHERE clause should be & "WHERE (((tblJobSheets.IDNumber)= " & iJS & ") AND ((tblJobSheets2.DelNo) = " & intDelNo & "));"

    not
    & "WHERE (((tblJobSheets2.IDNumber)= " & iJS & ") AND ((tblJobSheets2.DelNo) = " & intDelNo & "));"

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    No, just copy the relevant output of the debug and paste into a sql window.
    Then copy all of it and do the same, if incorrect, it should highlight where.

    Only you know your data.?, i am just looking at the syntax and the debug.print looks OK (to me at least?), but test anyway.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    It looks to me that your tblJobSheets table is a "summary" table not normalized (containing 7 sets of fields) so you would only append (insert) the first one (iJS=1) and you would UPDATE the rest (joining the two tables by the appropriate fields).

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    I have just seen you say
    I am outputting 1 single record into tblJobSheets2
    yet you have
    Code:
    "INSERT INTO tblJobSheets
    ????
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Hi Vlad, ahh ok, so this SQL is copied from an SQL of an append query and changed the field names based on the delivery number

    yes there are fields in tblJobSheets named Del1 , Del2, Del3 etc up to 10

    tblJobsheets has got a IDNumber (Primary Key)

    Once deliveries are assigned, it is looking up the Min IDNumber from tblJobSheets where the deldate is null (this is the next available record)

    The assignment is counting records and merging fields from tblAssgin ie: Vlad-Town-PostCode-7 Items

    If i assign all of Vlad's 7 records as drop number 1

    the tblJobSheets2 will have next (available job sheet number) DelNo 1 Delto Vlad-Town-Postcode Items = 7 Items

    Insert into tblJobSheets x 1 record into fields Del1, Name-Town-Postcode

    If you were assigned to drop number 4

    this would go into jblJobSheets Del4 field

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Sorry Dave but you are loosing me here, can you show us a couple of screen shots for both tables with related items? In tblJobSheets do you expect for each IDNumber (PK) to have all appropriate sets of fields populated (Del1 for Dave, Del2 for Vlad, etc.), which you will only achieve with a combination of insert (for Del1) and update (for the rest) or maybe with he elusive upsert query (https://stackoverflow.com/questions/6199417/upserting-in-ms-access). Or do you expect to end up with separate records for each DelNo (so only Del1 will be populated for Dave and the rest will be empty, Del2 for Vlad and the rest empty, etc.), which is what your current approach of only using inserts will do?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    So i'm trying to to tell IDNumber 5198 in tblJobSheets that in your Del (DelNo Field) if del4 then field del4 that the data needs to come from tblJobSheets2 where that also is 5198

  11. #11
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    here is the data stored in tblJobSheets2

    Click image for larger version. 

Name:	JS1.JPG 
Views:	18 
Size:	26.1 KB 
ID:	48128

    here where the lines are are where the data should transfer to, because drop 4 then into del4 field, if dave was drop2m, then field Del2

    tblJobSheets

    Click image for larger version. 

Name:	JS2.JPG 
Views:	19 
Size:	59.8 KB 
ID:	48129

  12. #12
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Wait a minute guy's the data IS GOING to tblJobSheets, it's not updating to that IDNumber, so false alarm, my code is working but how do I ensure i am not updating blank IDNumber ?

    Many thanks

  13. #13
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Do i need some kind of EDIT Record 5231 ? the testing records are going into the table just as a blank IDNumber

    Do you think i need to change this
    Code:
    & "WHERE (((tblJobSheets
    Code:
    2.IDNumber)= " & iJS & ") AND ((tblJobSheets2.DelNo) = " & intDelNo & "));"


    to this ?
    Code:
    & "WHERE (((tblJobSheets
    Code:
    .IDNumber)= " & iJS & ") AND ((tblJobSheets2.DelNo) = " & intDelNo & "));"


    But then it won't look in tblJobSheets2 for that IDNumber ?

  14. #14
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Do i need some kind of EDIT Record 5231
    Yes, that is what I've been saying, you only Insert once for the first record (Del1) for the rest you need to find that already existing record in tblJobSheets and update it; you do that via a UPDATE query based on the two tables where you join them by IDNumber.

    Or you can do both using one "upsert" query that inserts the record if none is present or updates it if present:
    https://stackoverflow.com/questions/...n-button-press

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  15. #15
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Ahh thanks Vlad, much appreciated, will try your suggestion

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

Similar Threads

  1. Replies: 1
    Last Post: 11-15-2019, 04:04 PM
  2. Replies: 7
    Last Post: 09-13-2017, 10:44 AM
  3. Replies: 5
    Last Post: 12-01-2014, 11:31 AM
  4. Replies: 2
    Last Post: 12-20-2011, 07:33 AM
  5. Replies: 2
    Last Post: 12-22-2010, 01:46 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