Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332

    Passthrough queries and listboxes

    Alright here's the setup.

    I have a class called GTSDAO which has a method on it called "OpenRecordset".

    Code:
    Public Function OpenRecordset(SQL As String, Optional dbType As RecordsetTypeEnum = dbOpenSnapshot, Optional dbOptions As RecordsetOptionEnum = dbSQLPassThrough) As Recordset
        Set OpenRecordset = db.OpenRecordset(SQL, dbType, dbOptions)
    End Function
    The db object in the class is a DAO database created with an ODBC connection string set elsewhere. This way I can run all of my SQL requests directly to the SQL Server and bypass the JET engine entirely. No linked tables or local queries at all. Everything is VBA. I'm doing this for various reasons that I won't get into here.

    Now I am trying to set the recordset for one of my listboxes to a recordset based on this "OpenRecordset" method.



    Code:
    SQL = "SELECT Customers.ID, CustomerName AS [Customer Name], Contacts.FullName AS [Contact Name], SQPhoneNumbers.PhoneNumber AS Phone," _
            & " StatusName AS Status, iif(InActive = 0, 'Yes', 'No') AS Active" _
            & " FROM ((((Customers LEFT JOIN CustomerDetails ON Customers.ID = CustomerDetails.CustomerID)" _
            & " LEFT JOIN Contacts ON CustomerDetails.PrimaryContactID = Contacts.ID)" _
            & " LEFT JOIN Customers_PhoneNumbers ON CustomerDetails.CustomerID = Customers_PhoneNumbers.CustomerID)" _
            & " LEFT JOIN (SELECT ID, PhoneNumber" _
            & " FROM PhoneNumbers WHERE [Primary]=1) AS SQPhoneNumbers" _
            & " ON Customers_PhoneNumbers.PhoneNumberID = SQPhoneNumbers.ID)" _
            & " LEFT JOIN CustomerStatuses ON CustomerDetails.CustomerStatusID = CustomerStatuses.ID" _
            & " WHERE CustomerName LIKE '" & letter & "%' AND CustomerName LIKE '%" & Me.txtCriteria & "%'" & IIf(chkShowInActive, "", " AND InActive=0")
        Set Me.lstCustomers.Recordset = db.OpenRecordset(SQL)
    The query works fine when I run it in SSMS, I get 2 results back. However my listbox is blank. I have even checked the recordset of the listbox and it says there are 2 records in the listbox recordset. How can there be 2 records in my recordset, but the listbox shows nothing?



    BTW, I did try switching over form dbSQLPassThrough to dbReadOnly and Access chokes on my subquery (SELECT ID, PhoneNumber FROM PhoneNumbers WHERE [Primary]=1) AS SQPhoneNumbers.

    Click image for larger version. 

Name:	EmptyListbox.jpg 
Views:	16 
Size:	21.2 KB 
ID:	15933

    That is my listbox upon opening. It clearly thinks there are headers because it gives all of the widths for each header... but no data. Not even header data.

  2. #2
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    FWIW, here is an MSN KB on using PassThrough for stored procedures which return results to a recordset. It's the method I'm using, just not as a stored procedure.

    http://support.microsoft.com/kb/184749

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,495
    First thought that comes to mind is that with DAO, you want * rather than % as a wildcard. It certainly clouds things that you're using a pass through, but I'd try switching that first since it's easy.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    Thanks pbaldy, but the wildcards are not the problem.
    Code:
        Set rs = Me.lstCustomers.Recordset
        rs.MoveLast
        Debug.Print rs.RecordCount
    This code in fact returns a RecordCount of 2.... which means the recordcount of what is in the listbox is correct, it's just not displaying the data.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,495
    I've never tried to set a listbox recordset like this:

    Set Me.lstCustomers.Recordset = db.OpenRecordset(SQL)

    I would think the listbox headers are there because the column widths are set in design view. What are the row source and row source types set to? I'll play with a test db.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,495
    Well, in a brief test I got it to work, so this falls in the "learn something new every day" category. I wonder if this is the problem:

    Set Me.lstCustomers.Recordset = db.OpenRecordset(SQL)

    Noting that you used the same name for your function as the method, which is never a good idea IMO.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    You'll need a SQL Server backend with a table. Then set the listbox's recordset to a recordset opened with the dbSQLPassThrough option. You get records in your recordset, but they won't display in your listbox.

  8. #8
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    I changed the database variable name to dbs... same thing. For your testing you'll want to use db.OpenRecordset(SQL, dbOpenSnapshot, dbSQLPassThrough).

    In my class I have defaults set for the Type and Options parameters so in my example code you don't see them, but when they get run dbOpenSnapshot and dbSQLPassThrough are used. Hope that makes sense.

  9. #9
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    Quote Originally Posted by pbaldy View Post
    I've never tried to set a listbox recordset like this:

    Set Me.lstCustomers.Recordset = db.OpenRecordset(SQL)

    I would think the listbox headers are there because the column widths are set in design view. What are the row source and row source types set to? I'll play with a test db.
    Row Source Type = Table/Query
    Row Source is left blank since I'm setting the recordset of the listbox and not it's rowsource.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,495
    Those settings had no effect using a local table. I certainly have access to SQL Server, but to fully replicate your environment I'd need to recreate your class, db object, etc, which I don't have time to do right now.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,707
    Steve,

    Just a guess since I don't know Sql Server, do you have to Requery the listbox. If the recordcount is correct, i seems a timing or refresh issue....

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,495
    I wondered about requerying the listbox too, but in my test it filled instantly and this:

    Set Me.List20.Recordset = db.OpenRecordset(strSQL, dbOpenSnapshot, dbSQLPassThrough)
    Debug.Print Me.List20.ListCount

    returned the correct count. Still, can't hurt to try!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    Paul, are you setting db to currentdb? Did results show up in your listbox?

  14. #14
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    Quote Originally Posted by orange View Post
    Steve,

    Just a guess since I don't know Sql Server, do you have to Requery the listbox. If the recordcount is correct, i seems a timing or refresh issue....
    I tried to do it Async but apparently that's not supported.

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,495
    Quote Originally Posted by Xipooo View Post
    Paul, are you setting db to currentdb? Did results show up in your listbox?
    Yes and yes. The full test code (in a junk test db) was:

    Code:
      Dim strSQL                  As String
      Dim db                      As DAO.Database
      Dim rs                      As DAO.Recordset
    
      Set db = CurrentDb()
    
      strSQL = "SELECT * FROM tblData"
    
      Set Me.List20.Recordset = db.OpenRecordset(strSQL, dbOpenSnapshot, dbSQLPassThrough)
      Debug.Print Me.List20.ListCount
      Set rs = Nothing
      Set db = Nothing
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 8
    Last Post: 02-26-2013, 06:44 PM
  2. Queries and Listboxes
    By h_rainier in forum Queries
    Replies: 5
    Last Post: 12-10-2012, 04:06 PM
  3. Passthrough over a timestamp?
    By KrisDdb in forum Access
    Replies: 1
    Last Post: 01-10-2012, 06:42 PM
  4. Cannot Make Table with Passthrough Query
    By chasemhi in forum Import/Export Data
    Replies: 0
    Last Post: 12-05-2011, 01:30 PM
  5. dropdowns and listboxes
    By t_dot in forum Forms
    Replies: 6
    Last Post: 08-19-2010, 11:12 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