Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    When you post something like "yeah, this exactly" - quote the post or refer to the responder. This forum does not alert a responder when he/she is composing a post and the thread has been updated. I left because I thought my interpretation was wrong and now I see I was not. Now you have two suggestions on how to handle this; 1 in post 6 and post 12.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  2. #17
    madpiet is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    154
    Quote Originally Posted by bastnpak View Post
    I have two tables. (Table A and Table B).

    I want to check if each of the fields on A are on B.

    my desired result is a message : -NameField, PhNum is on A but not B.
    - Address, CarMake is on both A and B
    I'm pretty sure that if you look up Error 3265 (field not found), you'll find an example of this...

    you'd loop through the fields in table A

    Public Sub ShowMissingFields(byval strTableA as string, strTableB as string)
    On Error Goto ErrHandler

    dim tdfA as tabledef
    dim fldA as field
    dim fldB as field

    set tdfA = CurrentDb.tabledefs(strTableA)
    for each fldA in tdfA.Fields
    set fldB = tdfB.Fields(fldA.Name)
    next fldA
    Exit Sub

    ErrHandler:
    If Err.Number = 3265 Then
    Msgbox "Field [" & fldA.Name & "] does not exist in " & tdfB.Name, vbokonly + vbinformation
    Resume Next '-- exit the handler and return to the next line in the code
    End If

    End Sub

    if the set statement fails, it means the field doesn't exist in table B, so you do something (like write the value to somewhere). Oh, and you need error handling on this, otherwise the error (3265) will cause your code to basically crash because of an unhandled exception. (this is aircode still... haven't tested it because I don't even know if that's the problem...)

  3. #18
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Looks viable to me. Now there are 3 proposed solutions. I'm usually not a fan of using *raised errors to figure something out* but in this case it seems simpler.
    * - an exception would be in dealing with commandbars or db properties such as bypass key.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #19
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    bastnpak, I tested your DCount code and it works for me.
    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.

  5. #20
    bastnpak is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2024
    Posts
    12
    I found my answer to the Dcount issue.
    1. Needed the [ ] around the table and field names
    2.Tried to use the equation to combine pieces of variable name
    -Instead predefined the variables and then just used the variables in the equation
    3.Realized did not need to put " " around variable name

    Code:
    'Before  - dcount field and table names used a more complex name set (had  " " around variable name')
             - missing [ ] around field and table name/variable
    
    
        TB_Cn = TB_C.Name             'table name ex: dc1_current
        TBF_B = TB_B.Fields(l).Name   'Field name ex: Component
        
        dc = DCount(" & TBF_B & ", " & TB_Cn & ")  ' attempt to use variables above in equation and add " manually
                    
    
    
    'After   - I predefined the dcount variables for field and table and added the [] in the predefined variables
    '        - Realized I did not need to add the  " " around variable name
        
        TB_Cn = "[" & TB_C.Name & "]"             'table name ex: [dc1_current]
        TBF_B = "[" & TB_B.Fields(l).Name & "]"   'Field name ex: [Component]
    
    
        dc = DCount(TBF_B, TB_Cn)
    then used the result of empty or a number to either add to error message or just go to next field
    Code:
            If IsEmpty(dc) = True Then            
                   Bf_message = Bf_message & vbCrLf & TBF_B
            End If
    'then moved to next field in loop and displayed bfmessage after loop finished

    Thank you for all the help

  6. #21
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    1. Only if you have spaces, which is never recommended.
    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

  7. #22
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Quote Originally Posted by Welshgasman View Post
    1. Only if you have spaces, which is never recommended.
    Or begin a field name with a number. Stumbled across that in somebody's posted code, not because it's something I do.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #23
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    you could try something like this

    Code:
    Dim dict1 As Scripting.Dictionary
    
    Sub checkTables()
    
        Dim db As DAO.Database
        Dim tdf As TableDef
        Dim tdf2 As TableDef
        Dim fld As Field
        
        Set db = CurrentDb
        Set dict1 = New Dictionary
    
        Set tdf = db.TableDefs("Table1")
        Set tdf2 = db.TableDefs("Table2")
    
        For Each fld In tdf.Fields
            dict1.Add fld.Name, fld.Name
        Next
    
        For Each fld In tdf2.Fields
            
            If dict1.Exists(fld.Name) Then Debug.Print fld.Name & " is in both tables"
            
        Next
    
        Set dict1 = Nothing
        Set db = Nothing
        Set tdf = Nothing
        Set tdf2 = Nothing
        
    End Sub
    You could also run it the other way to check flds in table1 to table2
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 7
    Last Post: 11-08-2021, 10:01 AM
  2. Check if a table field is null
    By Eranka in forum Access
    Replies: 1
    Last Post: 11-07-2019, 09:57 PM
  3. Replies: 9
    Last Post: 02-24-2017, 07:12 AM
  4. Replies: 3
    Last Post: 09-12-2015, 10:16 AM
  5. Check Table For Field
    By dandoescode in forum Programming
    Replies: 2
    Last Post: 05-22-2012, 01:58 PM

Tags for this Thread

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