Page 3 of 3 FirstFirst 123
Results 31 to 36 of 36
  1. #31
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hi again guy's, can i add a Count within a recordset ?

    So I can add total quantity on the next excel to the right of record set ?

    So the Sheet comes out something like : 1 Joe Bloggs Wolverhampton WV6 3GH 6 Items


    Somewhere in the code like And Vehicle ='" & VehReg & "' And (Count(DelTo Where DelNo = " & I,dbOpenSnapshot) ?????




    Code:
    Set rs2 = db.OpenRecordset("SELECT DISTINCT tblAssign.DelNo, tblAssign.DelTo, tblAssign.Town, tblAssign.PostCode FROM tblAssign " _
                                    & "WHERE Driver = '" & mDriver & "' And DeliveryDate = #" & mDelDate & "# And Vehicle = '" & VehReg & "' And DelNo = " & i, dbOpenSnapshot)
    

  2. #32
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Dave, try this:

    Create a query in your database with this definition:
    Code:
    SELECT DelTo, Town, PostCode,  Vehicle, Driver, DeliveryDate, Count(DelNo) AS DelCount 
    FROM tblAssign 
    GROUP BY DelTo, Town, PostCode, Vehicle, Driver, DeliveryDate;
    Check the results and save it.
    Then use its name (say qryAssignGroup) in your code as below:
    Code:
    Set rs = db.OpenRecordset("SELECT * FROM qryAssignGroup " _
                               & "WHERE Driver = '" & mDriver _
                               & "' And DeliveryDate = #" & mDelDate _
                               & "# And Vehicle = '" & VehReg _
                               & "' And DelCount <= " & mDelCount, dbOpenSnapshot)
    Export the data to Excel with "one step" method.
    Last edited by accesstos; 07-30-2019 at 01:33 AM. Reason: Code editing

  3. #33
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    Hi accesstos, I am shocked, everything you have sent works, this one I am getting syntax error FROM clause run time error 3131 ?

    Results are correct when I run the qryAssignGroup, the debug highlights the rs2 (only change from your code) as below

    Code:
    With xLWB
    For i = 1 To mDelCount
            'Open recordset with the current RemQty.
            Set rs = db.OpenRecordset("SELECT DISTINCT tblAssign.DelNo, tblAssign.DelTo, tblAssign.Town, tblAssign.PostCode FROM tblAssign " _
                                    & "WHERE Driver = '" & mDriver & "' And DeliveryDate = #" & mDelDate & "# And Vehicle = '" & VehReg & "' And DelNo = " & i, dbOpenSnapshot)
            Set rs2 = db.OpenRecordset("SELECT * FROM qryAssignGroup" _
                               & "WHERE Driver = '" & mDriver _
                               & "' And DeliveryDate = #" & mDelDate _
                               & "# And Vehicle = '" & VehReg _
                               & "' And DelCount <= " & mDelCount, dbOpenSnapshot)
            'Export data to worksheet.
            xLWB.Worksheets(1).Cells(2, 3).Offset((i - 1) * 2, 0).CopyFromRecordset rs
            xLWB.Worksheets(1).Cells(2, 7).Offset((i - 1) * 2, 0).CopyFromRecordset rs2
            rs.Close
        Next i
    .Worksheets(1).Cells(2, 2) = Me.cboVehicleIndex5
    .Worksheets(1).Cells(3, 2) = Me.cboDriverIndex5
    .Worksheets(1).Cells(5, 2) = Format(Me.cboDelDateIndex5, "ddd-dd-mmm-yyyy")
    .Worksheets(1).Cells.EntireColumn.AutoFit
    .Worksheets(1).Cells.EntireColumn.HorizontalAlignment = xlLeft
    End With

  4. #34
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,371
    I also have EntryCode and ExitCode text fields in tblDealers, is it easy to set up another recordset (rs3) to look these up based on the postcode from rs ?

    Many Thanks

  5. #35
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    I'm sorry Dave! It was my fault. You must enter a whitespace afterwards
    qryAssignGroup in SQL def (see the corrected code in my post).

    But, you have to export the rs2 once! Out of the For...Next loop.
    Code:
    xLWB.Worksheets(1).Cells(2, 7).CopyFromRecordset rs2
    If you need to have blank rows between entries in the worksheet, you should use a new loop for rs2 only.
    Could you attach the template workbook?

  6. #36
    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
    I also have EntryCode and ExitCode text fields in tblDealers, is it easy to set up another recordset (rs3) to look these up based on the postcode from rs ?

    Many Thanks
    Quote Originally Posted by Ajax View Post
    postcode is presumably text so should be

    "WHERE PostCode = '" & rs.Fields("PostCode") & i & "'"

    Otherwise what is the error description you get?
    So try this:

    Code:
    Set rs3 = db.OpenRecordset("SELECT DISTINCT tblDealers.PostCode, tblDealers.EntryCode, tblDealers.ExitCode FROM tblDealers " _ 
                                                                                             & "WHERE PostCode = '" & rs.Fields("PostCode") & "'", dbOpenSnapshot)

Page 3 of 3 FirstFirst 123
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