Results 1 to 12 of 12
  1. #1
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191

    Combo Field List

    Hi Guy's, I have 2 combos

    1: cboField, this is RowSource Field List

    2: cboData RowSource Table/Query

    So i am going to update the rowsource of cboData based on the Field name in cboField



    There are quite a number of fields in cboField

    Is it a matter of setting a load of if cboField statements then the row source of cboData = etc....

  2. #2
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Just a quick question, don't suppose i can string the field name based on the value of cboField ?

    in this case field selected is Supplier

    Can i String the bold text ? and take out any if statements ?

    Code:
    If Me.cboField = "Supplier" ThenMe.cboData.RowSource = "SELECT tblExpenses.Supplier From tblExpenses " _
        & "GROUP BY tblExpenses.Supplier " _
        & "HAVING (((tblExpenses.Supplier) Is Not Null)) " _
        & "ORDER BY tblExpenses.Supplier;"
    Me.cboData.Requery
    End If

  3. #3
    xps35's Avatar
    xps35 is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    229
    Yes you can.
    Code:
    "SELECT tblExpenses." & Me.cboField & " From tblExpenses "

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    I would use a Where instead of Having
    Also when you change the rowsource I think a requery is automatic?, so no need for yours?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    I am getting Undefined function tblExpenses.SupplierWHERE in expression when clicking on cboData


    Code:
        Me.cboData.RowSource = "SELECT tblExpenses." & Me.cboField & " From tblExpenses " _    & "GROUP BY tblExpenses." & Me.cboField & "" _
        & "WHERE (((tblExpenses." & Me.cboField & ") Is Not Null)) " _
        & "ORDER BY tblExpenses." & Me.cboField & ";"

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    Put spaces at the start of each new line. Not debug.printing again I see
    Put it into a string and use that.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    I think your problem is "" vs " " i.e. that is where you probably intended to insert a space. FWIW I prefer this format as it's less cluttered:
    Code:
    str = "SELECT tblExpenses." & Me.cboField & " From tblExpenses GROUP BY"
    str = str & " tblExpenses." & Me.cboField & " WHERE (((tblExpenses." & Me.cboField & ")"
    str = str & " Is Not Null)) ORDER BY tblExpenses." & Me.cboField & ";"
    I don't see how what you posted could work with a line continuation character in the middle of a line, but then again, I don't use them, so...
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Thanks guys, will adapt and try your suggestions

  9. #9
    CarlettoFed is offline Competent Performer
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2019
    Posts
    255

    Risposta

    In the presence of grouping
    - if you use the WHERE filter expression, it goes before GROUP BY
    - if you use the filter expression HAVING it goes after the GROUP BY

    so in this case the code is the following:

    Code:
        Me.cboData.RowSource = "SELECT tblExpenses." & Me.cboField & " From tblExpenses" _
            & " WHERE tblExpenses." & Me.cboField & " Is Not Null " _
            & " GROUP BY tblExpenses." & Me.cboField
    the final part & "ORDER BY tblExpenses." & Me.cboField & ";" it can be omitted as GROUP BY already performs the sorting of the data relative to those in the field on which it is carried out.

  10. #10
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    This done the job nicely, thank you all

    Code:
        Me.cboData.RowSource = "SELECT tblExpenses." & Me.cboField & " From tblExpenses" _        & " WHERE tblExpenses." & Me.cboField & " Is Not Null " _
            & " GROUP BY tblExpenses." & Me.cboField _
            & " ORDER BY tblExpenses." & Me.cboField & ";"

  11. #11
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Hi Guys, just to add this, how can I Run a Sum where the Field Name = etc, having a problem trying to do this bit, this will save many If statements etc..

    Code:
    curTotal = DSum("TotalAmount", "tblExpenses", "[" & Me.cboField & "] = '" & Me.cboData & "' And [Year] Between " & lYearStart & " And " & lYearEnd)

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    AGAIN! Put the criteria into a string variable and Debug.Print it.
    When you get it correct, use that variable in your Domain function.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 13
    Last Post: 12-02-2021, 12:11 AM
  2. Combo list selection to activate a field
    By Emmanuel in forum Forms
    Replies: 7
    Last Post: 05-18-2021, 04:06 PM
  3. Auto List Combo Field Editing
    By raychow22 in forum Forms
    Replies: 7
    Last Post: 09-09-2017, 08:44 PM
  4. Replies: 1
    Last Post: 03-17-2012, 08:49 PM
  5. Combo Box - Field List Values
    By jennyaccord in forum Forms
    Replies: 5
    Last Post: 07-29-2011, 01:49 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