设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2086|回复: 5
打印 上一主题 下一主题

[与其它组件] 如何判断一个表是否存在?

[复制链接]
跳转到指定楼层
1#
发表于 2006-8-2 19:18:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如何判断一个表是否存在? 比如我库中有表:aa,但我经常对它删除操作,所以请问一下如何判断一个表aa是否存在?[em01][em01][em01]
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2006-8-2 19:39:00 | 只看该作者
检索表名称:MSysObjects中是否有  name="aa" and ParentId=251658241  即可.
3#
 楼主| 发表于 2006-8-2 19:53:00 | 只看该作者
我想来想去好像只有这种办法比较简便


if isnull(DLookup("name", "MSysObjects", "name='表名'"))) then

真就是无此表


不知道还有别的更简单的吗?[em02][em02][em02]
4#
发表于 2006-8-2 19:58:00 | 只看该作者
如果有同名的窗体,就必须加上ParentId=251658241  的判断.
5#
发表于 2006-8-3 00:08:00 | 只看该作者
捕获错误方法也可以,ACCESS不是SQL SERVER。
6#
发表于 2006-8-3 02:15:00 | 只看该作者
Public Function IsExistTable(strTableName As String) As Boolean
'===============================================================================
'-函数名称:         IsExistTable
'-功能描述:         检查表是否存在
'-输入参数说明:     参数1:strTableName As String 检查表的名称
'-返回参数说明:     Boolean True表示存在 False 表示不存在
'-使用语法示例:     IsExistTable("表1")
'-参考:             ADO帮助
'-使用注意:
'-兼容性:           2000,XP,2003
'-作者:             fan0217@163.com
'-更新日期:        2006-03-17
'===============================================================================
On Error GoTo Error_IsExistTable
Dim Cnxn As New ADODB.Connection
Dim rstSchema As New ADODB.Recordset
Dim strCnxn As String
Set Cnxn = CurrentProject.Connection
Set rstSchema = Cnxn.OpenSchema(adSchemaTables)
    Do Until rstSchema.EOF
        If rstSchema!TABLE_NAME = Trim(strTableName) Then
            IsExistTable = True
        End If
        rstSchema.MoveNext
    Loop
rstSchema.Close
Cnxn.Close
Set rstSchema = Nothing
Set Cnxn = Nothing
Exit Function
Error_IsExistTable:
    IsExistTable = False
    If Not rstSchema Is Nothing Then
        If rstSchema.State = adStateOpen Then rstSchema.Close
    End If
    Set rstSchema = Nothing
    If Not Cnxn Is Nothing Then
        If Cnxn.State = adStateOpen Then Cnxn.Close
    End If
    Set Cnxn = Nothing
    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error_IsExistTable"
    End If
End Function
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-9 07:27 , Processed in 0.091507 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表