vb6.0无法在access2000里新增记录
初学VB6.0从书上输入以下程序到vb6.0里运行,但不能在access2000里新增记录,并且在access2000里需要转换vb创建的.mdb文件。如何才能在access2000里新增记录,及如何让access2000直接打开vb创建的.mdb文件。
Option Explicit
Dim VBDb As Database, VBWs As Workspace
Dim nametd As TableDef, scoretd As TableDef
Dim nameflds(5) As Field, scoreflds(4) As Field
Dim nameidx As Index, scoreidx As Index
Private Sub command1_click()
Data1.Recordset.AddNew
End Sub
Private Sub command2_click()
Dim msg As String
msg = "是否确定要删除记录 " & Chr$(10)
msg = msg & Text1.Text
If MsgBox(msg, 17, "删除记录? ") <> 1 Then Exit Sub
On Error GoTo Handle
Data1.Recordset.Delete
Data1.Recordset.MoveNext
If Data1.Recordset.EOF Then Data1.Recordset.MovePrevious
Handle:
MsgBox "该记录不能删除!! "
End Sub
Private Sub Command3_Click()
Data1.Recordset.MovePrevious
End Sub
Private Sub Command4_Click()
Data1.Recordset.MoveNext
End Sub
Private Sub Command5_Click()
End
End Sub
Private Sub form_load()
Dim datafilename As String
Dim i As Integer
datafilename = InputBox( "输入要创建的数据库文件名(*.mdb): ", "创建数据库 ")
If datafilename <> " " Then
If Right(datafilename, 4) <> ".mdb " Then
datafilename = datafilename + ".mdb "
End If
On Error GoTo 1
Set VBWs = DBEngine.Workspaces(0)
Set VBDb = VBWs.CreateDatabase(App.Path + "\ " + datafilename, dbLangGeneral)
setname
setscore
GoTo 2
1
If MsgBox( "文件已经存在,要打开该文件吗? ", vbYesNo) = vbNo Then End
Else
End
2
End If
Data1.DatabaseName = App.Path + "\ " + datafilename
Data1.Align = 2
Data1.RecordSource = "地址簿 "
Text1.DataField = "Au_ID "
Text2.DataField = "姓名 "
Text3.DataField = "出生年月 "
Text4.DataField = "工作单位 "
Text5.DataField = "电话 "
Text6.DataField = "E_mail地址 "
End Sub
Private Sub setname()
Set nametd = VBDb.CreateTableDef( "地址簿 ")
Set nameflds(0) = nametd.CreateField( "Au_ID ", dbLong)
nameflds(0).Attributes = dbAutoIncrField
Set nameflds(1) = nametd.CreateField( "姓名 ", dbText)
nameflds(1).Size = 8
Set nameflds(2) = nametd.CreateField( "出生年月 ", dbLong)
nameflds(2).Size = 4
Set nameflds(3) = nametd.CreateField( "工作单位 ", dbText)
nameflds(3).Size = 50
Set nameflds(4) = nametd.CreateField( "电话 ", dbLong)
nameflds(4).Size = 15
Set nameflds(5) = nametd.CreateField( "E_Mail地址 ", dbText)
nameflds(5).Size = 30
nametd.Fields.Append nameflds(0)
nametd.Fields.Append nameflds(1)
nametd.Fields.Append nameflds(2)
nametd.Fields.Append nameflds(3)
nametd.Fields.Append nameflds(4)
nametd.Fields.Append nameflds(5)
VBDb.TableDefs.Append nametd
End Sub
Private Sub setscore()
Set scoretd = VBDb.CreateTableDef( "成绩单 ")
Set scoreflds(0) = scoretd.CreateField( "Au_ID ", dbLong)
scoreflds(0).Attributes = dbAutoIncrField
Set scoreflds(1) = scoretd.CreateField( "姓名 ", dbText)
scoreflds(1).Size = 8
Set scoreflds(2) = scoretd.CreateField( "语文 ", dbLong)
scoreflds(2).Size = 4
Set scoreflds(3) = scoretd.CreateField( "数学 ", dbLong)
scoreflds(3).Size = 4
Set scoreflds(4) = scoretd.CreateField( "外语 ", dbLong)
scoreflds(4).Size = 4
scoretd.Fields.Append scoreflds(0)
scoretd.Fields.Append scoreflds(1)
scoretd.Fields.Append scoreflds(2)
scoretd.Fields.Append scoreflds(3)
scoretd.Fields.Append scoreflds(4)
VBDb.TableDefs.Append scoretd
End Sub
[解决办法]
将data控件换成ado控件吧
[解决办法]
就是直接在工具箱中选择ado控件,如果没有的话在组件中添加!!