Results 1 to 6 of 6
  1. #1
    RunTime91 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2014
    Posts
    284

    Populate ComboBox using PassThrough Query

    I can do this all day long using Linked Tables - But I want to populate a combobox by pulling the data directly from SSMS.

    Below is one of about 20 different configurations - with a few different variations
    Code:
    Private Sub CmboOpnCls_AfterUpdate()
    
    Dim StrSQL As String
    Dim conn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim Rs As ADODB.Recordset
    
      Set conn = New ADODB.Connection
      conn.ConnectionString = strConnection
      conn.Open
    
    If Me.CmboOpnCls.Value = "Open" Then
    
    
     StrSQL = "SELECT ATCCallDescription, ATCCategory " & _
               "FROM Tri.ATCCallResults " & _
               "WHERE ATCCategory = '" & Me!CmboOpnCls & "'"
    
    
    End If
    
    Debug.Print StrSQL
    
    Set Rs = conn.Execute(StrSQL)
    
    Me.CmboClsdRsn = Rs.Fields("ATCCallDescription")
    'Me.CmboClsdRsn.RowSource = Rs.Fields("ATCCallDescription")
    'Me.CmboClsdRsn.RowSource = StrSQL
    'Me.CmboClsdRsn.RowSourceType = StrSQL
     
    End If
    
    End Sub
    


    Shown within the code above are just a few examples of the Me.CmboClsdRsn... code attempts
    The top one currently being used pulls only the first record
    I've also tried to tie the Rs and the RowSource directly to the SELECT Statement
    My various attempts have pulled from nothing, to just the first record of the Rs, to an assortment of errors
    According to Google - I have to do this in a loop??? Using .Items.Add - which I can't get to work either
    Lastly - when I throw the DeBug.Print into SSMS - Of course it pulls the correct dataset



    Help

    Thanks All

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    In Access VBA it would be .AddItem. And combobox RowSourceType property would have to be set for ValueList.
    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.

  3. #3
    RunTime91 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2014
    Posts
    284
    Hey June ~ Thank you for chiming in

    The Combo is set for ValueList and I did try:
    Me.CMboClsdRsn.AddItem = Rs
    Me.CMboClsdRsn.AddItem = StrSQL

    I'm not sure what to put after AddItem - I just know the AddItem is looking for an object so I thought the = Rs would work
    But regardless of what I put I keep getting an Argument Not Optional error on the .AddItem

    The control is unbound - Not sure what I'm doing wrong

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    I went through something similar the other day, this might help https://youtu.be/0Qi_EGitNhk

    Also, head's up, I was having issues trying to call a table valued user define function from vba until I figured out the dbSQLPassThrough option for DAO OpenRecordset.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Loop through recordset. Add each record to combobox with .AddItem

    You said Google source indicated must use loop, was there example code?

    I have found code that sets combobox Recordset property with recordset object but cannot get to work.

    Examples of both in https://stackoverflow.com/questions/...dset-using-vba
    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.

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,235
    Have you tried to save the pass-through as a saved query that you set as the rowsource of the CmboClsdRsn combo like any other query and in the AfterUpdate event of the CmboOpnCls combo you just edit the SQL of the querydef to match the new selection?
    Code:
    Private Sub CmboOpnCls_AfterUpdate()
    Dim qdf  As DAO.QueryDef
    Set qdf  = CurrentDb.QueryDefs("qryYourPassThrough")
    
    StrSQL = "SELECT ATCCallDescription, ATCCategory " & _
               "FROM Tri.ATCCallResults " & _
               "WHERE ATCCategory = '" & Me!CmboOpnCls & "'"
    
    qdf.SQL=StrSQL 
    
    End Sub
    
    Private Sub CmboClsdRsn Enter()
         me.CmboClsdRsn.Requery
    End Sub
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Passthrough query confirmation
    By grumpy_whale in forum Queries
    Replies: 1
    Last Post: 10-29-2019, 09:42 AM
  2. Replies: 5
    Last Post: 10-26-2018, 03:40 PM
  3. Replies: 5
    Last Post: 06-14-2015, 07:56 PM
  4. Replies: 1
    Last Post: 02-01-2015, 12:16 AM
  5. Cannot Make Table with Passthrough Query
    By chasemhi in forum Import/Export Data
    Replies: 0
    Last Post: 12-05-2011, 01:30 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