Results 1 to 5 of 5
  1. #1
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162

    Output query to list box

    Hi,



    Im trying to output a query to a listbox the problem being if I set the source of the listbox to the query the query runs regardless of the error so the incorrect results are displayed. Is there a way to output a query using VBA?

    Thanks

  2. #2
    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,726
    What is the query? And what exactly is the error you mention?

  3. #3
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Its not so much an error, there are 2 queries which run depending on the selection from a combo. If the user selects all then the all query runs and if a user selects individual then the individual query runs. There is only 1 listbox on the form which I am trying to output both (obviously not at the same time) queries to.

    The only way I have managed to output to the listbox is to use the wizard, but Im guessing this isnt the correct way to go about this for this perticular task?

    thanks

  4. #4
    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,726
    So you have a combo, with options for all or one of several individuals.
    Once you make your combo selection (afterUpdate event), then you change the row source of the list box to the ALL query or the INDIVIDUAL query. Is that correct?

  5. #5
    shabbaranks is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Posts
    162
    Im getting there, but the queries arent outputting to the listbox - any ideas please?

    Code:
    Option Compare Database
    Private Sub TotalHoursOk_btn_Click()
        Dim dbs As DAO.Database
        Dim qdf As DAO.QueryDef
        Dim rst As DAO.Recordset
        Dim sProjectRef As String
        Dim sTotalhours As String
        Dim MySQL As String
        Dim sGroupID As String
        Dim sUserID As String
        Dim sFormUser As String
        sFormUser = Me.CurrentUser
        sUserID = Forms!TimesheetForm!LoggedInUser.Value
        sGroupID = DLookup("DepGroupID", "UserNames_tbl", "sUser='" & sUserID & "'")
        If Me.ProjectRef_txtbox = "" Or IsNull(Me.ProjectRef_txtbox) Then
            MsgBox "Please Enter a Project Number"
        Else
            If Me.TotalHours_Combo = "" Or IsNull(Me.TotalHours_Combo) Then
                MsgBox "Please Select from the drop down list"
            End If
           Select Case Me.TotalHours_Combo
           Case "All Employees"
           If sGroupID = "2" Then
            Me.Totalhrs_listbox.RowSource = ("Select * from qryTotalHrsALL")
            
            '*******************
            'DoCmd.OpenQuery "qryTotalHrsAll"
                  'MySQL = "Select * From TimesheetTable WHERE [ProjectRef] LIKE '*" & Me.ProjectRef_txtbox & "*'"
                   ' QDef (MySQL)
                    'Call TotalHours("qryTotalHrsAll", MySQL)
                    'Totalhrs_listbox.RowSource = MySQL
                    
            '***********************
                                           
                Else
                   MsgBox "You are only allowed to query your own hours, change your selection to current user", vbInformation
                End If
               Me.TotalHours_Combo.SetFocus
            Case "Current User"
            Me.Totalhrs_listbox.RowSource = ("Select * from qryTotalHrsCur")
                
            '*************************
                'DoCmd.OpenQuery "qryTotalHrsCur"
                'MySQL = "SELECT * FROM TimesheetTable WHERE ProjectRef LIKE '*" & Me.ProjectRef_txtbox & "*'AND " & sFormUser & ""
                'QDef (MySQL)
                'Call TotalHours("qryTotalHrsCur", MySQL)
           '*************************
                
           End Select
            
            
    End If
    End Sub

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

Similar Threads

  1. Ho do I output query to a list box
    By shabbaranks in forum Queries
    Replies: 1
    Last Post: 02-06-2012, 07:17 AM
  2. Query output for forms & reports
    By Dega in forum Forms
    Replies: 2
    Last Post: 08-20-2010, 12:45 PM
  3. Insert Query output into a table
    By ammu_sridhar in forum Programming
    Replies: 1
    Last Post: 06-12-2009, 01:09 AM
  4. Output Query to Text
    By denileigh in forum Queries
    Replies: 1
    Last Post: 05-27-2006, 12:34 PM
  5. How to output only current record in query
    By mslieder in forum Access
    Replies: 0
    Last Post: 01-20-2006, 05:48 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