有一个生成表的模块,用ADOX来实现.(其实用ADO也就行了,可是随便怎么调试都报错,实在是水平太差).
  在模块初始时有一个输入新表名的对话框,现在也就有问题了.怎么样才能检查这个表名是否已经存在了呢?如果存在的话,就要回报出错信息,重新输入.  这个问题我已经想的头发也快白了,还是没有想出.  附:模块代码Private Sub Command25_Click()
'建新表
Dim strTableName As String
strTableName = InputBox("请输入新建的测量名称,然后单击[确定]", "输入新建表名")
If strTableName = "" Then
MsgBox "测量名不得为空,请重新输入", vbCritical, "错误"
Call Command25_Click
End If
Dim sql As String
'sql = "Create Table " + strTableName + " (tp1 adInteger, tp2 adInteger, tp3 adInteger, tp4 adInteger, tp5 adInteger, tp6 adInteger, tp7 adInteger, tp8 adInteger)"
'cn.Execute (sql)
Dim tbl As New Tablecat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db2.mdb; Persist Security Info=False"
tbl.Name = strTableName
tbl.Columns.Append "tp1", adInteger    '表的第一个字段
tbl.Columns.Append "tp2", adInteger
tbl.Columns.Append "tp3", adInteger
tbl.Columns.Append "tp4", adInteger
tbl.Columns.Append "tp5", adInteger
tbl.Columns.Append "tp6", adInteger
tbl.Columns.Append "tp7", adInteger
tbl.Columns.Append "tp8", adInteger
cat.Tables.Append tbl    '建立数据表Frame1.Visible = True'开始测量
Call Command1_Click
Call Command4_Click
Call Command7_Click
Call Command10_Click
Call Command13_Click
Call Command16_Click
Call Command19_Click
Call Command22_Click
End Sub

解决方案 »

  1.   

    if exists (select * from " & strTableName & ")
    drop table [dbo].[AJJGINFO]
      

  2.   

    if exists (select 1
                from  sysobjects
               where  id = object_id('TableName')
                and   type = 'U')
       drop table TableName
      

  3.   

    写成存储过程,然后在vb里调用或strSql="select 1 from  sysobjects 
               where  id = object_id('TableName')
                and   type = 'U'"
    rs.open strSql, .....
    if not rs.eof then
        conn.execute "drop table TableName"
    end if上面语句我没用过,你试试吧
      

  4.   

    rs.open strSql, .....请问...省略的是什么啊?小的实在是太菜了....
      

  5.   

    dim conn as adodb.connection
    dim rs as adodb.recordserdim strConn as string
    dim strSql as stringstrConn=连接数据库的字串
    strSql="select 1 from  sysobjects 
               where  id = object_id('TableName')
                and   type = 'U'"conn.open strConn
    rs.open strSql, conn,adOpenStatic,adLockReadOnly
    if not rs.eof then
        conn.execute "drop table TableName"
    end if
      

  6.   

    cn.execute"if exists (select 1
                from  sysobjects
               where  id = object_id('TableName')) drop table TableName"
      

  7.   

    为什么我在执行cn.execute"if exists (select 1
                from  sysobjects
               where  id = object_id('TableName')) drop table TableName"时,总会报"实时错误3740,对象关闭时,不允许操作"???我在module里已经有Public Sub main()
      cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db2.mdb; Persist Security Info=False"
      cn.CursorLocation = adUseClient
      cn.Open
      frmMain.Show
    End Sub这段代码了呀.......郁闷........
      

  8.   

    你在本模块的别的地方是不是有cn.close
    或set cn=nothing的地方
    试着屏蔽一下
      

  9.   

    cn.open了没有,第一个方法应该是这个吧
      

  10.   

    还有,set cn=new adodb.connection了没有