I'm using the following to get create & fill a table with the the table links (I use a similar thing to get a list of fields and their datatypes).
The question:
How to trap Access Runtime errors in a module?
I want to create a table with DDL if it doesn't exist or DROP it if it does and start from scratch (or DELETE * if tables exists and CREATE table if it doesn't).
In any case there's going to be a Runtime error generated by either trying to DROP/DELETE * FROM a table that doesn't exist or CREATE a table that already exists.
Code below; TIA
Jeff
[tt]
Private Sub ShowTableLinks()
On Error GoTo Err_ShowTableLinks
Dim Db As DAO.Database
Dim Rst As DAO.Recordset
Dim Tdf As DAO.TableDef
Dim Fld As DAO.Field
Dim strDbName As String, strTblName As String, strFldName As String
Dim strSQL As String
'Avoid run-time error generated from existing copy of table
strSQL = "DROP TABLE TablesFields;"
DoCmd.RunSQL strSQL
strSQL = "CREATE TABLE TableLinks"
strSQL = strSQL & " (TableName TEXT(30), ConnectString TEXT(50);"
DoCmd.RunSQL strSQL
Set Db = CurrentDb()
Set Rst = Db.OpenRecordset("TableLinks"
With Rst
For Each Tdf In Db.TableDefs
If Left(Tdf.Name, 4) <> "Msys" Then
.AddNew
!TableName = .Name
!ConnectString = .Connection
.Update
End If
Next
.Close
End With
Exit_Err_ShowTableLinks:
Set Tdf = Nothing
Set Rst = Nothing
Set Db = Nothing
Exit Sub
Err_ShowTableLinks:
Select Case Err.Number
Case 3371
Resume Next
Case 3010
MsgBox "The table already exists"
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Err_ShowTableLinks
End Select
End Sub[/tt]
The question:
How to trap Access Runtime errors in a module?
I want to create a table with DDL if it doesn't exist or DROP it if it does and start from scratch (or DELETE * if tables exists and CREATE table if it doesn't).
In any case there's going to be a Runtime error generated by either trying to DROP/DELETE * FROM a table that doesn't exist or CREATE a table that already exists.
Code below; TIA
Jeff
[tt]
Private Sub ShowTableLinks()
On Error GoTo Err_ShowTableLinks
Dim Db As DAO.Database
Dim Rst As DAO.Recordset
Dim Tdf As DAO.TableDef
Dim Fld As DAO.Field
Dim strDbName As String, strTblName As String, strFldName As String
Dim strSQL As String
'Avoid run-time error generated from existing copy of table
strSQL = "DROP TABLE TablesFields;"
DoCmd.RunSQL strSQL
strSQL = "CREATE TABLE TableLinks"
strSQL = strSQL & " (TableName TEXT(30), ConnectString TEXT(50);"
DoCmd.RunSQL strSQL
Set Db = CurrentDb()
Set Rst = Db.OpenRecordset("TableLinks"
With Rst
For Each Tdf In Db.TableDefs
If Left(Tdf.Name, 4) <> "Msys" Then
.AddNew
!TableName = .Name
!ConnectString = .Connection
.Update
End If
Next
.Close
End With
Exit_Err_ShowTableLinks:
Set Tdf = Nothing
Set Rst = Nothing
Set Db = Nothing
Exit Sub
Err_ShowTableLinks:
Select Case Err.Number
Case 3371
Resume Next
Case 3010
MsgBox "The table already exists"
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Err_ShowTableLinks
End Select
End Sub[/tt]