Results 1 to 7 of 7
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479

    Getting recordset in a Form


    Hi, I want my Form to use data in a Recordset. Can I pass a recordset to a Form ?
    Or - should I just send the sql in as OpenArgs? Only I've got the recordset already open and only want the Form if record count > 1

  2. #2
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    309
    There are plenty of ways to get a recordset into a form.

    You can’t pass a recordset directly through OpenArgs since it only accepts strings. However, you can send an SQL query string with OpenArgs and use it to open a recordset in the form. Alternatively, you can pass a JSON string with multiple records through OpenArgs. Your form could also access a recordset from another open form or use a public variable to store and access the recordset.
    Please click on the ⭐ below if this post helped you.


  3. #3
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    567
    Quote Originally Posted by Middlemarch View Post
    Hi, I want my Form to use data in a Recordset. Can I pass a recordset to a Form ?
    Or - should I just send the sql in as OpenArgs? Only I've got the recordset already open and only want the Form if record count > 1
    Cart before the horse.
    What are you trying to accomplish? If you want to open the form to show only a single record, set filters in the Open command. Recordsets are not required to do it.

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    In the form's Open event you set its recordset to the open recordset:
    Code:
    Me.Recordset=rsYourRecordset
    https://github.com/MicrosoftDocs/VBA...m.Recordset.md
    https://www.access-programmers.co.uk...dsource.56221/
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    The form has a listview control showing many records, not just one.
    But if there are no records to show no form is necessary. The recordset is establish to get a count... my question was how best to pass it to the Form. Didn't seem right to open it a second time but that's what i ended up doing.
    Edgar had the answer, a public variable, but by then I'd used sql/Open Args.

  6. #6
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    Gicu, here's the click event
    Code:
    Private Sub cmdShowHits_Click()
    sql = "Select Distinct Path, tPerformer, Media from Tracks Where tCat = " & ThisDisk() & " and Media = '45';"
        Set r = CurrentDb.OpenRecordset(sql)
        If r.RecordCount > 0 Then
    DoCmd.OpenForm "frmListview2", acNormal, , , , , sql
            Else
            MsgBox "Nothing Found"
        End If
    End Sub
    Where would you put your Open event?

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    You don't need to open a recordset to get the count:
    Code:
    if dCount("*","Tracks","tCat = '" & ThisDisk() & "' and Media = '45'")>0 Then 'Assumes tCat is a string
    What I showed you was supposed to go into the frmListview2 Open event, not in the calling form. You would need to use a public variable to be able to pass the recordset, but it might not be needed. I have seen your posts in the past about using listview controls instead of the built-in listboxes and I do not have experience with listviews, but wouldn't the recordset you're asking about need to be bound to the ListView control and not the actual form?
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. form as recordset
    By krag in forum Programming
    Replies: 5
    Last Post: 02-19-2018, 02:00 AM
  2. Replies: 11
    Last Post: 11-28-2015, 09:58 PM
  3. Replies: 4
    Last Post: 05-20-2014, 12:45 PM
  4. Replies: 6
    Last Post: 12-03-2013, 11:14 PM
  5. Replies: 2
    Last Post: 03-08-2012, 12:59 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