Results 1 to 8 of 8
  1. #1
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130

    Populating Datasheet with SQL Query

    Good Afternoon:

    I am trying to populate a datasheet form with a SQL query. When I execute, the fields say #Name?, not the actual data.

    Code:
    Private Sub cmdEditRec_Click()
    Dim gstrEditSQL As String
    
    
    gstrEditSQL = ""
        Select Case Me.cbxSearchBy.Value
            Case "Customer Name"
                gstrEditSQL = "'SELECT(UPC, Category, Employee, Marketplace, Defect, CustomerName, OrderNumber, Status, DefectNotes, CallNotes) FROM DefectEvents WHERE CustomerName =' & Me.lstSearchResults.Selected;"
            Case "UPC"
                gstrEditSQL = "'SELECT(UPC, Category, Employee, Marketplace, Defect, CustomerName, OrderNumber, Status, DefectNotes, CallNotes) FROM DefectEvents WHERE UPC =' & Me.lstSearchResults.Select;"
            Case "Order Number"
                gstrEditSQL = "'SELECT(UPC, Category, Employee, Marketplace, Defect, CustomerName, OrderNumber, Status, DefectNotes, CallNotes) FROM DefectEvents WHERE OrderNumber =' & Me.lstSearchResults.Selected;"
            Case "Status"
                gstrEditSQL = "'SELECT(UPC, Category, Employee, Marketplace, Defect, CustomerName, OrderNumber, Status, DefectNotes, CallNotes) FROM DefectEvents WHERE Status =' & Me.lstSearchResults.Selected;"
            Case "Defect Category"
                gstrEditSQL = "'SELECT(UPC, Category, Employee, Marketplace, Defect, CustomerName, OrderNumber, Status, DefectNotes, CallNotes) FROM DefectEvents WHERE Category =' & Me.lstSearchResults.Selected;"
        End Select
        DoCmd.Close acForm, Me.Name
        DoCmd.OpenForm "frmDefViewEdit", acNormal, gstrEditSQL
     
    End Sub
    Do I need to set the value for each in code?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you dont need any code.
    make the 5 queries,
    put them in a table for a combo to read: SearchBy, Qry
    (user sees col1, but col 2 is the value)

    docmd.openquery me.cboBox

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'd go a different way. Base the form on a query with your SQL without criteria. Then cut your code to just the last bit and open the form this way:

    http://www.baldyweb.com/wherecondition.htm

    In other words:

    Code:
    Case "Customer Name"
      gstrEditSQL = "CustomerName ='" & Me.lstSearchResults.Selected & "'"
    then

    DoCmd.OpenForm "frmDefViewEdit", , , gstrEditSQL
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    By the way your SQL is invalid, you're never adding the single quote after the listbox reference.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130
    Here is my changed code.

    Code:
    Case "Work Order"
                  Select Case Me.cbxSearchBy.Value
                    Case "Order Number"
                        gstrViewSQL = "OrderNo ='" & Me.lstSearchResults.Selected(0) & "''"
                        DoCmd.OpenForm "frmWOView", , , gstrViewSQL
                    Case "UPC"
                        gstrViewSQL = "UPC ='" & Me.lstSearchResults.Selected(0) & "''"
                        DoCmd.OpenForm "frmWOView", , , gstrViewSQL
                    Case "Employee"
                        gstrViewSQL = "Employee ='" & Me.lstSearchResults.Selected(0) & "''"
                        DoCmd.OpenForm "frmWOView", , , gstrViewSQL
                End Select
    It is opening the form correctly, but all fields are blank. I suspect that it is a problem with the Control Source property with the textboxes on frmWOView, but am unsure what it should be. There are 4 to populate (tbxOrderNo, tbxUPC, tbxEmployee, and tbxDate_Time).

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Use this:

    http://www.baldyweb.com/ImmediateWindow.htm

    to see what gstrViewSQL is resolving to. Looks like you have an extra apostrophe at the end.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    dccjr3927 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    130
    gstrViewSQL is resolving to OrderNo ='0' for this code

    Code:
    Case "Order Number"
                        gstrViewSQL = "OrderNo ='" & Me.lstSearchResults.Selected(0) & "'"
                        DoCmd.OpenForm "frmWOView", acFormDS , , gstrViewSQL
                        Debug.Print gstrViewSQL
                    Case "UPC"
                        gstrViewSQL = "UPC ='" & Me.lstSearchResults.Selected(0) & "'"
                        DoCmd.OpenForm "frmWOView", acFormDS, , gstrViewSQL
                    Case "Employee"
                        gstrViewSQL = "Employee ='" & Me.lstSearchResults.Selected(0) & "'"
                        DoCmd.OpenForm "frmWOView", acFormDS, , gstrViewSQL
    In the case of OrderNo, there will only be 1 record result. However, with UPC and Employee, there will be multiple records. I read the page on Multi-Select Listbox http://www.baldyweb.com/multiselect.htm but this seems to be for reports. Would it be the same for a form? Since the user will not be able to edit these, should I use a report instead?

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The same method will work for a form, but it appears you aren't getting a valid value from the listbox. Is it multiselect or not? If it is you need to use the looping code to get all the selections, if not

    Me.lstSearchResults

    should return the selected value.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Related fields populating in Datasheet view
    By kstrecker1701 in forum Access
    Replies: 2
    Last Post: 08-01-2018, 10:46 AM
  2. Populating a query from two tables
    By Peter Simpson in forum Queries
    Replies: 46
    Last Post: 11-08-2017, 07:47 AM
  3. Replies: 8
    Last Post: 08-26-2012, 11:11 PM
  4. Replies: 4
    Last Post: 05-04-2012, 03:41 PM
  5. query column not populating text box.
    By mejia.j88 in forum Queries
    Replies: 2
    Last Post: 02-23-2012, 02: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