我的访问数据库中有一些链接表,这些表用于生成表查询,其中从中获取员工数据。有一个连接检查在最开始运行,但是当没有连接时,表被删除。我可以在任何地方添加一行代码,如果没有连接,可以告诉DB不要删除表吗?
答案 0 :(得分:0)
DAO对象库提供了一个TableDefs对象,它具有您要查找的属性(称为" Connect")。使用此方法,您可以扫描数据库以确定表是本地链接还是ODBC连接。
以下是我在很久以前写过的一段代码" document"使用DAO模型在Excel电子表格中的数据库属性。我已经包含了专门引用表属性的部分(因此理解我已经排除了数据库查找和创建Excel电子表格)。希望这能让您了解如何使用此属性创建您正在寻找的逻辑。
Dim oDb As DAO.Database
Dim oTbl As DAO.TableDef
Dim oTbls As DAO.TableDefs
'... A lot of code to open the appropriate database, create an excel spreadsheet
'and begin pasting DAO attributes into a template...
Dim oWkbk As Excel.Workbook
With oWkbk.Sheets("Tables")
.Select
.Range("B1").Value = TrimToChar(DbName, "\", True, True)
.Range("B1").Font.Bold = True
.Range("B1").Font.Size = 14
.Range("B3").Value = "Name"
.Range("C3").Value = "Linked"
.Range("D3").Value = "Linked DB"
.Range("E3").Value = "Date Created"
.Range("F3").Value = "Last Updated"
.Range("G3").Value = "Record Count"
.rows("3:3").Font.Bold = True
.rows("3:3").HorizontalAlignment = xlCenter
End With
Set oTbls = oDb.TableDefs
For Each oTbl In oTbls
With oWkbk.Sheets("Tables")
.Range("B" & i).Value = oTbl.Name
.Range("E" & i).Value = oTbl.DateCreated
.Range("F" & i).Value = oTbl.LastUpdated
End With
****'HERES THE PART TO PAY ATTENTION TO:****
If oTbl.Connect = "" Then
With oWkbk.Sheets("Tables")
.Range("C" & i).Value = "No"
.Range("G" & i).Value = oTbl.RecordCount
End With
ElseIf Left(oTbl.Connect, 4) = "ODBC" Then
With oWkbk.Sheets("Tables")
.Range("C" & i).Value = "ODBC"
End With
********************************************
Else
With oWkbk.Sheets("Tables")
.Range("C" & i).Value = "Yes"
.Range("D" & i).Value = TrimToChar(oTbl.Connect, "\", True, True)
End With
End If
Next oTbl