Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    jabarlee is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    49

    Select rows with null values


    Hello everyone,

    the title is not very accurate, I need your help with this:
    How can I select (actually delete) a record where all the values except the first column are null? For example:

    vvvv
    xxxx 1234
    yyyy werwe
    zzzz

    I would like to select records 1 and 4.
    The first column will always have a value, and the number of columns is not fixed.

  2. #2
    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 variable number of fields is suspect. The only way that comes to mind would involve VBA. Simplest offhand is opening a recordset on the table and looping the fields collection, skipping the first. If every field is null delete the record, else move to the next.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Wouldn't "DELETE FROM yourTable WHERE fld2 Is Null AND fld2 Is Null AND fld3 Is Null AND ..." work?
    Or am I missing something (likely)?

    EDIT
    actually more like DELETE yourTable.* FROM yourTable WHERE ((yourTable .fld2) Is Null) AND ((yourTable .fld3) Is Null) ((yourTable .fld4) Is Null)...
    I was in too much of a rush I guess.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  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
    You missed "the number of columns is not fixed". That would certainly work with a static table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    My bad. I should have re-read because it just seemed too simple. Agree with your answer.

    jabarlee: if you go that route and need help, you will have to at least post the name of the query that's involved although a zipped copy of your db would be even better to work with.
    If you only have a couple of dozen records, you might as well manually delete them. You can select contiguous records in a table if that helps. Not sure what happens in Access if you filter a table/query to where all said fields are Null, select all rows then delete. I'm pretty sure you can do this in Excel and only the visible rows are deleted.

    However, you might be dealing with a crosstab query if the number of fields isn't constant. In that case, I doubt you will be able to do this even via vba. Can you explain why the field count is variable?

  6. #6
    jabarlee is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    49
    Thank you for your answers.

    this is a temp table derived from a crosstab query, hence the column count is not fixed. (It is a table with lab exam results, per date).
    As the crosstab query results in many records that actualy have no lab results entries, I want to delete them to clean it up for presentation purposes.

    To loop through a record set seems a pretty clever idea, I will try it and come back with the results.

    I wonder though, how come that SQL doesn't have a "SELECT * EXCEPT"-like function, that would be really useful

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It would be handy, but to my knowledge doesn't exist.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    A ct query is for arranging normalized data from rows to columns. You shouldn't be making tables that are structured that way.

    Seems to me that there is a sort of "except" thing. It's called criteria, no?

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    the problem is not knowing the number of fields. only way I think you could do it is with a vba function looping through a recordset. Something like this untested code

    Code:
    Sub delEmpties(Tname As String)
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim i As Integer
        
        Set db = CurrentDb
        Set rst = db.OpenRecordset("SELECT * FROM " & Tname, dbOpenSnapshot)
    
        While Not rst.EOF
    
            For i = 1 To rst.Fields.Count - 1
    
                If Nz(rst.Fields(i), "") <> "" Then Exit For 'i count will be less that field count
    
            Next I
    
            If i = rst.Fields.Count Then db.Execute "DELETE * FROM " & Tname & " WHERE " & rst.Fields(0).Name & "= '" & rst.Fields(0) & "'" 'assumes the first field is text
    
            rst.MoveNext
    
        Wend
    
        set rst=nothing
        set db=nothing
    
    End Sub
    Last edited by CJ_London; 10-20-2018 at 12:13 PM. Reason: missed a quotation mark

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    There's your fish.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Never mind - I obviously was/am having a mental breakdown.

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I don't normally provide the fish, just the rod. But it's a slow evening - wife is watching her favourite programme and I'm looking for distractions to avoid doing the paperwork

  13. #13
    jabarlee is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    49
    Ajax thank you, I didn't see your answer in time!

    I used the following code and it works, do you think it is alright?
    Code:
        Dim SQL As String
        Dim rs1 As DAO.Recordset
        Dim fld As DAO.Field
        Dim f As Integer
    
        SQL = " SELECT * FROM tbl_TEMP_LabResults_Full"
    ' I used a dynaset recordset so I can use the .delete method
        Set rs1 = CurrentDb.OpenRecordset(SQL, dbOpenDynaset)
        
        If Not rs1.BOF And Not rs1.EOF Then
            rs1.MoveLast
            rs1.MoveFirst
            While (Not rs1.EOF)
                f = 0
                For Each fld In rs1.Fields
                    If Len(fld.Value) > 0 Then f = f + 1
    ' the 1st column has every field filled, so if there is at least a second field that is not empty, exit the loop
                    If f = 2 Then Exit For
                Next
    'if there is only one (the 1st) field filled, delete the record
                If f = 1 Then rs1.Delete
                rs1.MoveNext
            Wend
        End If
        
        rs1.Close
        Set rs1 = Nothing

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Good job and thanks for proving that people don't always need the fish handed to them. In my experience you don't need:

    Code:
    If Not rs1.BOF And Not rs1.EOF Then
      rs1.MoveLast
      rs1.MoveFirst
    ...
    End If
    The EOF loop will start at the first record anyway, and won't error on an empty recordset. Is the test accounting for Null values? I'd typically use

    If Len(fld.Value & vbNullString) > 0 Then

    I'm not on a computer so can't test yours.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    jabarlee is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Posts
    49
    Paul, thank you for your answer.

    You're right, I removed those lines and everything is ok.
    As for the Null values, LEN(fld.Value) > 0 does work, but now I realize that it might as well not have worked. I have used it several times, though

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2018, 05:56 PM
  2. Replies: 8
    Last Post: 02-08-2016, 11:40 AM
  3. Access : Null / Blank rows select
    By jagadeesh.rt in forum Access
    Replies: 4
    Last Post: 09-11-2014, 12:26 AM
  4. Finding the Max Date and Null Values if Null
    By SpdRacerX in forum Queries
    Replies: 1
    Last Post: 02-03-2012, 06:29 AM
  5. Replies: 2
    Last Post: 08-28-2011, 06:06 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