Results 1 to 10 of 10
  1. #1
    dizy8 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2021
    Posts
    12

    Search Form Using Combo

    Hi All,

    I have a Search form using a combo. The program is already running but I want the output to be in ascending order. The program is below.

    Private Sub cboDocType_AfterUpdate()
    Dim myCompany As String
    myDocumentType = "Select * from tbl_Incoming where ([Doc_Type] = '" & Me.cboDocType & "')"


    Me.tbl_Incoming_subform.Form.RecordSource = myDocumentType
    Me.tbl_Incoming_subform.Form.Requery
    End Sub

    Hope somebody can help.

    Thank you!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Either use a query (or SQL statement) with ORDER BY clause as RecordSource or set SortBy property of form.
    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
    dizy8 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2021
    Posts
    12
    Hi June,

    Thank you for the reply. I am new in Access and I am blurr on your instruction. Do you mind you can type it in?

    Thank you so much.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    myDocumentType = "SELECT * FROM tbl_Incoming WHERE [Doc_Type] = '" & Me.cboDocType & "' ORDER BY Doc_Type"


    or whatever field you want to base sort on.

    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
    dizy8 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2021
    Posts
    12
    Hi June!

    Thank you for the input. I finally figured it out based on your suggestion. for the benefit of others that may have encountered the same problem as mine, please refer below.

    Thank you for the help!

    Private Sub cboCompany_AfterUpdate()
    Dim myCompany As String
    myCompany = "Select * from tbl_Incoming where [Company] = '" & Me.cboCompany & "' ORDER BY tbl_Incoming.ID ASC"
    Me.tbl_Incoming_subform.Form.RecordSource = myCompany
    Me.tbl_Incoming_subform.Form.Requery
    End Sub

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    FWIW when you set a recordsource, there is no need for a requery.?
    You are effectively doing that by setting the source, so just wasting resources, especially if that source is large.
    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
    dizy8 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2021
    Posts
    12
    Oh gosh it only work for my first combo box. If I use my second combo box search which is the (Doc_Type), it got error already.

    Private Sub cboCompany_AfterUpdate()
    Dim myCompany As String
    myCompany = "Select * from tbl_Incoming where [Company] = '" & Me.cboCompany & "' ORDER BY tbl_Incoming.ID ASC"
    Me.tbl_Incoming_subform.Form.RecordSource = myCompany
    Me.tbl_Incoming_subform.Form.Requery
    Me.cboDocType = Null
    End Sub


    Private Sub cboDocType_AfterUpdate()
    Dim myDocumentType As String
    myDocumentType = "Select * from tbl_Incoming where ([Doc_Type] = '" & Me.cboDocType & "' ORDER BY tbl_Incoming.ID ASC"
    Me.tbl_Incoming_subform.Form.RecordSource = myDocumentType
    Me.tbl_Incoming_subform.Form.Requery
    Me.cboCompany = Null
    End Sub

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Text need single quotes ' or triple double quotes if value could contain a '.
    Dates need #
    Numerics do not need anything
    Last edited by Welshgasman; 12-19-2023 at 07:04 AM.
    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

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    you have an unnecessary bracket

    ..... where ([Doc_Type] = '" & M......

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Parenthesis ( )

    Brackets [ ]

    Apostrophes ' '

    Quotes " "

    Must always be in pairs when serving as special characters in SQL.
    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.

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

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2018, 08:52 AM
  2. Replies: 5
    Last Post: 09-22-2016, 08:42 AM
  3. Search Combo Box on Form
    By Rhubie in forum Forms
    Replies: 15
    Last Post: 03-15-2013, 02:40 PM
  4. Replies: 7
    Last Post: 08-08-2012, 03:28 PM
  5. Replies: 1
    Last Post: 04-20-2012, 03:16 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