• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 11. Handling Access Error Codes > Fixing Errors After Trapping Them

Fixing Errors After Trapping Them

The preceding example handled the error by invoking a MsgBox with additional information. That is one option, but in certain cases, it might be possible to actually fix the error that is causing the problem—especially if it is impossible to code around the problem. The following code handles the error by creating the missing table that caused the error:

Private Sub Command18_Click() 
Dim td As TableDef, fld As Field, test As String 
Dim db As Database, rs As Recordset, ws As Workspace 
DoCmd.SetWarnings (False) 
DoCmd.RunSQL "Delete NonCurrent.[Table Name] " & _ 
"FROM NonCurrent;" 
DoCmd.SetWarnings (True) 
' This program creates a Table of Tables in your database 
  Set ws = CreateWorkspace("JetWorkspace", "admin", _ 
    "", dbUseJet) 
  Set db = ws.OpenDatabase("l:\office\Archive.mdb") 
  On Error GoTo FixIt 
  test = CurrentDb.TableDefs("NonCurrent").NAME 'see if table exists 
  Set rs = CurrentDb.OpenRecordset("NonCurrent") 'if it does open it 
  For Each td In db.TableDefs 
    If Left(td.NAME, 2) <> "MS" Then 
    With rs 
      .AddNew 
      ![Table Name] = td.NAME 
      .Update 
      .Bookmark = .LastModified 
    End With 
    End If 
  Next 
Set db = Nothing 
Set td = Nothing 
Me.Refresh 
Exit Sub 
FixIt: 
  Set td = CurrentDb.CreateTableDef("NonCurrent") 
  Set fld = td.CreateField("Table Name", dbText, 55) 
  td.Fields.Append fld 
  td.Fields.Refresh 
  db.TableDefs.Append td 
  db.TableDefs.Refresh 
  Resume Next 
End Sub 

					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint