Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Use an array to filter a form

    I have a form with a fairly large set of tables which lock up my records to read only because I have a few many to many junctions involved.

    However I would like to open the form, display the records then loop through them all and stuff each ID into an array.

    Then I would like to change the record source of the form and give it a less expanded recordset (less the junction tables) and have it be filtered by the array.

    so the array would take 223, 224, 224

    then filter the form with records that match.



    Any advice would be appreciated greatly

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I personally wouldn't use an array. I'd probably build a string that looked exactly like what you posted, then when building the new SQL use that string in an In() clause. Basically like this, though it's using a multiselect listbox.

    http://www.baldyweb.com/multiselect.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by pbaldy View Post
    I personally wouldn't use an array. I'd probably build a string that looked exactly like what you posted, then when building the new SQL use that string in an In() clause. Basically like this, though it's using a multiselect listbox.

    http://www.baldyweb.com/multiselect.htm
    I ended up building the array and a string using it

    Code:
    Dim i As Long
    Dim rs As DAO.Recordset
    Dim myArray() As Long
    Dim txt As String
    Set rs = Me.RecordsetClone
    
    
    ReDim myArray(0 To (rs.RecordCount - 1))
    
    
    rs.MoveFirst
    For i = 0 To (rs.RecordCount - 1)
    myArray(i) = rs!BookingsID
    txt = txt & myArray(i) & ","
    rs.MoveNext
    Next i
    
    
    txt = Left(txt, Len(txt) - 1)
    
    
    MsgBox txt
    I want to be able to manipulate the array.

    I still need to figure out how to filter the form using the string or array.

    since I have the array now I can build it as anything - which I want to now put as a where

    so the filter can get every ID and filter the form to only show those records with the same ID

    yet to figure out.

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Now makes the where string

    Code:
    Dim i As Long
    Dim txt As String, strWhere As String
    Dim rs As DAO.Recordset
    Dim myArray() As Long
    Set rs = Me.RecordsetClone
    
    
    ReDim myArray(0 To (rs.RecordCount - 1))
    
    
    rs.MoveFirst
    For i = 0 To (rs.RecordCount - 1)
    myArray(i) = rs!BookingsID
    txt = txt & myArray(i) & ","
    strWhere = strWhere & "([BookingsID] = " & myArray(i) & ") or "
    rs.MoveNext
    Next i
    
    
    txt = Left(txt, Len(txt) - 1)
    
    
    
    
    
    
    MsgBox txt
    MsgBox strWhere

  5. #5
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    And the working code

    Works like it should - would appreciate a look through

    Code:
    Private Sub Form_Load()
    
    
    Dim i As Long
    Dim strWhere As String
    Dim rs As DAO.Recordset
    Dim myArray() As Long
    Set rs = Me.RecordsetClone
        
        'closes form if record count 0
        If rs.RecordCount <= 0 Then
        MsgBox "There are no bookings listed"
        rs.Close
        Set rs = Nothing
    
    
        Exit Sub
        End If
        
    'gives the array a final number (minus 1 because array starts at 0)
    ReDim myArray(0 To (rs.RecordCount - 1))
    
    
    'move to first record, record it and stuff it into array and builds a string then loops through all
    rs.MoveFirst
    For i = 0 To (rs.RecordCount - 1)
    myArray(i) = rs!BookingsID
    strWhere = strWhere & "([BookingsID] = " & myArray(i) & ") or "
    rs.MoveNext
    Next i
    
    
    strWhere = Left(strWhere, Len(strWhere) - 4) 'remove the trailing or
    
    
    Me.RecordSource = "qryAdminStep2_KEEP" ' change recordset to the unlocked recordset
    
    
    'filter form
    Me.Filter = strWhere
    Me.FilterOn = True
    
    
    End Sub

  6. #6
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    The problem I have now is that the string is too long - I can remove [BookingsID] from it and make it shorter however I would like a better solution

  7. #7
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I'm thinking to write to a text file, then in the sql of the query import the text file as the where statement... if possible.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You might try a Variant in place of the string but I don't know what you mean by too long.

    replace strWhere with varWhere

    Instead of a text file write it to a temp table. Then you could do an inner join to the query that your form is based on and change the form's recordset to that, maybe. And for that matter, you already have the data set so maybe there is an alternative to the Array.

    It has been a long day so I may not be thinking clearly.

  9. #9
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ItsMe View Post
    You might try a Variant in place of the string but I don't know what you mean by too long.

    replace strWhere with varWhere

    Instead of a text file write it to a temp table. Then you could do an inner join to the query that your form is based on and change the form's recordset to that, maybe. And for that matter, you already have the data set so maybe there is an alternative to the Array.

    It has been a long day so I may not be thinking clearly.
    Not sure you can have a me.filter = varWhere because it isn't a string?

    I have made a text file that puts in each where line "([BookingsID] = " & myArray(i) & ") or " but bringing that into the query is unknown to me

  10. #10
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I have the text file contain
    Code:
    "([BookingsID] = 25780) or "
    "([BookingsID] = 25548) or "
    "([BookingsID] = 25455) or "
    now in the sql on the where I hope to import the directory and file

    WHERE = 'directory/file'

    or something like WHERE (('c:\QueryImport\WhereStatement.txt') AND ((tblShows.TheatreShow)=False))

    update: I have removed the final or in the file

    should read
    Code:
     
    ([BookingsID] = 25717) or 
    ([BookingsID] = 25398) or 
    ([BookingsID] = 25832) or 
    ([BookingsID] = 25834) or 
    ([BookingsID] = 25782) or 
    ([BookingsID] = 24950)

  11. #11
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Yah, I was not sure what you would do with the Variant once you had it. Just thinking of a way to make room.

    You can edit a query object using DAO and query defs. Not the most fun thing in the world. That is why I was thinking to append the booking ID to a temp table. What do you want with the Booking ID any way, open another form? You could place a subform on the existing form and link the Master Child on the Record ID. Then just hide controls on the main form and cause the subform to be visible. Might take a performance hit though.

    EDIT : Looking at post #10 it seems the problem is you are storing it as an Array. That is why your string is so large.

  12. #12
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ItsMe View Post
    Yah, I was not sure what you would do with the Variant once you had it. Just thinking of a way to make room.

    You can edit a query object using DAO and query defs. Not the most fun thing in the world. That is why I was thinking to append the booking ID to a temp table. What do you want with the Booking ID any way, open another form? You could place a subform on the existing form and link the Master Child on the Record ID. Then just hide controls on the main form and cause the subform to be visible. Might take a performance hit though.

    EDIT : Looking at post #10 it seems the problem is you are storing it as an Array. That is why your string is so large.
    if I store it as a string I am still going to be over the 255 character limit due to there being so many records...?

  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Yah, I don't know if the limit you are experiencing is characters or mb. I would start by looking for an alternative to the Filter property. I still am not quite sure what you are doing or why. I build my Where Criteria and WHERE statements in a single line/string.

    Building a query from the form's dataset may be what you need. I do not know.

  14. #14
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ItsMe View Post
    Yah, I don't know if the limit you are experiencing is characters or mb. I would start by looking for an alternative to the Filter property. I still am not quite sure what you are doing or why. I build my Where Criteria and WHERE statements in a single line/string.

    Building a query from the form's dataset may be what you need. I do not know.
    Because I have one query that is locked due to having more than one many to many relationships - it locks the query so it can't be edited.

    I want to search by performer (which is the extra junction) so I do, then grab all those IDs of the related bookings then give the form an editable query (less that performer junction) that opens all the records to display those booking id that have been filterd by the grab.

    It's why I change the recordsource - to swap them out. But because I haven't got the performer table anymore I need to filter it by something. BookingIDs fits.

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    The 255 character limit is for each parameter in a parameterized query. It appears Filter property has the same limit. I wonder if the WHERE CONDITION of OpenForm/OpenReport has the same limit.

    Options I see:

    1. As ItsMe suggested, a 'temp' table to save the BookingIDs, join Bookings table to the temp table to retrieve only the records that match.

    2. Yes/No field in the Bookings table and code sets the value for each record
    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.

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

Similar Threads

  1. Filter By Form and Apply Filter Buttons
    By JustLearning in forum Forms
    Replies: 13
    Last Post: 02-18-2015, 01:01 PM
  2. Replies: 5
    Last Post: 02-07-2013, 12:21 PM
  3. Replies: 1
    Last Post: 08-16-2012, 01:51 PM
  4. Replies: 28
    Last Post: 03-08-2012, 06:47 PM
  5. Show array of bytes as picture in form
    By HZwaan in forum Forms
    Replies: 0
    Last Post: 06-17-2010, 04:07 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