Results 1 to 8 of 8
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365

    TableDefs ' Sub 0r Function not defined

    I picked this idea up on line

    If TableDefs("Tracks").fieldDefs(This(0).Name).Name > "" Then

    The idea being to see if field this(0).name exists in table Tracks.
    I've seen examples where you look through every field name for a match but if the above can work it seems more efficient?
    The example didn't show how TableDefs was defined.
    Is it possible? Thanks.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    You should pay attention to the IntelliSense - it’s fields, not fielddefs.

    and perhaps you need currentdb.tabledefs

  3. #3
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365
    Not quite getting anywhere with that. Should this work

    Dim MyField As DAO.Field
    For Each MyField In CurrentDb.TableDefs("Tracks").Fields
    Debug.Print MyField.Name
    Next
    because I'm getting error 'Object invalid or no longer set.

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    You could use error handling to do it.

    Code:
    Function FieldExists(vTable As String, vField As String) As Boolean
        Dim x As String
    
        On Error GoTo FieldExists_Error
        
        x = CurrentDb.TableDefs(vTable).Fields(vField).Name
    
        FieldExists = True
    
        On Error GoTo 0
        Exit Function
    
    FieldExists_Error:
    
        If Err.Number = 3265 Then
            FieldExists = False
            Exit Function
    
        End If
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure FieldExists, line " & Erl & "."
    
    End Function
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365
    Thanks I have that all working nicely. But the next step has caught me
    ? this!Dat
    But Dat is a variable folding the field name. I was sure
    ? this.Controls(Dat).Value would work... but no. Controls isn't jn IntelliSense, and ! doesn't work either,

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    what is 'this'? name of your form? i

    and what is Dat? name of your control?

  7. #7
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Middlemarch View Post
    Thanks I have that all working nicely. But the next step has caught me
    ? this!Dat
    But Dat is a variable folding the field name. I was sure
    ? this.Controls(Dat).Value would work... but no. Controls isn't jn IntelliSense, and ! doesn't work either,
    try this
    Code:
    this.Fields(Dat) 'This(Dat) might also work

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I'll call out what I see as the problem here
    For Each MyField In CurrentDb.TableDefs("Tracks").Fields

    because otherwise you're doomed to repeat since you don't seem to grasp what you're saying. That's like saying for each Field IN myTable.Fields
    The fields in myTable don't have fields in them.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 14
    Last Post: 04-01-2020, 06:03 AM
  2. Compile Error: Sub/Function not defined
    By TheKillerMonkey in forum Programming
    Replies: 12
    Last Post: 04-18-2016, 01:48 PM
  3. User Defined IIf Function
    By EddieN1 in forum SQL Server
    Replies: 6
    Last Post: 09-22-2015, 11:38 AM
  4. How to use User Defined Function in VBA
    By sbehera in forum Programming
    Replies: 7
    Last Post: 05-12-2014, 07:38 AM
  5. Sub or Function not defined
    By waqas in forum Programming
    Replies: 2
    Last Post: 02-06-2013, 10:07 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