Results 1 to 12 of 12
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Write Table Info To Text

    My module is almost complete, but I am getting a run time error of - Application Defined or Object Defined Error on this line of code


    Code:
    If objRegExp.test(objFile) Then
    I have these references added to my project
    Code:
    Visual Basic For Application
    Microsoft Access 15.0 Object Library
    OLE Automation
    Office 15.0 Access Database Engine Object
    Microsoft Scripting Runtime
    And this is full syntax. What do I need to alter in order for my code to execute as I so desire?
    Code:
    Sub ScanTablesWriteDataToText()
        Dim Fileout As Object
        Dim fso As Object
        Dim objFSO As Object
        Dim accapp As Access.Application
        Dim db As DAO.Database
        Dim tdf As DAO.TableDef
        Set objFSO = CreateObject("Scripting.FileSystemObject")
     
        Dim objRegExp As Object
        Set objRegExp = CreateObject("VBScript.RegExp")
        objRegExp.pattern = "*.accdb"
        objRegExp.IgnoreCase = True
     
        Dim colFiles As Collection
        Set colFiles = New Collection
     
        RecursiveFileSearch "C:\Test\", objRegExp, colFiles, objFSO
     
        For Each f In colFiles
            Set accapp = New Access.Application
            accapp.OpenCurrentDatabase (filename)
            accapp.Visible = False
            Set db = accapp.CurrentDb
            For Each tdf In db.TableDefs
              If Not (tdf.name Like "MSys*") Then
                    Set fso = CreateObject("Scripting.FileSystemObject")
                    Set Fileout = fso.OpenTextFile("C:\datafile.txt", 8, True)
                    Fileout.Write CStr(filename) & "," & name & "," & connect & vbCrLf
                    Fileout.Close
              End If
            Next
            Set tdf = Nothing
            Set db = Nothing
        Next
        Set objFSO = Nothing
        Set objRegExp = Nothing
     
    End Sub
    Sub RecursiveFileSearch(ByVal targetFolder As String, ByRef objRegExp As Object, _
                        ByRef matchedFiles As Collection, ByRef objFSO As Object)
     
        Dim objFolder As Object
        Dim objFile As Object
        Dim objSubFolders As Object
        Set objFolder = objFSO.GetFolder(targetFolder)
        For Each objFile In objFolder.files
            If objRegExp.test(objFile) Then
                matchedFiles.Add (objFile)
            End If
        Next
        Set objSubFolders = objFolder.Subfolders
        For Each objSubfolder In objSubFolders
            RecursiveFileSearch objSubfolder, objRegExp, matchedFiles, objFSO
        Next
        Set objFolder = Nothing
        Set objFile = Nothing
        Set objSubFolders = Nothing
     
    End Sub

  2. #2
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I'd say it's because there's no reference to DAO in your list. Usually, the offending part is highlighted when there is a compile error, but you have not indicated what that is.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by Micron View Post
    I'd say it's because there's no reference to DAO in your list. Usually, the offending part is highlighted when there is a compile error, but you have not indicated what that is.
    it is a run time error not a compile time. And this is the offending line
    Code:
    If objRegExp.test(objFile) Then
    Is it a reference to
    Microsoft DAO 3.6 Object Library

    If that is the case, anytime I try to add this reference i get an error of:
    Error in loading dll

  4. #4
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Sorry, I missed that. The only other suggestion I have is to try Set objRegExp = New RegExp
    as opposed to
    Set objRegExp = CreateObject("VBScript.RegExp")

    The lack of capitalization on the method (such as .pattern) suggests the object has not been formed, thus giving the defined error message.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    If I comment out
    Code:
    Set objRegExp = CreateObject("VBScript.RegExp")
    


    and add
    Code:
    Set objRegExp = New RegExp 


    I get a debug error of
    User defined type not defined
    on the line

    Code:
    New RegExp
    

  6. #6
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    I have worked through this error, by adding (I know this is best practice)
    Code:
    option explicit
    to the top of my method which pointed out on this line
    Code:
    accapp.OpenCurrentDatabase (filename)
    I had not declared the variable filename, so I changed this to

    Code:
    accapp.OpenCurrentDatabase (f)
    However, now when the data is written to a text file I get Chinese symbols instead of American English.

    Is there a caveat to using
    Code:
    Fileout.Write CStr(filename) & "," & name & "," & connect & vbCrLf
    that puts the text in that language?

  7. #7
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    You know, Option Explicit was the first thing that I noticed was missing, but to be honest, I assumed anybody who could work with scripting languages such as this would not make that oversight. I just figured it was at the top of the module along with the Option Compare statement.

    As for the Chinese characters, I doubt that is the normal result - wouldn't make sense. Besides, this character substitution problem has been covered in this forum at least once before. Search the forum for "chinese characters" and I think you'll find the general consensus is that it is due to db corruption. I'd try a compact/repair before coding anything else.

  8. #8
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    It was honestly pure oversight on my part for excluding it. In practice I do always add that to the top.

    As for the Chinese characters, I created a blank new database and ran the procedure again with the same result.

    I'll search the forums and see if anyone has relevant information for me.

    Thanks for your help!

  9. #9
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is what I have found:

    Yes , the top two lines in EVERY module should be:
    Code:
    Option Compare Database
    Option Explicit
    I found 3 variables not declared: "FileName", "Connect" and "f"
    I would guess that the first two would be strings.
    The "f" should be declared as a Variant.
    '----------------------------

    For this line in the code, delete the "*":
    Code:
    objRegExp.pattern = "*.accdb"
    Use
    Code:
    objRegExp.pattern = ".accdb"
    The Recursive File Search subroutine now works for me.
    '----------------------------

    Next is the code for the outer For...Next loop.
    If you changed "Filename" to "f", "f" contains the full path and file name of an Access dB, so you can open the dB.
    '----------------------------

    Now the inner For...Next loop.
    If you changed "Filename" to "f"
    Code:
    Fileout.Write CStr(f) & "," & Name & "," & Connect & vbCrLf
    What are you converting to a string to print? Remember, "f" contains the full path and filename of the dB.
    So you would NOT be printing the contents (data) of the dB, you would be printing the actual dB file bits and bytes!!

    Since the inner For...Next loop is dealing with table defs, you need to open each table and loop through the records and fields, printing the data.
    Don't know what to recommend to you - not sure what you are trying to print.
    '----------------------------

    Lastly, these two lines should NOT be inside the For...Next loops.
    Code:
        Set tdf = Nothing
        Set db = Nothing
    You do not want to destroy the two variables while you are still using them.
    The last lines should be
    Code:
    <snip>
            Next
        Next
    
        Set tdf = Nothing
        Set db = Nothing
        Set colFiles = Nothing  '<<--I added this line
    
        Set objFSO = Nothing
        Set objRegExp = Nothing
    
    End Sub
    '----------------------------

    I am still wondering what "Name" and "Connect" are in the code:
    Code:
    Fileout.Write CStr(FileName) & "," & Name & "," & Connect & vbCrLf

    Good luck with your project.......

  10. #10
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    This line
    Code:
    Fileout.Write CStr(FileName) & "," & Name & "," & Connect & vbCrLf
    Should actually be
    Code:
    Fileout.Write CStr(FileName) & "," & tdf.Name & "," & tdf.Connect & vbCrLf
    What I am after is the name of every database that has a linked table (full database name) the table name, and the connection string for the linked table.

    May be a simpler way of achieving such, but w/ exception of my Chinese characters my syntax is working as expected.

  11. #11
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    @ssanfu, some great detective work there. I should refrain from posting when imbibing during and concentrating on the baseball game (Cubs - Indians still tied)!
    @jo15765, suggest you set 'require variable declaration' as default so you don't have to worry about it

    In case you're interested, I'm a Blue Jays fan and in this 3rd world series game, the home plate umpire is atrocious.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Maybe this??
    Code:
    Sub ScanTablesWriteDataToText()
        Dim Fileout As Object
        Dim fso As Object
        Dim objFSO As Object
        Dim accapp As Access.Application
        Dim db As DAO.Database
        Dim tdf As DAO.TableDef
        Dim objRegExp As Object
        Dim colFiles As Collection
    
        '-------------------------------
        'this was not declared
        Dim f As Variant
        '-------------------------------
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        Set objRegExp = CreateObject("VBScript.RegExp")
        objRegExp.Pattern = ".accdb"   ' <-- I deleted the "*"
        objRegExp.IgnoreCase = True
    
        Set colFiles = New Collection
    
        RecursiveFileSearch "C:\Test\", objRegExp, colFiles, objFSO
    
        'create a text file
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set Fileout = fso.OpenTextFile("C:\datafile.txt", 8, True)
    
        'loop through the colFiles collection
        For Each f In colFiles
            Set accapp = New Access.Application
            accapp.OpenCurrentDatabase (f)
            accapp.Visible = False
            Set db = accapp.CurrentDb
            For Each tdf In db.TableDefs
                If Not (tdf.Name Like "MSys*") Then
                    'test for a linked table
                    If Len(Trim(tdf.Properties("Connect") & "")) > 1 Then
                        Fileout.Write CStr(f) & "," & tdf.Name & "," & tdf.Connect & vbCrLf
                    End If
                End If
            Next
        Next
        'close text file
        Fileout.Close
    
        'clean up
        Set tdf = Nothing
        Set db = Nothing
        Set colFiles = Nothing
    
        Set objFSO = Nothing
        Set objRegExp = Nothing
    
    End Sub
    
    Sub RecursiveFileSearch(ByVal targetFolder As String, ByRef objRegExp As Object, _
                            ByRef matchedFiles As Collection, ByRef objFSO As Object)
    
        Dim objFolder As Object
        Dim objFile As Object
        Dim objSubFolders As Object
        Dim objSubFolder As Variant
        
        Set objFolder = objFSO.GetFolder(targetFolder)
        For Each objFile In objFolder.Files
            If objRegExp.test(objFile) Then
                matchedFiles.Add (objFile)
            End If
        Next
        Set objSubFolders = objFolder.Subfolders
        For Each objSubFolder In objSubFolders
            RecursiveFileSearch objSubFolder, objRegExp, matchedFiles, objFSO
        Next
        Set objFolder = Nothing
        Set objFile = Nothing
        Set objSubFolders = Nothing
    
    End Sub

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

Similar Threads

  1. Replies: 3
    Last Post: 10-31-2014, 01:57 PM
  2. Replies: 7
    Last Post: 10-25-2014, 10:41 AM
  3. How do I write to a table from text box
    By noweyout in forum Forms
    Replies: 5
    Last Post: 03-17-2011, 07:14 PM
  4. Replies: 3
    Last Post: 01-31-2011, 11:47 AM
  5. Write text to text box on a form
    By DKY in forum Programming
    Replies: 0
    Last Post: 10-08-2008, 11:34 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