Page 3 of 3 FirstFirst 123
Results 31 to 43 of 43
  1. #31
    Join Date
    Apr 2017
    Posts
    1,792
    Look next post instead!

  2. #32
    Join Date
    Apr 2017
    Posts
    1,792
    You can also use your existing code to fill the dummy table. It will be slower, but change to code will be minimal.

    Code:
    Private Sub CmdViewSelection_Click()
    
    Dim strQry As String
    Dim ctl As Control
    Dim varItem As Variant
    Dim dbs As DAO.Database
    
    On Error GoTo CmdViewSelection_Click_Err
    
    'make sure a selection has been made
    If Me.SearchResults_Multi.ItemsSelected.Count = 0 Then
      MsgBox "At Least 1 Record Must Be Selected!!", vbInformation, "Record Selection Required"
      Exit Sub
    End If
    'add selected values to string
    Set ctl = Me.SearchResults_Multi
     For Each varItem In ctl.ItemsSelected
      strQry = "INSERT INTO tblDummy (RoomID) VALUES (" & ctl.ItemData(varItem) & ")"
      dbs.Excecute strQry
     Next varItem
    
     DoCmd.OpenReport "rptPartBuildings", acViewReport, , "RoomID IN(SELECT RoomID FROM tblDummy)"
    ...

  3. #33
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    Do i have to add all the relevant field names to the dummy table? as i still cannot get it to work.

  4. #34
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    i put your code in to the click event of the "Select All" button and nothing was added to the table i created. I am at a totla loss. But thanks for your help anyway.

  5. #35
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    I tried the code and got method or data member not found and the following line was highlighted. So not sure where this is going wrong. Sorry to keep coming back too you on this.

    dbs.Excecute strQry

    Private Sub CmdViewSelection_Click()

    Dim strQry As String
    Dim ctl As Control
    Dim varItem As Variant
    Dim dbs As DAO.Database

    On Error GoTo CmdViewSelection_Click_Err

    'make sure a selection has been made
    If Me.SearchResults_Multi.ItemsSelected.Count = 0 Then
    MsgBox "At Least 1 Record Must Be Selected!!", vbInformation, "Record Selection Required"
    Exit Sub
    End If
    'add selected values to string
    Set ctl = Me.SearchResults_Multi
    For Each varItem In ctl.ItemsSelected
    strQry = "INSERT INTO tblDummy (RoomID) VALUES (" & ctl.ItemData(varItem) & ")"
    dbs.Excecute strQry
    Next varItem

    DoCmd.OpenReport "rptPartBuildings", acViewReport, , "RoomID IN(SELECT RoomID FROM tblDummy)"

  6. #36
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    Tried code again and got this error now.

    Run time error '91'
    object variable or with variable block not set.

  7. #37
    Join Date
    Apr 2017
    Posts
    1,792
    There was missing code row, as I did write the code on fly. And instead of Execute I used DoCmd. I needed to manipulate warnings, but so you don't need to activate DAO.
    And I added a possibility to use a single report button - when no filter is set, all rooms are selected for reporting.

    Code:
    Private Sub CmdViewSelection_Click()
    
    Dim strQry As String
    Dim ctl As Control
    Dim varItem As Variant
    
    On Error GoTo CmdViewSelection_Click_Err
    
    'Delete previous entries from tblDummy
    DoCmd.SetWarnings (False) ' warnings are set off - otherwise you may have to click OK for several hundred times
    strQry = "DELETE FROM tblDummy"
    DoCmd.RunSQL strQry
    
    'Fill tblDummy
    If Me.SearchResults_Multi.ItemsSelected.Count = 0 Then ' when nothing is selected, the report includes all rooms
        strQry = "INSERT INTO tblDummy SELECT RoomID FROM qryRoomInformation"
        'dbs.Execute strQuery
        DoCmd.RunSQL strQry
    Else ' the table tblDummy is filled with selection
        Set ctl = Me.SearchResults_Multi
        For Each varItem In ctl.ItemsSelected
            strQry = "INSERT INTO tblDummy (RoomID) VALUES (" & ctl.ItemData(varItem) & ")"
            'dbs.Execute strQuery
            DoCmd.RunSQL strQry
        Next varItem
    End If
    DoCmd.SetWarnings (True) ' warnings are set back on
    
    ' Open the report
    DoCmd.OpenReport "rptPartBuildings", acViewReport, , "RoomID IN(SELECT RoomID FROM tblDummy)"
    ... ' continue as in original event

  8. #38
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    Thanks. On way home from work will try later and let you know. Much appreciated.

  9. #39
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Instead of the SelectAll button that actually selects all records in the listbox, use a checkbox and code:
    Code:
    If Me.chkAll Then
    
        strWHERE = "[CampusNames] & [BuildingName] & [RoomNumber] & [RoomActivity] & [RoomType] & [WorkType] & [SquareFootage] Like '*" & Me.SearchFor_Multi & "*'"
    
        DoCmd.OpenReport "rptPartBuildings", acViewReport, , strWHERE
    
    Else
    
        ...
    
    End If
    If users are manually selecting group of records in the listbox and the resulting IN() string is more than 255 characters, then the temp table approach may be best, otherwise build a search form with multiple search controls. See example in http://allenbrowne.com/ser-62.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #40
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    Will try in the morning, thanks for all ideas,/suggestions, much appreciated.

  11. #41
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    Quote Originally Posted by ArviLaanemets View Post
    You can also use your existing code to fill the dummy table. It will be slower, but change to code will be minimal.

    Code:
    Private Sub CmdViewSelection_Click()
    
    Dim strQry As String
    Dim ctl As Control
    Dim varItem As Variant
    Dim dbs As DAO.Database
    
    On Error GoTo CmdViewSelection_Click_Err
    
    'make sure a selection has been made
    If Me.SearchResults_Multi.ItemsSelected.Count = 0 Then
      MsgBox "At Least 1 Record Must Be Selected!!", vbInformation, "Record Selection Required"
      Exit Sub
    End If
    'add selected values to string
    Set ctl = Me.SearchResults_Multi
     For Each varItem In ctl.ItemsSelected
      strQry = "INSERT INTO tblDummy (RoomID) VALUES (" & ctl.ItemData(varItem) & ")"
      dbs.Excecute strQry
     Next varItem
    
     DoCmd.OpenReport "rptPartBuildings", acViewReport, , "RoomID IN(SELECT RoomID FROM tblDummy)"
    ...
    good morning

    I have run your code this morning and it works a treat. As you stated it is a little slow and would be interested how to speed this function up, although not critical. Thanks again for your support as it has been invaluable and i have learnt a lot. This was a problem I came across this problem several months ago during the design of my database and just left it on the back burner, but thanks to your guidance you have solved a burning issue for me. Thanks.

  12. #42
    Join Date
    Apr 2017
    Posts
    1,792
    About speeding up:
    1. When filter string will be shorter, you can use your previous approach. But it is difficult to decide, at which point to change the approach. And when the number of rooms to filter is small, the real time gain is minimal;
    2. When there is no records selected to filter, and you use the code example from my last posting, you can run DoCmnd.OpenReport separately for "If Me.SearchResults_Multi.ItemsSelected.Count = 0" (without filter) and for "Else" (with filter);
    3. Instead of filling tblDummy stepwise, you can use VBA to construct a SQL string and fill the table executing a single query string ("INSERT INTO tblDummy SELECT RoomID FROM qryRoomInformation WHERE ..."). For sure it will be faster, especially when the number of filtered rows is bigger. At same time I got the feeling, that columns compared in WHERE clause will depend on filter text in SearchResults_Multi text box. And you have to take another filter (the dropdown one) into account too. So a lot of more code spent on SQL string construction to get this to work properly.

  13. #43
    hinchi1 is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2017
    Posts
    181
    Thanks for your response and to be honest i am really happy with what i have. My next stage is to learn Visual Studio and work with the data i have and maybe redesign the front end, but that is a long way off. Thanks again for your help. I have changed the code to all 5 of my report forms and it works perfectly.

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

Similar Threads

  1. filtered subform unshown records when choose another brand
    By georgesobhyy@gmail.com in forum Forms
    Replies: 6
    Last Post: 09-19-2016, 07:32 AM
  2. Dcount records from a filtered form
    By charly.csh in forum Access
    Replies: 6
    Last Post: 11-20-2014, 01:14 PM
  3. How to get count of filtered records
    By DBDave in forum Queries
    Replies: 2
    Last Post: 11-03-2014, 02:07 PM
  4. Exporting Pivot Table to Excell - only want filtered records
    By Jennifer227 in forum Import/Export Data
    Replies: 1
    Last Post: 04-01-2011, 03:10 PM
  5. Open form to filtered records
    By ducecoop in forum Access
    Replies: 3
    Last Post: 10-22-2010, 10:53 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