Results 1 to 5 of 5
  1. #1
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65

    Find if there is any blank values in a query field

    Hello!



    When I open my MainMenu form, I'd like to have a warning massage if there is any null values in a specific query (Query1).
    I've tried so far the following code in my On Load Event of the MainMenu form, but there is something missing.

    Code:
    Private Sub Form_Load()
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("Query1", dbOpenSnapshot, dbReadOnly)
    rs.FindFirst IsNull("FieldNameToFindNullValue")
    If True Then
    Me.Warning.Visible = True
    Me.Warning.Value = "Empty values!"
    Else
    Me.Warning.Visible = False
    End If
    End Sub
    What am I doing wrong?

    Thx!

  2. #2
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    This code works fine if it finds any null value:

    Code:
    Private Sub Form_Load()
    Dim seepp As String
    seepp = DLookup("AnotherFieldOfTheQuery", "Query1", "IsNull([FieldNameToFindNullValue])")    
    If seepp = True Then ...
    But if it doesn't find any null values, I get Run-time error '94': "Invalid use of Null"

    Any ideas?

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    I wrote this routine to demo how the looping construct with recordset you provided in post1 could b modified to do what you are asking. Namely, to review a query and see if any of the fields were NULL or were zero length strings.


    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : boboivan
    ' Author    : mellon
    ' Date      : 21/02/2016
    ' Purpose   : To see if there are any null or ZLS(zero length strings) in any fields in a query
    '
    ' If any fields are NULL or ZLS, then increment variable emptyFieldCount
    '---------------------------------------------------------------------------------------
    '
    Sub boboivan()
              Dim sql As String   'the query sql
    10       On Error GoTo boboivan_Error
    
    20        sql = "SELECT ancestor.name, ancestor.id, ancestor.deathdate, ancestor.birthdate, ancestor.preregcode " _
                    & " FROM ancestor;"
              Dim db As DAO.Database
              Dim rs As DAO.Recordset
              Dim emptyFieldCount As Integer
    30        emptyFieldCount = 0
              Dim i As Integer
    40        Set db = CurrentDb
    50        Set rs = db.OpenRecordset(sql, dbReadOnly)
    60        With rs
    70            Do While Not .EOF
    80                If IsNull(![name]) Or Len(![name] & "") = 0 Then emptyFieldCount = emptyFieldCount + 1
    90                If IsNull(![id]) Or Len(![id] & "") = 0 Then emptyFieldCount = emptyFieldCount + 1
    100               If IsNull(![deathdate]) Or Len(![deathdate] & "") = 0 Then emptyFieldCount = emptyFieldCount + 1
    110               If IsNull(![birthdate]) Or Len(![birthdate] & "") = 0 Then emptyFieldCount = emptyFieldCount + 1
    120               If IsNull(![preregcode]) Or Len(![preregcode] & "") = 0 Then emptyFieldCount = emptyFieldCount + 1
    130           rs.MoveNext
    140           Loop
    150       End With
    160       Debug.Print IIf(emptyFieldCount > 0, "EmptyFieldsFound", "No EmptyFields")
    
    170      On Error GoTo 0
    180      Exit Sub
    
    boboivan_Error:
    
    190       MsgBox "Error " & Err.Number & " in line " & Erl & " (" & Err.Description & ") in procedure boboivan of Module AccessD_KillText"
    
    End Sub
    My table ancestor

    Code:
    name id deathdate birthdate preregcode
    sam 12/03/1976 01/11/1922 21
    bob 1 13/03/1976 31/10/1922 44
    jim 2 17/09/1964 22/03/1944 63
    ken 45 07/02/2006 09/09/1999 82
    TestPerson 169 19/03/2009 21/07/1937
    TestPerson 170 19/03/2009 21/07/1937
    TestPerson 171 19/03/2009 21/07/1937
    TestPerson172 172 19/03/2009 21/07/1937
    TestPerson173 173 19/03/2009 21/07/1937
    TestPerson174 174 19/03/2009 21/07/1937
    TestPerson175 175 19/03/2009 21/07/1937
    TestPerson176 176 19/03/2009 21/07/1937
    Result of the routine:

    EmptyFieldsFound

    Good luck.

  4. #4
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    Thank you Orange! You're a gent!

    I've tried to run the macro in the Event On Load form, with an if condition like:

    Code:
    Private Sub Form_Load()
    If boboivan True Then 
    do stuff
    End if
    End Sub
    and I get a Compile error: Expected variable or procedure, not module".

    On the other hand, I really think that the second code is more useful to me, as can return also in my warning msgbox the value in "AnotherFieldOfTheQuery" associated with the null value in "FieldNameToFindTheNulValue".
    How can I fix that Run-time error '94': "Invalid use of Null"?

    Thx a lot again!

  5. #5
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    Got it!

    The working code is:

    Code:
    Private Sub Form_Load()
    Dim seepp As String
    Dim countpp As String
    
    
    countpp = DCount("AnotherFieldOfTheQuery", "Query1", "IsNull([fieldNameToFindNullValue])")
        
    If countpp <> 0 Then
    seepp = DLookup("AnotherFieldOfTheQuery", "Query1", "IsNull([fieldNameToFindNullValue])")
    do other stuff
    End If
    
    End Sub

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

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2016, 10:49 AM
  2. Query to find Values that do NOT exist
    By SoonerLater in forum Queries
    Replies: 4
    Last Post: 09-19-2015, 06:29 PM
  3. Query Blank Values
    By kgbo in forum Queries
    Replies: 2
    Last Post: 10-29-2013, 03:54 PM
  4. CrossTab Query - Blank Values
    By bullwinkle55423 in forum Queries
    Replies: 4
    Last Post: 07-17-2013, 01:59 PM
  5. Using if to find blank fields?
    By newtoAccess in forum Queries
    Replies: 4
    Last Post: 03-20-2011, 10:48 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