[AccessD] SaveAsText, LoadFromText (was: URGENT!! A2K - form won't load; is code lost??)

MartyConnelly martyconnelly at shaw.ca
Sun Jul 25 13:30:48 CDT 2004


hmm.. I never thought of attaching  form code to a newsgroup message 
this way.
It would show the design quite easily

However you might find this useful as it uses same method.

 Here is the method to dump everything in an mdb to text files in temp
directory via saveastext--loadfromtext method into a new mdb and then
compile it.
  Code is very busy and concise..
I borrowed it from an Lyle Fairfield, access newsgroup posting
a long time ago. I have never seen GetObject used this way to startup
a second version of Access.
He was in charge of Dallas Fort Worth PC or Access UserGroup
at one time. Maybe Drew has come across him.

If you remove the kill txt statement, all your code will be available
from the temporary text files in temp directory.
This code is useful to try and recover clobbered or corrupted mdb files,
if you can open the mdb.

'===================================
'code provided by Lyle Fairfield
Sub BackUpThisTextFiles()
    Dim a(1 To 5) As String
    Dim bck As String
    Dim con As Container
    Dim doc As Document
    Dim objAccess As Access.Application
    Dim Ref As Reference
    Dim txt As String
    Dim zip As String
    Dim z As Long
    a(1) = "Tables"
    a(2) = "Forms"
    a(3) = "Reports"
    a(4) = "Scripts"
    a(5) = "Modules"
    bck = DBEngine(0)(0).Name
    bck = Left(bck, Len(bck) - 4) & "_BackUp.mdb"
    Do While Forms.Count > 0
        DoCmd.Close acForm, Forms(0).Name
    Loop
    Do While Reports.Count > 0
        DoCmd.Close acReport, Reports(0).Name
    Loop
    Do
        txt = Environ("temp") & "\" & CStr(Fix(Timer)) & ".txt"
    Loop Until Len(Dir$(txt)) = 0
    SaveAsText 6, "", bck
    Set objAccess = GetObject(bck)
    On Error Resume Next
    With objAccess
        For Each Ref In .References
            .References.Remove Ref
        Next Ref
    End With
    For Each Ref In References
        With Ref
         If Not .BuiltIn Then objAccess.References.AddFromFile Ref.FullPath
        End With
    Next Ref
    Set Ref = Nothing
    For z = 1 To 5
        Set con = DBEngine(0)(0).Containers(a(z))
        With con
            For Each doc In .Documents
                With doc
                    On Error Resume Next
                    SaveAsText z, .Name, txt
                    objAccess.Application.LoadFromText z, .Name, txt
                    On Error GoTo 0
                End With
            Next doc
        End With
    Next z
    Kill txt
    Set doc = Nothing
    Set con = Nothing
    With objAccess
        .DoCmd.OpenModule _
          (.DBEngine(0)(0).Containers("Modules").Documents(0).Name)
        .DoCmd.RunCommand acCmdCompileAndSaveAllModules
        .Application.Quit
    End With
    Set objAccess = Nothing
    Shell SysCmd(acSysCmdAccessDir) & "MsAccess.Exe " & _
                 """" & bck & """" & "/compact"
    On Error Resume Next
    zip = bck
    FileName zip
    zip = "d:\" & Format(Now(), "yyyymmddhhnnss") & zip
    FileCopy bck, zip
    MsgBox "All Done Creating " & bck
End Sub
'===================================
Private Sub FileName(ByRef sIn As String)
    Dim pos As Long
    Dim posForwardSlash As Long
    pos = InStr(sIn, "\")
    posForwardSlash = InStr(sIn, "/")
    If pos = 0 Then pos = posForwardSlash
    If pos Then
        sIn = Mid(sIn, pos + 1)
        FileName sIn
    End If
End Sub
'===================================



Gustav Brock wrote:

>Hi Marty
>
>  
>
>>you can try to use the undocumented SaveAsText and LoadFromText, may
>>help if an object is corrupted or otherwise behaving weirdly.
>>At the debug/immediate window type:
>>Application.SaveAsText acForm,"MyForm","c:\form.txt
>>or depending on Access version
>>SaveAsText acForm,"MyForm","c:\form.txt
>>You can then load the file into a new MDB.
>>Application.LoadFromText acForm,"MyForm","c:\from.txt"
>>    
>>
>
>This is very handy! Thanks for that tip.
>This way you can easily include a small form and other objects in a
>message like this without attachments.
>
>Have you more small gems like this?
>
>/gustav
>
>  
>

-- 
Marty Connelly
Victoria, B.C.
Canada






More information about the AccessD mailing list