Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195

    Updating Records Where Driver Not Already Assigned

    Hi Guy's I am having trouble with this one



    I am trying to find which driver name is not assigned for work on a Thursday, the issue updating warehouse cover to go to "Del To" Field (sSQL2)

    in Immediate window, all of the criteria match so i am trying to update but coming up with Runtime Error 3075 which i believe is data type ?

    Can you guy's see anything wrong with sSQL2 considering, this is the debug window ?

    much appreciated

    Debug.Print dtThur & " ; " & sThur & vbCrLf & strTask & " ; " & sDriver
    Imm Window
    14/03/2024 ; Thursday
    Warehouse Cover ; Drivers Name removed on Forum (already assigned with del no >0)


    Code:
    Dim dtMon As Date, dtFri As Date, dtDayMon As Date, dtDayFri As Date, dtNow As Date, dtThur As Date
    Dim sSQL As String, sSQL2 As String, sMon As String, sThur As String, sFri As String, strMonTask As String, strTask As String
    Dim sDriver As String
    Dim rs As DAO.Recordset
    
    
    
    
    dtNow = Format(Now(), "dd/mm/yyyy")
    
    
    If Weekday(Now()) = 6 Then
        dtFri = 13 - Weekday(Date) + Date
        dtMon = 9 - Weekday(Date) + Date
        dtThur = 12 - Weekday(Date) + Date
    Else
        dtFri = 6 - Weekday(Date) + Date
        dtMon = 2 - Weekday(Date) + Date
        dtThur = 5 - Weekday(Date) + Date
    End If
    
    
    sThur = WeekdayName(Weekday(dtThur, 0), False, 0)
    
    
    If Not IsNull(DLookup("DelDate", "tblRoutes", "[DelDate] = #" & dtThur & "#")) Then
        Set rs = CurrentDb.OpenRecordset("Select * From tblRoutes " _
                & "WHERE DelDate = #" & dtThur & "#")
            Do Until rs.EOF
                sDriver = rs.Fields("Driver")
                rs.MoveNext
            Loop
    End If
    
    
    sSQL = "INSERT INTO tblRoutes ( Driver ) " _
            & "SELECT tblStaff.Name " _
            & "From tblStaff " _
            & "WHERE (((tblStaff.Name) Not In ('" & sDriver & "') And ((tblStaff.DelsMail) = Yes)));"
    
    'ADDS ALL STAFF APART FROM (sDriver Correct, already assigned)
    
    
    DoCmd.SetWarnings False
        DoCmd.RunSQL sSQL
    DoCmd.SetWarnings True
    
    
    strTask = "Warehouse Cover"
    
    
    sSQL2 = "UPDATE tblRoutes SET tblRoutes.DelDate = #" & Format(dtThur, "mm/dd/yyyy") & "#, tblRoutes.DayName = '" & sThur & "', tblRoutes.DelTo = '" & strTask & "' " _
            & "WHERE (((tblRoutes.DelNo)=0) AND ((tblRoutes.DelDate) = #" & dtThur & "#) AND ((tblRoutes.Driver) <> & '" & sDriver & "'));"
    
    
    Debug.Print dtThur & " ; " & sThur & vbCrLf & strTask & " ; " & sDriver
    
    
    
    
    DoCmd.SetWarnings False
        DoCmd.RunSQL sSQL2
    DoCmd.SetWarnings True
    
    
    Forms!frmPlanning!frmPlanningDS.Requery

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You are not concatenating sDriver, but overwriting on each record?
    So you will always get the last one?
    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,195
    Ahh WGM, ok so you mean by concatenating sDriver = sDriver & rs.Fields etc... ?



  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    I will put SQL2 into a select query to try and get the results and change back to an update query

    that's usually not bad practice

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by DMT Dave View Post
    Ahh WGM, ok so you mean by concatenating sDriver = sDriver & rs.Fields etc... ?


    Yes, with , to separate. I am saying this as you are using IN in your sql.
    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 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You need to move the INSERT and UPDATE statements inside the Do Until rs.EOF loop...
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi Vlad, thank you, moved as suggested and as I understand we are looping the INSERT AND UPDATE, now had a tidy up

    do i still need to run SQL after loop ?

    also still having a problem with sSQL2

    as below

    Tidy code
    Code:
    sThur = WeekdayName(Weekday(dtThur, 0), False, 0)
    
    strTask = "Warehouse Cover"
    
    
    If Not IsNull(DLookup("DelDate", "tblRoutes", "[DelDate] = #" & dtThur & "#")) Then
        Set rs = CurrentDb.OpenRecordset("Select * From tblRoutes " _
            & "WHERE DelDate = #" & dtThur & "#")
        
        Do Until rs.EOF
            sDriver = rs.Fields("Driver")
                
        sSQL = "INSERT INTO tblRoutes ( Driver ) " _
            & "SELECT tblStaff.Name " _
            & "From tblStaff " _
            & "WHERE (((tblStaff.Name) Not In ('" & sDriver & "') And ((tblStaff.DelsMail) = Yes)));"
    
    
        sSQL2 = "UPDATE tblRoutes SET tblRoutes.DelDate = #" & Format(dtThur, "mm/dd/yyyy") & "#, tblRoutes.DayName = '" & sThur & "', tblRoutes.DelTo = '" & strTask & "' " _
            & "WHERE (((tblRoutes.DelNo)=0) AND ((tblRoutes.DelDate) = #" & dtThur & "#) AND ((tblRoutes.Driver) <> & '" & sDriver & "'));"
    
    
            rs.MoveNext
        Loop
    End If
    
    Debug.Print sSQL2
    
    
    DoCmd.SetWarnings False
        DoCmd.RunSQL sSQL
        DoCmd.RunSQL sSQL2
    DoCmd.SetWarnings True
    
    
    Forms!frmPlanning!frmPlanningDS.Requery
    Click image for larger version. 

