在程序中如何ACCESS库中是否有此表,如有则进行添加记录如没有则先建立表然后再添加记录?最好给一个实例?谢谢各位了!

解决方案 »

  1.   

    rs1.open "select [name] from msysobjects where [type]=1 and [name]='" & 你要查的表名 & "'" if rs1.recordcount=0 then
        ... '创建表
    end if
    ... '添加记录set rs1=nothing
      

  2.   

    rs.open "select [name] from msysobjects where [type]=1 and [flags]=0 and [name]='" & strTableName & "'" ,cnif rs.eof then
      '创建表
      cn.execute "create table "& strtablename & "(a int,b int)"
    end if
     '添加记录
     cn.execute "insert into " & strtablename & "(a,b) select 1,2"
    rs.close
    set rs=nothing
      

  3.   

    '功能:获取access库中表的个数及表的名称
    '用ado怎样实现
    '工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)
    '----------------------------------------------------------------------------
    Private Sub Form_Load()
    Dim adoCN   As New ADODB.Connection                '定义数据库的连接
    Dim strCnn   As New ADODB.Recordset
    Dim rstSchema As New ADODB.Recordset
    Dim I As Integer
       str1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Northwind.MDB;Persist Security Info=False"
       adoCN.Open str1
             
       Set rstSchema = adoCN.OpenSchema(adSchemaTables)
         
       Do Until rstSchema.EOF
            If rstSchema!TABLE_TYPE = "TABLE" Then
               out = out & "Table  name:  " & _
                   rstSchema!TABLE_NAME & vbCr & _
                   "Table  type:  " & rstSchema!TABLE_TYPE & vbCr
                I = I + 1
            End If
            rstSchema.MoveNext
       Loop
       MsgBox I
       rstSchema.Close
         
       adoCN.Close
    Debug.Print out
    End Sub
      

  4.   

    '用ADOX建立数据库与表
    '引用Microsoft ADO Ext 2.5 for DDL and Security
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    Dim con As ADODB.Connection    On Error GoTo 0    ' Create the new database.
        Set cat = New ADOX.Catalog
        cat.Create _
            "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=" & DatabaseName & ";"    ' Create a new table.
        Set tbl = New ADOX.Table
        tbl.Name = "TestTable"
        tbl.Columns.Append "FirstName", adVarWChar, 40
        tbl.Columns.Append "LastName", adVarWChar, 40
        tbl.Columns.Append "Birthdate", adDate
        tbl.Columns.Append "Weight", adInteger
        cat.Tables.Append tbl 
       
        '设置列可以为NULL    
        tb1.columns("Weight").Attributes=AdColNullable

        ' Connect to the database.
        Set con = cat.ActiveConnection    ' Insert records.
        con.Execute "INSERT INTO TestTable VALUES ('Andy', 'Able', '1 Jan 1980', '150')"
        con.Execute "INSERT INTO TestTable VALUES ('Betty', 'Baker', #2/22/1990#, 70)"    ' Close the database connection.
        con.Close
        Set con = Nothing
        Set tbl = Nothing
        Set cat = Nothing