Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100

    How to identify records with empty fields that are required?

    Hi all - I have a table named FieldReq that has the same names found in the Data table. The purpose of the FieldReq table is to know which field is required to be populated in the Data table.



    Needs:

    I would like to identify records that require data but have no value.
    I would like to identify records that do not require data but have values.

    I have no control over the Data table. This is an internal source that is downloaded and I only have to verify that everything is filled in as required. Note that this table has up to 25k records.

    I have looked into creating a Index Query but I found no examples that I could use to tie it to the FieldReq table. All I found online were examples of Forms but nothing just tied to a query.

    If someone could point me to the right direction (examples) so I can build this, I'd appreciate it.

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    I have a table : tRules
    Fld, Enabled
    ----------------
    Name, true
    Phone, true
    etc.

    (the enabled field lets you turn on/off validation of that field)

    before the person can leave (or print ) ,
    i load this recordset (of enabled fields) into a collection,
    then i scan the recordset of the current record and compare the two.
    If it hits a field with no data, msgbox: "XXX field is required"

    it it scan thru w/o errors, then they can leave screen or print document.

  3. #3
    madpiet is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    151
    Why not just set the fields to be required at table level? Then those are validated before the insert is even attempted? If you have a complex rule that depends on more than on column, you either have to use a Form's BeforeInsert event or you need maybe a data macro. (Not sure, I pretty much stopped using Access for anything serious before they were added).

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Further to madpiet's suggestion:

    -you can iterate over all of your tables, and
    -for each field you can test the Required Property

    Here's an example,note it checks all non-system tables

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: IdentifyRequiredFields
    ' Purpose: To Identify which fields in tables are required.
    ' Procedure Kind: Sub
    ' Procedure Access: Public
    ' Author: Jack
    ' Date: 16-Feb-23
    ' ----------------------------------------------------------------
    Sub IdentifyRequiredFields()
        Dim tdf As DAO.TableDef
        For Each tdf In CurrentDb.TableDefs
            If Not tdf.name Like "MSys*" Then RequiredOutput tdf
    
    'Note
    'You could add other table names to be excluded OR
    ' Only include the table names you want to interrogate
    
        Next
    End Sub
    The called subprocedure

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: RequiredOutput
    ' Purpose: Procedure to show if the table field is Required or not
    ' Procedure Kind: Sub
    ' Procedure Access: Public
    ' Parameter tdfTemp (TableDef):
    ' Author: Jack
    ' Date: 12-Mar-20
    ' ----------------------------------------------------------------
    Sub RequiredOutput(tdfTemp As TableDef)
           
        On Error GoTo RequiredOutput_Error
              Dim fld As Field
           
              ' Enumerate Fields collection of the specified TableDef
              ' and show the Required property.
    10        Debug.Print "Fields in " & tdfTemp.name & ":"
    20        For Each fld In tdfTemp.Fields
    30          '  If fld.Required Then
    40                Debug.Print , fld.name & ", Required = " & _
                          fld.Required
    50          '  Else
    60          ' End If
    70        Next fld
           
        
        On Error GoTo 0
    RequiredOutput_Exit:
        Exit Sub
    
    RequiredOutput_Error:
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure RequiredOutput, line " & Erl & "."
        GoTo RequiredOutput_Exit
    End Sub

  5. #5
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Can you post a screenshot of the FieldReq table? Does it contain just the required fields of the Data table? Do the data types match or the FieldReq fields are just Yes/No?
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    madpiet is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    151
    It's going to be tedious, but you can create a query, and add all the columns from the table you're testing. Then to test for "Required"/"Not Null" violations, you'd OR together a bunch of [ColumnName] IS NULL... so something like

    SELECT *
    FROM MyTable
    WHERE RequiredColumnName1 IS NULL
    OR RequiredColumnName2 IS NULL
    etc.

    Otherwise, you could do this in VBA and loop over the fields collection, and do similar and get a row count. If the rowcount > 0 then you'd print out the column name that's violating the rule.
    <Insert aircode siren here>

    dim tdf as dao.tabledef
    dim fld as dao.field
    set tdf = currentdb.TableDefs(strTable)
    for each fld in tdf.fields
    if fld.Required = True Then
    '--- do something: like write to output, or to table
    CurrentDb.Execute "INSERT INTO LogTable (TableName, FieldName, Required) VALUES ('" & tdf.name & "', '" & fld.Name & "'," & fld.required & ");", dbFailOnError
    next fld

    set tdf = nothing

  7. #7
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100

    How to identify records with empty fields that are required?

    Thank you all for responding. A little background on the Data table. This data comes from an old system (1990s - no joke) that no longer gets updated with field requirements. So attached is an example that I made. As you can see table FieldReq has the name of fields and if they are required by product.

    I would just like an alert of some kind that would tell me to look at that specific record.
    Attached Files Attached Files

  8. #8
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Is the real Data table's structure identical to your sample, meaning it does not have a primary key?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100
    Yes but I can add a primary key because I upload the file to access since the data table is an excel file I download from the system.

  10. #10
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    OK, that's is what I did, I'm working on a VBA function now.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #11
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Hi,

    Please have a look, I have simply added a calculated boolean field to flag the records that are missing required data. So this would give you the first part of your outcome; to get the other you can write a similar function that does the opposite (look for the N in FieldReq and Not IsNull in Data).
    If you need to create a list of what is missing you could leave the rsReq to loop to the end and build a string of field names then change the function from boolean to string to return that).
    EDIT:
    Forgot to mention that you have to make sure the entries in Field (bad name by the way) in FieldReq match the field names in Data; you had two that had a dot after (Product serial no. and Store No.).
    Cheers,
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  12. #12
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100
    This is perfect, I don't need a list. With the flag you created I can go into the system and populate the required data.

    I appreciate your time and help on this.

    Thank you

  13. #13
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  14. #14
    madpiet is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    151
    Seems that you should be able to have a table with a (tableName, ColumnName, Required) or just the list of (tableName, ColumnName) where the column in the table is required. Then You can just use that to look for NULLS. Just some stupid dynamic SQL. (you just set the .SQL property of your dummy query. Then you open it or append those violations to another table. Then all of your violations can be summarized in a query or report, and you know what to fix.
    Or if you were really creative, you could fix it. (Well, except if you haven't cleaned up the data, then you won't be able to save the changes because the existing records will violate the new validation rules)

    Not super hard, but you'll have to be at least reasonably comfortable writing VBA and some SQL.

    Or maybe I'm paying more attention to the hockey game than I am to this post... hmmm...

  15. #15
    madpiet is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    151
    Oh, there's a HUGE difference between columns being required on a record by record basis, and a column that is required. If a column/field requires a value, you'd just implement that in the table definition. Otherwise, you have to loop through the table a record at a time and check, which is going to be slow... but at least if you do it in code, it's faster than doing it by hand.

    Going back to watching hockey. =)

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

Similar Threads

  1. Replies: 7
    Last Post: 07-07-2014, 02:39 PM
  2. Replies: 2
    Last Post: 04-23-2012, 10:13 PM
  3. Replies: 4
    Last Post: 11-20-2011, 01:08 PM
  4. saving records without null or empty fields
    By amber mara in forum Access
    Replies: 1
    Last Post: 05-05-2010, 02:34 PM
  5. Hiding fields that contains empty records
    By sakthivels in forum Reports
    Replies: 4
    Last Post: 05-27-2009, 07: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