Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371

    rs3 wont go to cell 18 on Excel from Access

    Hi guy's bit of a long code but all works apart from rs3, this one is bugging me because I can't see anything wrong ?????

    I have ## out personal details but does all go in correctly apart from rs3 ???

    RemQty = DMax("RemNo", "tblRemovals", "[Engineer] = '" & mDriver & "' And [RemovalDate] = #" & RemDate & "#")
    MyStatus = "Run Sheet"



    Code:
    pOpen = "T:\XL Files\RUN SHEETS\"fOpen = "RUN SHEET TEMPLATE" & ".xlsx"
    pSave = "T:\XL FILES\EXISTING RUN SHEETS\"
    fSave = Me.cboVehicle & " " & Format(Me.cboRemovalDate, "dd-mm-yy") & ".xlsx"
    Set apXL = CreateObject("Excel.Application")
    Set xLWB = apXL.Workbooks.Open(pOpen & fOpen)
    apXL.ActiveWorkbook.SaveAs pSave & fSave
    apXL.Workbooks.Open pSave & fSave, True, False
    apXL.Visible = True
    Select Case RemQty
    Case 1
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Case 2
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Case 3
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Set rs3 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 3")
    Case 4
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Set rs3 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 3")
    Set rs4 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 4")
    Case 5
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Set rs3 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 3")
    Set rs4 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 4")
    Set rs5 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 5")
    Case 6
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Set rs3 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 3")
    Set rs4 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 4")
    Set rs5 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 5")
    Set rs6 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 6")
    Case 7
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Set rs3 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 3")
    Set rs4 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 4")
    Set rs5 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 5")
    Set rs6 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 6")
    Set rs7 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 7")
    Case 8
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Set rs3 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 3")
    Set rs4 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 4")
    Set rs5 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 5")
    Set rs6 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 6")
    Set rs7 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 7")
    Set rs8 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 8")
    Case 9
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Set rs3 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 3")
    Set rs4 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 4")
    Set rs5 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 5")
    Set rs6 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 6")
    Set rs7 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 7")
    Set rs8 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 8")
    Set rs9 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 9")
    Case 10
    Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 1")
    Set rs2 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 2")
    Set rs3 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 3")
    Set rs4 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 4")
    Set rs5 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 5")
    Set rs6 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 6")
    Set rs7 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 7")
    Set rs8 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 8")
    Set rs9 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 9")
    Set rs10 = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = 10")
    End Select
    
    
    With xLWB
    Select Case RemQty
    Case 1
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    Case 2
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    Case 3
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    .Worksheets(1).Cells(18, 1).CopyFromRecordset rs3
    Case 4
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    .Worksheets(1).Cells(18, 1).CopyFromRecordset rs3
    .Worksheets(1).Cells(20, 1).CopyFromRecordset rs4
    Case 5
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    .Worksheets(1).Cells(18, 1).CopyFromRecordset rs3
    .Worksheets(1).Cells(20, 1).CopyFromRecordset rs4
    .Worksheets(1).Cells(22, 1).CopyFromRecordset rs5
    Case 6
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    .Worksheets(1).Cells(18, 1).CopyFromRecordset rs3
    .Worksheets(1).Cells(20, 1).CopyFromRecordset rs4
    .Worksheets(1).Cells(22, 1).CopyFromRecordset rs5
    .Worksheets(1).Cells(24, 1).CopyFromRecordset rs6
    Case 7
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    .Worksheets(1).Cells(18, 1).CopyFromRecordset rs3
    .Worksheets(1).Cells(20, 1).CopyFromRecordset rs4
    .Worksheets(1).Cells(22, 1).CopyFromRecordset rs5
    .Worksheets(1).Cells(24, 1).CopyFromRecordset rs6
    .Worksheets(1).Cells(26, 1).CopyFromRecordset rs7
    Case 8
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    .Worksheets(1).Cells(18, 1).CopyFromRecordset rs3
    .Worksheets(1).Cells(20, 1).CopyFromRecordset rs4
    .Worksheets(1).Cells(22, 1).CopyFromRecordset rs5
    .Worksheets(1).Cells(24, 1).CopyFromRecordset rs6
    .Worksheets(1).Cells(26, 1).CopyFromRecordset rs7
    .Worksheets(1).Cells(28, 1).CopyFromRecordset rs8
    Case 9
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    .Worksheets(1).Cells(18, 1).CopyFromRecordset rs3
    .Worksheets(1).Cells(20, 1).CopyFromRecordset rs4
    .Worksheets(1).Cells(22, 1).CopyFromRecordset rs5
    .Worksheets(1).Cells(24, 1).CopyFromRecordset rs6
    .Worksheets(1).Cells(26, 1).CopyFromRecordset rs7
    .Worksheets(1).Cells(28, 1).CopyFromRecordset rs8
    .Worksheets(1).Cells(30, 1).CopyFromRecordset rs9
    Case 10
    .Worksheets(1).Cells(14, 1).CopyFromRecordset rs
    .Worksheets(1).Cells(16, 1).CopyFromRecordset rs2
    .Worksheets(1).Cells(18, 1).CopyFromRecordset rs3
    .Worksheets(1).Cells(20, 1).CopyFromRecordset rs4
    .Worksheets(1).Cells(22, 1).CopyFromRecordset rs5
    .Worksheets(1).Cells(24, 1).CopyFromRecordset rs6
    .Worksheets(1).Cells(26, 1).CopyFromRecordset rs7
    .Worksheets(1).Cells(28, 1).CopyFromRecordset rs8
    .Worksheets(1).Cells(30, 1).CopyFromRecordset rs9
    .Worksheets(1).Cells(32, 1).CopyFromRecordset rs10
    End Select
    .Worksheets(1).Cells(2, 9) = Me.cboRemovalDate
    .Worksheets(1).Cells(4, 9) = Me.cboDriver
    .Worksheets(1).Cells(6, 9) = Me.cboVehicle
    .Worksheets(1).Cells(33, 2) = Ref
    .Worksheets(1).Cells.EntireColumn.AutoFit
    .Worksheets(1).Cells.EntireColumn.HorizontalAlignment = xlLeft
    End With
    xLWB.Close
    apXL.Quit
    Set apXL = Nothing

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    would help if you identified which rs3 you are referring to and what what 'bugging me' means - won't compile? get an error? wrong result? something else?

  3. #3
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    Rather than read all of that I'll suggest, why not debug.print the rs3 sql, copy/paste into new query and test? Maybe there is nothing to "go out"?
    Also, by not declaring CurrentDb as an object variable, Access will create and destroy this object every time you invoke it. Very inefficient.
    And... it looks like the sql for any given rs (2 or 3 or 4...) is exactly the same for any case. Why not write it once as a variable and reference that?? Less code, less chance for error between multiple rs2's, much easier to read.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thanks guys, how can i reduce the code to give less chance of error, I will try what micron suggested and debug or get the vba from a query, yes ideally i would be better shortening the code, unsure how to though because it is looking at the RemQty ie: if the RemQty field has got 6 then case 6 should apply but don't i need rs2 for example in all cases ?

  5. #5
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hi Guys, just added a query to test and the 3rd job is not listed when i run query, i will triple check data input but sure there is a remno 3!!

  6. #6
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Urggggg Wrong date on remno 3!! it's the can't see the woods for the trees scenario, would still love some help on shortening the code though if you very kind people would help ??

    Kindest

  7. #7
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by DMT Dave View Post
    Urggggg Wrong date on remno 3!! it's the can't see the woods for the trees scenario, would still love some help on shortening the code though if you very kind people would help ??

    Kindest
    For example, Something like that?
    Code:
        For i = 1 To RemQty
            'Open recordset with the current RemQty.
            Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo =" & i, dbOpenSnapshot)
            'Export data to worksheet.
            xLWB.Worksheets(1).Cells(14, 1).Offset((i- 1) * 2, 0).CopyFromRecordset rs
            rs.Close
        Next i
    Last edited by accesstos; 07-26-2019 at 11:38 AM. Reason: code correction

  8. #8
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    accessto, pretty much what I had in mind (very nicely done) but for the reason noted I still recommend

    Dim db As DAO.Database

    Set db = CurrentDb

    Set rs = db.OpenRecordset...

    EDIT - May have spoken too soon. I had this question in mind but dismissed it. Shouldn't it be
    .Offset((i- 1) * 2, 0), otherwise the offset never changes. If RemQty is 6, it's always (6-1)*2 on every loop.

  9. #9
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Dear Micron,
    thank you very much for your comments and suggestions! You are right! Of course, it must be (i-1) and I fixed it. I wrote it without trying it but I just want to show the capabilities. Even so, that shows the much easier debuging. ;-)
    For "CurrentDB...", I agree with your comment, it is a good practice, at least for this case, but I leave it as it was for the sake of demo. But sometimes, especialy when we make updates in the data or the structure, we need a fresh instance of the database every time we refers to it, so we can not avoid allways the "CurrentDB ..." expression.

    Regards,
    John

  10. #10
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    You might find a comparison between useful

    dbengine v currentdb
    currentdb v db=currentdb


    https://sourcedaddy.com/ms-access/th...-function.html explains why 'To use CurrentDb effectively, it is always wiser to assign the reference to an object variable.'
    http://www.mendipdatasystems.co.uk/s...s-2/4594428908 by our very own Isladogs which looks at performance between the different methods

    Please draw your own conclusions

  11. #11
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Thank all of you brilliant folks for doing this, i have tried the shortening version and works perfect apart from, there is 5 RemQty on a particular output to the Excel file and it is only putting number 3 on the Excel Sheet, where or how do i get it to .moveFirst and loop through RemNo 1 2,3,4 and 5 to output ???

    I love the code accesstos added and like mentioned, it works a treat and so much quicker but it is only adding number RemQty 3 and not 1,2,3,4,5

    result of accesstos code

    Click image for larger version. 

