重金求答(dao数据库编程)

xiaoxiaohan 2002-04-02 07:42:17
在VB中不使用控件,运用DAO建立ACCESS数据库.在建库之初不建立主键与索引,然后向数据库中添加数据,添加数据后建立主键与索引。
问题如下:
我添加数据后,不知如何得到已建立数据库的tbdef,因此不能建立主键与索引,请各位大侠指教。
...全文
45 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
bcrun 2002-04-11
  • 打赏
  • 举报
回复
另外证实用ADO做可以这样,唉,都是从http://www.vbforums.com/上找的

Private Sub Command2_Click()
Dim tbl As New Table
Dim cat As New adox.Catalog
Dim col As New adox.Column

' Open the Catalog.
cat.ActiveConnection = m_str_Connect
col.Name = "ID"
Set col.ParentCatalog = cat
col.Type = adInteger
col.Properties("AUTOINCREMENT").Value = True
cat.Tables("MyTable4").Columns.Append col

End Sub
bcrun 2002-04-11
  • 打赏
  • 举报
回复
人世界最简单的莫过于此:)

ALTER TABLE T2 ADD COLUMN ID COUNTER
sonicdater 2002-04-03
  • 打赏
  • 举报
回复
This example creates a new Index object, appends it to the Indexes
collection of the Employees TableDef, and then enumerates the Indexes
collection of the TableDef. Finally, it enumerates a Recordset, first using
the primary Index, and then using the new Index. The IndexOutput procedure
is required for this procedure to run.

===================================================================
Sub IndexObjectX()

Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim idxNew As Index
Dim idxLoop As Index
Dim rstEmployees As Recordset

Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind!Employees

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

With idxNew
.Fields.Append .CreateField("Country")

..Fields.Append .CreateField("LastName")
.Fields.Append .CreateField("FirstName")
End With

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

Debug.Print .Indexes.Count & " Indexes in " & _
.Name & " TableDef"

' Enumerate Indexes collection of Employees
' table.
For Each idxLoop In .Indexes
Debug.Print " " & idxLoop.Name

Next idxLoop

Set rstEmployees = _
dbsNorthwind.OpenRecordset("Employees")

' Print report using old and new indexes.
IndexOutput rstEmployees, "PrimaryKey"
IndexOutput rstEmployees, idxNew.Name
rstEmployees.Close

' Delete new Index because this is a
' demonstration.
.Indexes.Delete idxNew.Name
End With

dbsNorthwind.Close

End Sub

Sub IndexOutput(rstTemp As Recordset, _
strIndex As String)
' Report function for FieldX.

With rstTemp
' Set the index.
.Index = strIndex
.MoveFirst
Debug.Print "Recordset = " & .Name & _
", Index = " & .Index
Debug.Print " EmployeeID - Country - Name"

' Enumerate the recordset using the specified
' index.
Do While Not .EOF
Debug.Print " " & !EmployeeID & " - " & _
!Country & " - " & !LastName & ", " & !FirstName
.MoveNext
Loop

End With

End Sub
===================================================================
xiaoxiaohan 2002-04-03
  • 打赏
  • 举报
回复
To sonicdater(发呆呆)
我先添加数据再建主键和索引的目的是看一看能否提高速度,因为数据量较大,每添加一条记录就往索引里加可能会影响速度。
thorkhan 2002-04-02
  • 打赏
  • 举报
回复
Dim sTmp As String
Dim tbl As TableDef
Dim cnDase As Database
On Error Resume Next

For Each tbl In cnDase.TableDefs
sTmp = tbl.Name
If (cnDase.TableDefs(sTmp).Attributes And dbSystemObject) = 0 Then
tbl.CreateIndex
' 判斷是那個表
If sTmp = "List" Then
tbl.CreateIndex
end if
End If
Next
thorkhan 2002-04-02
  • 打赏
  • 举报
回复
To sonicdater(发呆呆)
不好意思﹐我是從我的源代碼中Copy出來的﹐沒仔細看﹐Sorry
sonicdater 2002-04-02
  • 打赏
  • 举报
回复
To: xiaoxiaohan
我不太明白你的意思. 你既然 已建立了表,怎么会 无法获得 TableDefs 集合呢?
另外, 你是不是想 添加 主键 或是 索引?
sonicdater 2002-04-02
  • 打赏
  • 举报
回复
thorkhan(灰滿) 的程序 不对吧. 怎么连 End If 都少?
wkoji 2002-04-02
  • 打赏
  • 举报
回复
GZ
道素 2002-04-02
  • 打赏
  • 举报
回复
Private Sub Command1_Click()

On Error GoTo Err100

'定义表与字段
Dim DefDatabase As Database
Dim DefTable As TableDef, DefField As Field

Set DefDatabase = Workspaces(0).OpenDatabase(App.Path & "\vbeden.mdb", 0, False)
Set DefTable = DefDatabase.CreateTableDef("xxxx")

'dbBinary = 9
'dbBoolean = 1
'dbByte = 2
'dbChar=18
'dbDate=8
'dbInteger=3
'dbLong=4
'dbMemo=12
'dbText=10

'建立Name字段为8个字符型
Set DefField = DefTable.CreateField("Name", dbText, 8)
DefTable.Fields.Append DefField

Set DefField = DefTable.CreateField("Sex", dbText, 2)
DefTable.Fields.Append DefField

'该字段允许为空
DefField.AllowZeroLength = True
'建立Age字段为3个的常整型
Set DefField = DefTable.CreateField("Age", dbInteger, 3)
'字段追加
DefTable.Fields.Append DefField

'表追加
DefDatabase.TableDefs.Append DefTable

MsgBox " 数据库建立完成!", vbInformation

Exit Sub

Err100:
MsgBox "对不起,不能建立表。请先再建表前建立数据库? ", vbCritical

End Sub

Private Sub cmdCreate_Click()

On Error GoTo Err100

CreateDatabase "VB-CODE", dbLangGeneral

MsgBox "数据库建立完成! ", vbInformation

Exit Sub

Err100:
MsgBox "不能建立数据库! " & vbCrLf & vbCrLf & Err.Description, vbInformation

End Sub
thorkhan 2002-04-02
  • 打赏
  • 举报
回复
Dim sTmp As String
Dim tbl As TableDef
Dim cnDase As Database
On Error Resume Next

For Each tbl In cnDase.TableDefs
sTmp = tbl.Name
If (cnDase.TableDefs(sTmp).Attributes And dbSystemObject) = 0 Then
tbl.CreateIndex
' 判斷是那個表
If sTmp = "List" Then
tbl.CreateIndex
End If
Next
xiaoxiaohan 2002-04-02
  • 打赏
  • 举报
回复
没有重复
ltpao 2002-04-02
  • 打赏
  • 举报
回复
可能要建主键的字段数据有重复吧?
happybeyond 2002-04-02
  • 打赏
  • 举报
回复
up一下!
happybeyond 2002-04-02
  • 打赏
  • 举报
回复
学习中!关注!

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