Name:	Capture.JPG 
Views:	14 
Size:	13.7 KB 
ID:	51587

    Click image for larger version. 

Name:	Capture2.JPG 
Views:	14 
Size:	24.9 KB 
ID:	51588

    The only other part that i can change is something i see on this forum regular, formatting dtThur to mm/dd/yyyy (USA) instead of UK dd/mm/yyyy or not formatting at all

    I have just had other issues ona customer update file where records didn't out put all of a sudden, changed to formatting to mm/dd/yyyy and worked

    I have just noticed on line 1 of sSQL2, i have FOrmat(dtThur,mm/dd/yyyy") then on line 2, i haven't formatted

    do you think that is data reading issue ?

    thanks again

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    How many times do you need to be told?
    Debug.print your sql strings.
    The fact that that error message shows & tells me your concatenation is off. Also shouldn't the date be in mm/dd/yyyy format?
    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

  9. #9
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Hi WGM, yes i noticed i didn't format as mm/dd/yyyy stated that in my last post, going to change that now

    I have debug.print sSQL2 to try and see where the issue is, but going to log back in and format dates to USA format, i have noticed that sometimes dates work ok with dd/mm then in some areas doesn't work

  10. #10
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Getting Missing Operator so going to check delimiters in sSQL2 (even after formatting to mm/dd/yyyy)

    I think i have lost my self in trying lol

    all i was trying to do was to find driver names in staff where DelsMail = true

    check tblRoutes to see if no one is assigned for Thursday

    assign whoever is NOT assigned to Thursday to warehouse tasks

    but lost myself on route with it

  11. #11
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    This is my Debug.Print Window

    UPDATE tblRoutes SET tblRoutes.DelDate = #03/14/2024#, tblRoutes.DayName = 'Thursday', tblRoutes.DelTo = 'Warehouse Cover' WHERE (((tblRoutes.DelNo)=0) AND ((tblRoutes.DelDate) = #03/14/2024#) AND ((tblRoutes.Driver) <> & 'Joe Bloggs'));

    Removed correct driver name

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    why do you have an ampersand between <> and Joe?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Micron View Post
    why do you have an ampersand between <> and Joe?
    Yes, that is one of the issues. You should not see those.
    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

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well access knows you cannot have 14 months, so if you use 14/03/2024, it knows it is 14th March. However, 09/03/2024 would be taken as 3rd September.
    Use yyyy-mm-dd if you cannot remember to use the correct format.
    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

  15. #15
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,195
    Ahh ok WGM, thank you, never tried using yyyy first, always used dd/mm/yyyy or mm/dd/yyyy

    yes i did see 03/14/2024 in my immediate window but took it that we formatted US format

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

Similar Threads

  1. Replies: 5
    Last Post: 03-30-2020, 05:31 PM
  2. Replies: 4
    Last Post: 06-22-2017, 03:49 PM
  3. Updating Old Records Based on New Records
    By lzook88 in forum Programming
    Replies: 24
    Last Post: 09-18-2015, 09:17 AM
  4. Multiple records assigned to one Employee
    By Bosakie in forum Forms
    Replies: 3
    Last Post: 10-08-2014, 09:29 AM
  5. Filter by Records Assigned To User
    By skankingpigeon in forum Access
    Replies: 2
    Last Post: 07-23-2012, 03:35 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