Results 1 to 4 of 4
  1. #1
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100

    How to check table data from 2nd table requirements?

    Hi again - this question is related to the post found here. I am not looking for the answer, I would prefer an example of another block of code or maybe an article to read (I'm really trying to do this on my own as much as possible). If I manage to figure it out I will post the code so people can refer to it in the future if needed, if I can't, then I will ask for more help.



    The purpose of the code below is:


    • Go through the Data and Field_Req table to identify records that are missing required data or have data but shouldn't. I run the code it works exactly as expected.


    My database has 3 tables.


    • Field_Req - This table identifies which fields in the Data table require/don't require data.
    • Data - This table has about 10k records that gets updated daily (100 columns).
    • Results - This table holds the results of which fields have data but shouldn't and vice-versa.


    The Field_Req table has 10 rows with over a hundred columns with a "Y" or "N" for that particular field. One of those columns is Customer_Type (new) which has Customer_1, Customer_2 all the way up-to Customer_10. All have different requirements.

    The Data table has those 10k records mentioned above with a field Customer_Type. I would now like for the code below to check the Field_Req table for that 'Customer_Type' to see which fields are required. I can't seem to find good examples, that's all I would like.


    Code:
    Public Sub CheckFields()    Dim rsFieldReq As DAO.Recordset    Dim rsFieldReq As DAO.Recordset    Dim rsData As DAO.Recordset
        Dim rsResults As DAO.Recordset
        Dim fld As DAO.Field
        Dim strFieldName As String
        Dim strAct As String
        
        ' Open recordsets for Field_Req, Data, and Results tables
        Set rsFieldReq = CurrentDb.OpenRecordset("Field_Req")
        Set rsData = CurrentDb.OpenRecordset("Data")
        Set rsResults = CurrentDb.OpenRecordset("Results")
        
        ' Loop through each record in Data table
        Do While Not rsData.EOF
            ' Loop through each field in Field_Req table
            For Each fld In rsFieldReq.Fields
             ' Check if field is required (Y) or not (N)
                If fld.Value = "Y" Then
                 ' Field is required, check if it is null in Data table
                 If IsNull(rsData(fld.Name)) Then
                    ' Field is null, add error to Results table
                    strFieldName = fld.Name
                    strAct = rsData("act num").Value
                    rsResults.AddNew
                    rsResults("act num").Value = strAct
                    rsResults("FieldName").Value = strFieldName
                    rsResults("ErrorType").Value = "Y-field is null"
                    rsResults.Update
                End If
            ElseIf fld.Value = "N" Then
                ' Field is not required, check if it is not null in Data table
                If Not IsNull(rsData(fld.Name)) Then
                    ' Field is not null, add error to Results table
                    strFieldName = fld.Name
                    strAct = rsData("act num").Value
                    rsResults.AddNew
                    rsResults("act num").Value = strAct
                    rsResults("FieldName").Value = strFieldName
                    rsResults("ErrorType").Value = "N-field is not null"
                    rsResults.Update
                End If
            End If
        Next fld
        
         rsData.MoveNext ' Move to the next record in Data table
        Loop
        
        ' Close recordsets
        rsFieldReq.Close
        rsData.Close
        rsResults.Close
        Set rsFieldReq = Nothing
        Set rsData = Nothing
        Set rsResults = Nothing End Sub


  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Two options:

    1. inside rsData loop, open rsFieldReq filtered to just record associated with CustomerType of rsData record - open and close rsFieldReq within loop

    2. use FindFirst method on rsFieldReq

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    The Field_Req table has 10 rows with over a hundred columns with a "Y" or "N" for that particular field.
    That does not sound right at all?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100
    I added this:

    Code:
    Set rsFieldReq = CurrentDb.OpenRecordset("SELECT * FROM rsFieldReq WHERE Data_Element_Name = '" & rsrsData ("Product_Name").Value & "'")
    right after the 1st loop and it works. Took a lot of trial and error and searching various forums for an example I can use.

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

Similar Threads

  1. Replies: 5
    Last Post: 11-03-2022, 05:37 PM
  2. Replies: 4
    Last Post: 06-16-2014, 08:31 AM
  3. Replies: 15
    Last Post: 11-18-2013, 10:49 PM
  4. Replies: 6
    Last Post: 09-28-2012, 02:49 PM
  5. check table, if exist then update another table
    By JeroenMioch in forum Programming
    Replies: 6
    Last Post: 07-06-2012, 09:12 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