Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trapping Runtime Errors

Status
Not open for further replies.

Quehay

Programmer
Feb 6, 2000
804
US
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) <> &quot;Msys&quot; 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 &quot;The table already exists&quot;
Resume Next
Case Else
MsgBox Err.Number & &quot;: &quot; & Err.Description
Resume Exit_Err_ShowTableLinks
End Select



End Sub[/tt]
 
Thanks to Rohdem and Jim Lunde I think I've solved this.

Rohdem offered Jim's

FAQ181-36

to check for object's existence in db container.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top