Results 1 to 3 of 3
  1. #1
    quicova is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    53

    Get table names from another database file VB code

    Hi everyone.

    I'm trying to find all the table names from another database file using VB code.
    this is what i have so far:

    Code:
    Function getFileNameOpen(path) As String
    
    
        Dim f    As Object
        Dim varFile As Variant
        
        Set f = Application.FileDialog(3)
        
        With f
        
            f.AllowMultiSelect = True
       
            f.Filters.Clear
            f.Filters.Add "Access Databases", "*.accdb"
            f.Filters.Add "All Files", "*.*"
            
            If f.show = -1 Then
                For Each varFile In f.SelectedItems
                file = varFile
                Next varFile
            End If
        End With
        
        path = file
       
    End Function
    this function will show a browser window so that the user can select a file, and returns the file path.

    Code:
    Function OpenFile()
        
        Call getFileNameOpen(path)
        
        Dim db As Database
        Dim td As TableDef
        
        Database = path
        
        Set db = CurrentDb()
        For Each td In db.TableDefs
            Debug.Print td.name
        Next td
        
    End Function
    them I have this function. I need to get all the table names from the selected access file into a list so I can do a for loop after.

    I can't figure out how to set the CurrentDb() to the path one.
    This code gives me the table names and some other names of stuff which I don't know what they are. But for the current database not the one in the path.
    Can anyone help me out here



    Thanks so much

  2. #2
    quicova is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    53
    Got it, answered my own question

    maybe there would be a better solution but I'm new to this VB coding.



    Code:
    Function OpenFile()
        
        Call getFileNameOpen(path)
        
        Dim db As Database
        Dim td As TableDef
        Dim WS As Workspace
        Dim i As Integer
        Dim ListOfNames()
        
        i = 0
        ReDim ListOfNames(i)
        
        Set WS = CreateWorkspace("DBtoReadTables", "admin", "", dbUseJet)
        Set db = WS.OpenDatabase(path, True)
    
    
        For Each td In db.TableDefs
            ListOfNames(i) = td.name
            i = i + 1
            ReDim Preserve ListOfNames(i)
        Next td
    
    
        db.Close
        Set db = Nothing
        Set WS = Nothing
        
        Dim tbname As Variant
        
        For Each tbname In ListOfNames
            Debug.Print tbname
        Next
        
        
    End Function

  3. #3
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Good enough. Thanks for posting your solution. Please mark thread solved. Top of page, under thread tools.

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

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2012, 06:32 PM
  2. count number of file names within a folder containing
    By dumbledown in forum Programming
    Replies: 2
    Last Post: 04-24-2012, 02:55 AM
  3. File Names in "Open Recent Database" list
    By Ron.Sul in forum Access
    Replies: 3
    Last Post: 12-29-2011, 10:06 AM
  4. Importing file names into a table?
    By davetheant in forum Forms
    Replies: 6
    Last Post: 06-10-2011, 10:37 PM
  5. Import File Names
    By cassidym in forum Import/Export Data
    Replies: 3
    Last Post: 01-11-2011, 03:19 PM

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