Name:	Capture.JPG 
Views:	13 
Size:	67.4 KB 
ID:	39216

  12. #12
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hi Guys, it may be better if I give you the full code, note accesstos code added in but it's only outputting RemNo 3 when there should be 5 for that date and notice the snippet of excel result, row 14 is not added (RemNo1), neither row 16 (RemNo2) but RemNo3 is on 18 ???? I know Using For I = 1 then using Next I after code should start and finish the recordset, how can I achieve adding from RemNo 1 to RemNo 5????

    Code:
    Dim pOpen As String, fOpen As String, pSave As String, fSave As String, mDriver As String
    Dim rs As DAO.Recordset
    Dim apXL As Object, xLWB As Object
    Dim xlApp As Excel.Application
    Dim RemDate As Date
    Dim MyStr As String, MyLeft As String, MyRight As String, Inits As String, Ref As String, MyStatus As String
    Dim RemQty As Long, RemNo As Long, i As Integer
    
    
    If Me.cboOptions <> "Run Sheet" Then
    MsgBox ("You Can't Use These Options Unless Run Sheet Is Selected In Options!"), vbInformation + vbInformation, "RUN SHEET NOT SELECTED"
    DoCmd.CancelEvent
    Else
    RemDate = Me.cboRemovalDate
    mDriver = Me.cboDriver
    MyStatus = "Run Sheet"
    MyStr = InStr(mDriver, " ")
    MyLeft = Trim(Left(mDriver, MyStr))
    MyRight = Trim(Right(mDriver, MyStr))
    Set rs3 = CurrentDb.OpenRecordset("Select * From tblRunSheetNo")
    With rs3
    .AddNew
    !RunNo = Ref
    .Update
    .Close
    End With
    Inits = Left(MyLeft, 1) & Left(MyRight, 1)
    RemQty = DMax("RemNo", "tblRemovals", "[Engineer] = '" & mDriver & "' And [RemovalDate] = #" & RemDate & "#")
    RemNo = DMax("RecordNo", "tblRunSheetNo")
    Ref = Inits & RemQty & Format(Me.cboRemovalDate, "ddmmyy")
    pOpen = "C:\XL Files\RUN SHEETS\"
    fOpen = "RUN SHEET TEMPLATE" & ".xlsx"
    pSave = "C:\XL Files\EXISTING RUN SHEETS\"
    fSave = Me.cboVehicle & " " & Format(Me.cboRemovalDate, "dd-mm-yy") & ".xlsx"
    Set apXL = CreateObject("Excel.Application")
    Set xLWB = apXL.Workbooks.Open(pOpen & fOpen)
    apXL.ActiveWorkbook.SaveAs pSave & fSave
    apXL.Workbooks.Open pSave & fSave, True, False
    apXL.Visible = True
    With xLWB
    For i = 1 To RemQty
            'Open recordset with the current RemQty.
            Set rs = CurrentDb.OpenRecordset("SELECT tblRemovals.RemNo, tblRemovals.Client, tblRemovals.Add1, tblRemovals.Add2, tblRemovals.Town, tblRemovals.Postcode, " _
                                    & "tblRemovals.Tel, tblRemovals.Mobile, tblRemovals.LiftMake, tblRemovals.LiftModel FROM tblRemovals " _
                                    & "WHERE Engineer = '" & mDriver & "' And RemovalDate = #" & RemDate & "# And Status = '" & MyStatus & "' And RemNo = " & i, dbOpenSnapshot)
            'Export data to worksheet.
            xLWB.Worksheets(1).Cells(14, 1).Offset((i - 1) * 2, 0).CopyFromRecordset rs
            rs.Close
        Next i
    .Worksheets(1).Cells(2, 9) = Me.cboRemovalDate
    .Worksheets(1).Cells(4, 9) = Me.cboDriver
    .Worksheets(1).Cells(6, 9) = Me.cboVehicle
    .Worksheets(1).Cells(33, 2) = Ref
    .Worksheets(1).Cells.EntireColumn.AutoFit
    .Worksheets(1).Cells.EntireColumn.HorizontalAlignment = xlLeft
    End With
    xLWB.Close
    apXL.Quit
    Set apXL = Nothing
    If MsgBox("Removals Update File Created Successfully" & Chr(10) & Chr(10) & _
    "Do You Want To Open It To Check Data ?", vbQuestion + vbYesNo, "FILE CREATED") = vbNo Then
    DoCmd.CancelEvent
    Else
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    xlApp.Workbooks.Open pSave & fSave, True, False
    xlApp.WindowState = xlMaximized
    DoCmd.RunCommand acCmdAppMinimize
    End If
    End If
    
    Click image for larger version. 

