office交流網(wǎng)--QQ交流群號(hào)及微信交流群

Access培訓(xùn)群:792054000         Excel免費(fèi)交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

微信交流群(請(qǐng)用微信掃碼)

        

Access 使用DAO及ADO創(chuàng)建數(shù)據(jù)表主鍵PrimaryKey的不同方法對(duì)比

2017-07-13 21:51:00
ADAM-Office中國(guó)
原創(chuàng)
5195

Access 使用DAO及ADO創(chuàng)建數(shù)據(jù)表主鍵PrimaryKey的不同方法對(duì)比,去來(lái)貼一個(gè)吧


一、Access 使用DAO創(chuàng)建Access 數(shù)據(jù)表主鍵PrimaryKey的代碼如下:



Sub DAOCreatePrimaryKey()
    Dim db      As DAO.Database
    Dim tbl     As DAO.TableDef
    Dim idx     As DAO.Index
    
    'Open the database
    Set db = DBEngine.OpenDatabase("C:\nwind.mdb")
    
    Set tbl = db.TableDefs("Contacts")
    
    ' Create the Primary Key and append table columns to it.
    Set idx = tbl.CreateIndex("PrimaryKey")
    idx.Primary = True
    idx.Fields.Append idx.CreateField("ContactId")
    
    ' Append the Index object to the Indexes collection of the TableDef.
    tbl.Indexes.Append idx
    db.Close
End Sub


二、Access 使用Ado創(chuàng)建Access 數(shù)據(jù)表主鍵PrimaryKey的代碼如下:


ADOX

Sub ADOCreatePrimaryKey()
    Dim cat     As New ADOX.Catalog
    Dim tbl     As ADOX.Table
    Dim pk      As New ADOX.Key
    
    ' Open the catalog
    cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\nwind.mdb;"
    Set tbl = cat.Tables("Contacts")
        
    ' Create the Primary Key and append table columns to it.
    pk.Name = "PrimaryKey"
    pk.Type = adKeyPrimary
    pk.Columns.Append "ContactId"
    
    ' Append the Key object to the Keys collection of Table
    tbl.Keys.Append pk
    
    Set cat = Nothing
    
End Sub
分享
文章分類
聯(lián)系我們
聯(lián)系人: 王先生
Email: 18449932@qq.com
QQ: 18449932
微博: officecn01
移動(dòng)訪問(wèn)