我有个很损的办法: SQL="Select * from ttttttttttttttttttt" “ttttttttttttttttttt“是一个不存在的表,然后执行这个SQL会返回一个特定的 Err.ID 然后比较Err.ID就知道你不确定的表是不是存在了,呵呵
Sorry: 是Err.Number 不是Err.ID
我没有用过ACCESS,不知道ACCESS有没有像SQL一样在SYSOBJECTS中保存数据对象
如果用DAO就很方便了。dim db as database,i as integerset db=dbengien.opendatabase("yourdatabase") for i =0 to db.tabeldefs.count-1 debug.print db.tabeldefs(i).name nextdb.close这样就可以获取数据库中所有的表名了。
参考,稍改下就好了 Function GetTables(cnn As ADODB.Connection) As Boolean 'Purpose : Get table names from the Connection given 'Input : cnn, ther ADODB.Connection 'Output : TRUE/FALSE 'On Error GoTo GetTables_ErrorHandler Screen.MousePointer = 11
Dim rstSchema As ADODB.Recordset cboTablesName.Clear
Set rstSchema = cnn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "Table")) Do Until rstSchema.EOF If StrComp(Left(rstSchema!TABLE_NAME, 4), "MSys", vbTextCompare) <> 0 Then cboTablesName.AddItem rstSchema!TABLE_NAME End If rstSchema.MoveNext Loop
rstSchema.Close Set rstSchema = Nothing
Screen.MousePointer = 0ErrorHandler: Exit Function GetTables_ErrorHandler: Screen.MousePointer = 0 ShowErrMsg ("GetTables") Resume ErrorHandlerEnd Function
SQL="Select * from ttttttttttttttttttt"
“ttttttttttttttttttt“是一个不存在的表,然后执行这个SQL会返回一个特定的
Err.ID
然后比较Err.ID就知道你不确定的表是不是存在了,呵呵
是Err.Number 不是Err.ID
for i =0 to db.tabeldefs.count-1
debug.print db.tabeldefs(i).name
nextdb.close这样就可以获取数据库中所有的表名了。
Function GetTables(cnn As ADODB.Connection) As Boolean
'Purpose : Get table names from the Connection given
'Input : cnn, ther ADODB.Connection
'Output : TRUE/FALSE
'On Error GoTo GetTables_ErrorHandler
Screen.MousePointer = 11
Dim rstSchema As ADODB.Recordset
cboTablesName.Clear
Set rstSchema = cnn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "Table"))
Do Until rstSchema.EOF
If StrComp(Left(rstSchema!TABLE_NAME, 4), "MSys", vbTextCompare) <> 0 Then
cboTablesName.AddItem rstSchema!TABLE_NAME
End If
rstSchema.MoveNext
Loop
rstSchema.Close
Set rstSchema = Nothing
Screen.MousePointer = 0ErrorHandler:
Exit Function
GetTables_ErrorHandler:
Screen.MousePointer = 0
ShowErrMsg ("GetTables")
Resume ErrorHandlerEnd Function