Name:	Capture.JPG 
Views:	14 
Size:	67.4 KB 
ID:	39217

  13. #13
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,941
    the code should do what you want - the implication is there is not data for the other remQty's

  14. #14
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    Revisit your prior posts. I suggested how to validate, to which you replied that remno had the wrong date (but remno is declared as a Long?).
    Validate as before but on each pass until the issue is discovered.
    You do not require variable declaration, do you? rs is not declared?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    thanks guy's, do I need to set RemNo as Integer ?, I think I don't fully understand these better methods, I always try the hard way!!! is it possible to add adjust and highlighted adjusted ???

    The RemNo is a number and RenDate is where I had the wrong date in so it would output...

    Many Thanks to you all

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

Similar Threads

  1. Import Excel data cell by cell into an Access table.
    By russmann2000 in forum Import/Export Data
    Replies: 3
    Last Post: 03-21-2018, 07:18 PM
  2. Replies: 4
    Last Post: 06-18-2013, 08:50 PM
  3. import specific cell from excel to access
    By maneuk in forum Import/Export Data
    Replies: 10
    Last Post: 07-01-2011, 06:24 AM
  4. importing a single cell from excel onto access
    By virus100 in forum Import/Export Data
    Replies: 0
    Last Post: 03-20-2010, 11:57 AM
  5. Can we post Access cell data to Excel cell properties?
    By Zethro in forum Import/Export Data
    Replies: 1
    Last Post: 12-13-2005, 08:42 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