Question : How can tables be detected in Access using VBA?

Hi Experts,

How can tables be detected in Access using VBA?

I have this code to link tables but I want to detect before doing this for each table. How can this be done?

Thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
Option Compare Database
 
Public Sub Reconnect_Database_Table_Links()
 
 
Dim db As DAO.Database
Dim tbd As DAO.TableDef
 
Dim PathIs As String
 
'Detect path
PathIs = Application.CurrentProject.Path
 
Set db = CurrentDb
 
'FTT_BUDGETED
Set tbd = db.CreateTableDef(Name:="FTT_BUDGETED")
tbd.SourceTableName = "FTT_BUDGETED"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_Budget.mdb"
db.TableDefs.Append tbd
 
'FTT_ACTUALS
Set tbd = db.CreateTableDef(Name:="FTT_ACTUALS")
tbd.SourceTableName = "FTT_ACTUALS"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_FTT_Actuals.mdb"
db.TableDefs.Append tbd
 
 
'LOCATIONS
Set tbd = db.CreateTableDef(Name:="LOCATIONS")
tbd.SourceTableName = "LOCATIONS"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_Manager.mdb"
db.TableDefs.Append tbd
 
'TRANSPORT_MODES
Set tbd = db.CreateTableDef(Name:="TRANSPORT_MODES")
tbd.SourceTableName = "TRANSPORT_MODES"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_Manager.mdb"
db.TableDefs.Append tbd
 
 
'LANES
Set tbd = db.CreateTableDef(Name:="LANES")
tbd.SourceTableName = "LANES"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_FTT_Actuals.mdb"
db.TableDefs.Append tbd
 
'LANE_TRANSPORT_MODES
Set tbd = db.CreateTableDef(Name:="LANE_TRANSPORT_MODES")
tbd.SourceTableName = "LANE_TRANSPORT_MODES"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_FTT_Actuals.mdb"
db.TableDefs.Append tbd
 
 
 
'USERS
Set tbd = db.CreateTableDef(Name:="USERS")
tbd.SourceTableName = "USERS"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_Manager.mdb"
db.TableDefs.Append tbd
 
'CAL_YEAR
Set tbd = db.CreateTableDef(Name:="CAL_YEAR")
tbd.SourceTableName = "CAL_YEAR"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_Manager.mdb"
db.TableDefs.Append tbd
 
 
'CAL_MONTH
Set tbd = db.CreateTableDef(Name:="CAL_MONTH")
tbd.SourceTableName = "CAL_MONTH"
tbd.Connect = ";DATABASE=" & PathIs & "\SMC_Logistics_Manager.mdb"
db.TableDefs.Append tbd
 
 
 
 
End Sub
Open in New Window Select All

Answer : How can tables be detected in Access using VBA?

You can attempt to open the table and trap the error which indicates it does not exist"

On Error Resume Nex
Currentdb.OpenRecordset("YourTableName")

If err.Number = 3078 Then
   ' Table does not exist
Else
  ' whatever
End If

mx
Random Solutions  
 
programming4us programming4us