Results 1 to 8 of 8
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,065

    Constraint "rsLK.RecordCount > 1" does NOT work for subset of tables

    Good morning:

    I have a quick question about altering a table WHERE condition of "rsLK.RecordCount > 1" is met.

    Specifically, in the attached example, I have 4 tables with the following record counts:
    1. [LK_AGE] = 1;
    2. [LK_CIV_MIL] = 2,


    3. [LK_CLOSE_DATE] = 1;
    4. [LK_EMERGENCY_ROOM] = 4

    For any table where "rsLK.RecordCount > 1" (e.g., [LK_CIV_MIL] and [LK_EMERGENCY_ROOM]), I then want to insert a new field [XYZ].

    However, once I run the VBA routine, the new field [XYZ] is inserted into three (3) tables incl. [LK_AGE].

    My question: Why does [LK_AGE] -- containing a single record -- by-pass the IF constraint and thus execute the following sub-routine?

    Code:
    If rsLK.RecordCount > 1 Then
    
    
        'Add [TARGET_STRING] (Text)
        Set fld = New DAO.Field
        fld.Name = "XYZ"                'Field to be added!
        fld.Type = dbText
        .Append fld
                            
        'Refresh fields collection
        .Refresh
        
        n = n + 1
    End If
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,046
    before you can know the actual recordcount you have to go to the last record (movelast) of the recordset.

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,545
    skydivetom

    You have the following line in your code:
    Code:
                        If i > 1 Then        'In this example, the IF constrating only applies to 2 tables ([LK_CIV_MIL] and [LK_EMERGENCY_ROOM]).                                                        'However, the field below is also added to [LK_AGE]... it shouldn't!
    You say that the field is added to [LK_AGE] but when I run the code it is added to [LK_CLOSE_DATE]
    This was after adding the line: rsLK.MoveLastimmediately before your test of the recordcount
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,065
    Bob:

    As suggested, I added the following line:

    Code:
                        rsLK.MoveLast                    
                        If rsLK.RecordCount > 1 Then
    Also, as you indicated, table CIV_MIL, EMERGENCY_ROOM, AND CLOSE_DATE receive the new field.

    The question remains... how do I need to modify the code so that *only* tables with more than one (1) record (i.e., CIV_MIL and EMERGENCY_ROOM) have the table altered?

    //

    Alternatively, I could go for the field count... that is, tables with 1 record only have 5 fields. Please note that LK_AGE should have field DATE_UPDATED as well. So, anything where table with field count > 5 should be altered. How would that work?

  5. #5
    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,743
    Tom,
    Just looked at you material.
    Here's an alternative you could try.

    Code:
    Public Sub AddFieldsJ()
              
        Dim db As DAO.Database
        Dim tdf As DAO.TableDef
        Dim fld As DAO.Field
        Dim n As Integer
        Set db = CurrentDb
        For Each tdf In db.TableDefs
            If tdf.Name Like "LK_*" Then
                If tdf.RecordCount > 1 Then
                    Debug.Print tdf.Name & " has " & tdf.RecordCount & " records"
                    'Add [TARGET_STRING] (Text)
                    With tdf.Fields
                        Set fld = New DAO.Field
                        fld.Name = "XYZ"                'Field to be added!
                        fld.Type = dbText
                        .Append fld
                        Debug.Print "Field 'XYZ' added to " & tdf.Name
                        n = n + 1
                        GoTo get_next_tdf
                    End With
                End If
            End If
    get_next_tdf:
        Next tdf
        Debug.Print "Finished " & n & " tables received new specified field(s)."
        MsgBox n & " tables received new specified field(s).", vbInformation, "Status"
        Set fld = Nothing
        Set db = Nothing
    End Sub
    When I test your data with this I get these messages.

    LK_CIV_MIL has 2 records
    Field 'XYZ' added to LK_CIV_MIL
    LK_EMERGENCY_ROOM has 4 records
    Field 'XYZ' added to LK_EMERGENCY_ROOM
    Finished 2 tables received new specified field(s).

    jack


    Update: It isn't rsLK that you want recordcounts from. You need to evaluate the individual tables whose names are in the rsLK recordset.

  6. #6
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,065
    Jack:

    Your solution is PERFECT! Many thanks for providing an alternative method. Only 2 tables get updated... yeah!

    Final question... is there a way to insert the new field "XYZ" into a specific position between existing fields? That is, instead of [XYZ] being the last field, I'd like to have it inserted between [TARGET_VALUE] and [DEFAULT_VALUE]. Can the field position be coded? If so, how?

  7. #7
    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,743
    Hi Tom,
    Adjusted as below. In your tables, the position of the new field is the same for the tables involved in your database.
    Code:
    Public Sub AddFieldsJ()
              
        Dim db As DAO.Database
        Dim tdf As DAO.TableDef
        Dim fld As DAO.Field
        Dim n As Integer
        Set db = CurrentDb
        For Each tdf In db.TableDefs
            If tdf.Name Like "LK_*" Then
                If tdf.RecordCount > 1 Then
                    Debug.Print tdf.Name & " has " & tdf.RecordCount & " records"
                    'Add [TARGET_STRING] (Text)
                    With tdf.Fields
                        Set fld = New DAO.Field
                        fld.Name = "XYZ"                'Field to be added!
                        fld.Type = dbText
                        fld.OrdinalPosition = 4 ' placement of the new field before the Append
                        .Append fld
                        Debug.Print "Field 'XYZ' added to " & tdf.Name & " in position 4"
                        n = n + 1
                        GoTo get_next_tdf
                    End With
                End If
            End If
    get_next_tdf:
        Next tdf
        Debug.Print "Finished " & n & " tables received new specified field(s)."
        MsgBox n & " tables received new specified field(s).", vbInformation, "Status"
        Set fld = Nothing
        Set db = Nothing
    End Sub
    Debug.print results
    LK_CIV_MIL has 2 records
    Field 'XYZ' added to LK_CIV_MIL in position 4
    LK_EMERGENCY_ROOM has 4 records
    Field 'XYZ' added to LK_EMERGENCY_ROOM in position 4
    Finished 2 tables received new specified field(s).

    Click image for larger version. 

Name:	TomOrdinalPosition.png 
Views:	14 
Size:	19.1 KB 
ID:	48288

  8. #8
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,065
    Jack -- your solution is PERFECT!! Thank you so very much for the assistance.

    Cheers,
    Tom

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

Similar Threads

  1. Simple table relationships ("faces" to "spaces" to "chairs")
    By skydivetom in forum Database Design
    Replies: 36
    Last Post: 07-20-2019, 01:49 PM
  2. Replies: 3
    Last Post: 09-21-2017, 05:40 AM
  3. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  4. Replies: 2
    Last Post: 09-29-2012, 11:22 PM
  5. Replies: 3
    Last Post: 10-20-2009, 06:56 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