Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2019
    Posts
    1,065

    Need to requery/refresh DLOOKUP (in unbound control) after listbox selection has changed

    Hello:

    I need some assistance with re-querying a DLOOKUP control.

    Process:
    - I have a form including 2 listboxes allowing to select a *SINGLE* value each.
    - Upon executing a command button the 2 listbox values are passed into a query (criteria) and then automatically update the subform's data source.
    - This works great!

    Next:
    - Upon reopening the form, the subform's reloads (reopens) the data source based on the previous query execution.
    - While that's ok, I'd have added 2 unbound controls containing a DLOOKUP parameter.
    - That said, when reopening the form, the 2 unbound control show the previous value selection which matches the subform's underlying data set.



    Challenge:
    - While the above works fine, the moment I re-execute the query with different values from the listbox, the displayed values in the 2 unbound controls do NOT reflect the current listbox values.

    Control Source for DLookups in unbound control "txtFIELD_XYZ":
    Code:
    =DLookUp("FIELD_XYZ","Queryname")
    Code:
    Private Sub lst_LIST1_AfterUpdate()
        
        Me.txtFIELD_XYZ.Requery
      
    End Sub
    My question:
    - What code is missing/required to ensure the unbound's control DLOOKUP is updated immediately after the listbox value has changed?
    - Apparently, the above AFTERUPDATE function alone does not work properly. At least, the displayed value is NOT updated immediately... it seems like it's lagging 1 step behind.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    There is no "Single" option for listbox. Multi-select property offers: Simple, Extended, None.

    Listbox cannot be set for multiple selection.

    With setting of None, DLookup() updates immediately.
    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
    Join Date
    Feb 2019
    Posts
    1,065
    Property = None

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    Works for me. No code needed.

    Does query have multiple records?

    Your DLookup can only always return value from first record because there is no search criteria.

    =DLookup("FIELD_XYZ", "Queryname", "somefield=" &
    [listboxname])
    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.

  5. #5
    Join Date
    Feb 2019
    Posts
    1,065
    It would be awesome if that's working! Right now, I have placed the following into the unbound's control CONTROL SOURCE:

    =DLookup("ORGANIZATION", "Q212_MainForm_CustomerReview_Subform", "txtORGANIZATION_SELECTED=" & [lst_Organization])

    where:
    Table/query field name = "ORGANIZATION"
    Q212... = query name
    Unbound control name = txtORGANIZATION_SELECTED
    lst_Organization = listbox

    Right now, the following occurs:
    - Upon opening the form, unbound control shows: "#Error"... keep in mind, no value has been selected upon opening the form.
    - Once I click the listbox, "#Error" is blinking superfast...


    Process Recap:
    - I select values from 2 listboxes and then click command button to execute/update existing query.
    - Query is updated in the background and subform is immediately refreshed.
    - Whether I have 1 record or 100 records, the ORGANIZATION value is always the same. So, I did like the idea of just grabbing the "first" record's organization value.
    - But again, I do see the proposed solution as an issue as I want to immediately show an organization value once I open up the form... even before re-selecting the same/different organization value from the listbox.

    Makes sense?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    txtORGANIZATION_SELECTED is wrong usage. Must be a field in table/query such as the key field.

    Don't need to use DLookup. Include Organization field as a column of listbox. If it is in 3rd column, its index is 2 so expression in textbox: =lst_Organization.Column(2)

    Why isn't Organization displayed in listbox anyway? What is listbox for?
    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.

  7. #7
    Join Date
    Feb 2019
    Posts
    1,065
    June7:

    Almost there... =[lst_Organization].[Column](0) immediately displays a value once I select/change the listbox value. Great!

    HOWEVER... nothing is displayed upon opening the form! That's what I tried to use the DLookup pointing to the query (incl. the previously selected value from its last execution) which would always show a value.

    So, this proposed solution still does not meet the envisioned requirement.

  8. #8
    Join Date
    Feb 2019
    Posts
    1,065
    Oh, btw... yes, a listbox value is shown/highlighted once a selection has been made.

    However, given my subform is automatically populated based on the last/current execution, it's confusing to have subform records when the listbox doesn't have a value selected when the form opens.

    That's why I want to use the unbound control to always show the associated organization when the form opens.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    I don't understand "incl. the previously selected value from its last execution".

    If Organization is same in all records, then agree, don't need filter in DLookup.

    If nothing is selected in listbox, why would you need to have anything in textbox?

    So listbox is not bound? What is RowSource for listbox? Would there always be a record with ID of 1? Could set its DefaultValue property.
    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.

  10. #10
    Join Date
    Feb 2019
    Posts
    1,065
    "If nothing is selected in listbox, why would you need to have anything in textbox?"

    Again, the subform is linked to the query! Listbox merely updates query criteria but subform will always display values. That's why I want the unbound controls to display.

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

Similar Threads

  1. Replies: 8
    Last Post: 10-14-2019, 02:09 PM
  2. DLookup in unbound control
    By Abacus1234 in forum Forms
    Replies: 5
    Last Post: 10-01-2015, 11:47 AM
  3. Replies: 8
    Last Post: 08-30-2015, 02:54 PM
  4. Replies: 1
    Last Post: 10-22-2014, 12:44 PM
  5. Requery vs. Refresh
    By tylerg11 in forum Forms
    Replies: 3
    Last Post: 03-28-2012, 11:54 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