Office中国论坛/Access中国论坛

标题: 用VBA设定表的主关键字 [打印本页]

作者: wjb5645    时间: 2003-6-12 00:23
标题: 用VBA设定表的主关键字
我用"生成表查询"建一表后,想用按钮单击来设定某一字段为主关键字。
不懂写,请板主、高手们出手赐教!

作者: sunredday    时间: 2003-6-12 01:04
利用ADO 或DAO 追加INDEX

作者: wjb5645    时间: 2003-6-12 01:36
谢谢!
具体一点好吗?
作者: sunredday    时间: 2003-6-12 04:56
Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
     Dim idxNew As DAO.Index
     
     Set dbs = OpenDatabase("C:\yourdatabase.mdb")
    Set tdf = dbs!yourtable

    With tdf
        ' Create new index, create and append Field
        ' objects to its Fields collection.
        Set idxNew = .CreateIndex("NewIndex")

        With idxNew
            .Fields.Append .CreateField("国家")
            .Fields.Append .CreateField("姓氏")
            .Fields.Append .CreateField("名字")
        End With

        ' Add new Index object to the Indexes collection
        ' of the Employees table collection.
       .Indexes.Append idxNew
        .Indexes.Refresh
  END WITH








[此贴子已经被作者于2003-6-12 9:07:32编辑过]


作者: wjb5645    时间: 2003-6-12 09:06
谢谢sunredday大侠!!!
我看不懂语句的意思,请问("NewIndex")是否为表名,("国家")等为待设定的字段名?
试一下提示:缺少 End With
在最后加 End With
又提示:方法或数据成员未找到
请指教!
作者: sunredday    时间: 2003-6-12 17:10
引用DAO3.6
另外,增加的是索引,还不是主关键字。
作者: sunredday    时间: 2003-6-12 17:12
NEWINDEX 是索引名称,不用管它
国家等是你的作为索引的字段名称,此处用了三个字段作为一个索引。
作者: wjb5645    时间: 2003-6-12 18:03
sunredday谢谢!
还是不行.
错误指向 .Indexes.Append idxNew
Set tdf = dbs!yourtable是啥意思?
请再指教!
作者: wjb5645    时间: 2003-6-12 20:10
我乱改为:
Private Sub 命令1_Click()
Dim dbs As Database
    Dim tdf As TableDef
     Dim idxNew As Index
     
     Set dbs = OpenDatabase("d:\db1.mdb")
     'Set dbs = CurrentDb()
    Set tdf = dbs![站点表]
    With tdf
               Set idxNew = .CreateIndex("primarykey")
        

        With idxNew
            .Fields.Append .CreateField("站点")
            '.Fields.Append .CreateField("站点ID")
            '.Fields.Append .CreateField("名字")
End With

             tdf.Indexes.Append idxNew
        tdf.Indexes.Refresh
  
   End With
   
End Sub

只可加一个索引(为无重复),如多一两字段,却只显一个,其它的是加了索引可在表设计中看不到?
现差一步之遥,请大侠们帮忙啦!

作者: sunredday    时间: 2003-6-12 20:19
主键加
idxNew.Primary = True

Set tdf = dbs!yourtable是啥意思?
你的表的名称为表1,则
Set tdf = dbs!表1

作者: sunredday    时间: 2003-6-12 20:20
Private Sub 命令6_Click()
Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
     Dim idxNew As DAO.Index
     
     Set dbs = OpenDatabase("C:\Documents and Settings\administrator\My Documents\db1.mdb")
    Set tdf = dbs!表1

    With tdf
        ' Create new index, create and append Field
        ' objects to its Fields collection.
        Set idxNew = .CreateIndex("NewIndex")

        With idxNew
            .Fields.Append .CreateField("dfas")
               idxNew.Primary = True
            
        End With

        ' Add new Index object to the Indexes collection
        ' of the Employees table collection.
       .Indexes.Append idxNew
     
        .Indexes.Refresh
  End With






   
End Sub

作者: wjb5645    时间: 2003-6-12 21:18
非常感激sunredday大侠,帮我解决了大问题.
这个论坛高手很多,能耐心地有救苦救难风范的侠客不多.
再次感谢sunredday大侠!!!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3