Results 1 to 8 of 8

Thread: [RESOLVED] SQL DISTINCT only works for the first item in my listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Resolved [RESOLVED] SQL DISTINCT only works for the first item in my listbox

    This is a 2 part question:

    1. I use the following sql statement fill my second listbox (this works fine):

    strSQL = "SELECT DISTINCT * FROM Vehicle"

    The problem is that only the first item in the first listbox will be affected by the DISTINCT in the sql, but the remaining items in the first listbox will not.

    Ex.. If the user selects "BrassTag" from the listbox, then the second listbox will populate with the numbers for "BrassTag" with no duplicates.

    When the user selects the next item (ClassCode) from the same listbox, then the second listbox will populate with the data for "ClassCode",but with duplicates.

    2. How to get the blank fields out of my listbox? Certain fields in my database have nothing in them and when the sql retrieves the data for my listbox I get blank spaces.

    I tried using trim, but it didnt work
    Last edited by cfd33; Apr 30th, 2007 at 10:13 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SQL DISTINCT only works for the first item in my listbox

    When loading your listbox, just create an If Then cod block to test for a nullstring "". Skip .additem if its nullstring etc.

    Now for the non-distinct problem: If you are only using a single field in your select query then dont call all fields as the distinct is for all fields at once. So if field1 and field2 combined are distinct then your good but if field1 is distinct but field2 is a different value then its still a distinct row.

    Ie: First record distinct
    Field1
    "Dog"

    Field2
    "Cat"

    Second record distinct but will have a dup value for one of the fields:
    Field1
    "Dog"

    Field2
    "Cow"

    You see the two rows are distinct as its a combination of the two fields that when compared are distinct, not individual fields compared.


    So to solve it just do a ...

    strSQL = "SELECT DISTINCT Field1 FROM Table1;"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: SQL DISTINCT only works for the first item in my listbox

    Rob,

    Thanks for responding.

    I'm not quite sure that I follow you. Here is my code for the lstboxes:

    vb Code:
    1. Option Explicit
    2. Dim rs As ADODB.Recordset
    3. Dim strSQL As String
    4. Dim strQuery As String
    5. Dim strClassCode As String
    6.  
    7. Private Sub Form_Load()
    8.     openConnection
    9.    
    10.     With lstQuery
    11.         .Clear
    12.         .AddItem "BrassTag"
    13.         .AddItem "ClassCode"
    14.         .AddItem "MfgYear"
    15.         .AddItem "Bureau"
    16.         .AddItem "Mfg"
    17.         .AddItem "AquPrice"
    18.         .AddItem "Model"
    19.         .AddItem "Misc"
    20.         .AddItem "PermAssign"
    21.         .AddItem "CurrAssign"
    22.         .AddItem "StaTakeHome"
    23.         .AddItem "TakeHome"
    24.         .AddItem "Hours"
    25.         .AddItem "CurOdometer"
    26.         .AddItem "ReadDate"
    27.        
    28.     End With
    29.     'rs.Close
    30.    
    31.    
    32. End Sub
    33.  
    34. Private Sub Form_Unload(Cancel As Integer)
    35.     'rs.Close
    36.     Set rs = Nothing
    37.     closeConnection
    38.     Unload Me
    39.     frmVMD.Enabled = True
    40.    
    41. End Sub
    42.  
    43. Private Sub lstQuery_Click()
    44.      Set rs = New ADODB.Recordset
    45.      
    46.     If lstQuery.ListIndex <> -1 Then
    47.         strQuery = lstQuery.List(lstQuery.ListIndex)
    48.         rs.CursorLocation = adUseClient
    49.        
    50.         strSQL = "SELECT DISTINCT * FROM Vehicle"
    51.         'rs.Close
    52.        
    53.         rs.Open strSQL, DBCon, adOpenDynamic, adLockOptimistic
    54.        
    55.         With lstQueryResults
    56.             .Clear
    57.             Do While Not rs.EOF
    58.                 .AddItem Trim(rs.Fields(strQuery).Value) & ""
    59.                 rs.MoveNext
    60.             Loop
    61.         End With
    62.    
    63.     Else
    64.         If (rs.BOF Or rs.EOF) Then
    65.             MsgBox "No matches found; Please check your criteria", vbInformation + vbOKOnly, "Vehicle Query"
    66.         End If
    67.     End If
    68.  
    69.    
    70.    
    71. End Sub

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SQL DISTINCT only works for the first item in my listbox

    The listbox's contents are to be used as the field to select or as a criteria to filter byu on a single field?

    strSQL = "SELECT DISTINCT " & lstQuery.List(lstQuery.ListIndex) & " FROM Vehicle"

    Or

    strSQL = "SELECT DISTINCT Field1 FROM Vehicle WHERE Field1 = '" & lstQuery.List(lstQuery.ListIndex) & "';"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: SQL DISTINCT only works for the first item in my listbox

    Rob,

    The first code works like a charm (funny thing is; I had that written like your code in the beginning but couldnt get mine to work and now I see why; I used '" & strquery & "' -- those little things (') get me all the time,lol)

    I tried to write the If block for the IsNull, but didnt see a .skip anywhere?

    vb Code:
    1. With lstQueryResults
    2.             .Clear
    3.             Do While Not rs.EOF
    4.                 If IsNull(rs.Fields(strQuery).Value) Then
    5.                    
    6.                 Else
    7.                     .AddItem rs.Fields(strQuery).Value & ""
    8.                     rs.MoveNext
    9.                 End If
    10.             Loop
    11.         End With

    Does this look correct (with the exception of missing the .skip)?


    LOL, I am a complete moron and I'm really tired; I just notice that you didnt write .skip, but Skip the additem. Man, I'm going to bed.
    Last edited by cfd33; Apr 30th, 2007 at 11:29 PM.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SQL DISTINCT only works for the first item in my listbox

    Not an actual skip method but by code like this...
    Code:
    With lstQueryResults
        .Clear
        Do While Not rs.EOF
            If Not IsNull(rs.Fields(strQuery).Value) Then
                .AddItem rs.Fields(strQuery).Value & ""
            End If
            rs.MoveNext
        Loop
    End With
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: SQL DISTINCT only works for the first item in my listbox

    Rob,

    The code will take out most of the blank spaces in the listbox, but still leaves one blank at the very beginning.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    449

    Re: SQL DISTINCT only works for the first item in my listbox

    Got it!

    Thanks for all of your help RobDog!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width