mydatabase 中是否存在studid,它的数据类型是否为字符型,如果是字符型用 Data1.RecordSource = "select * from MyDataBase where studID = '5'" 试一下。
所有代码: Option Explicit Private Sub Command1_Click(Index As Integer) Select Case Index Case 0 Data1.Recordset.MoveFirst Case 1 If Data1.Recordset.BOF <> True Then Data1.Recordset.MovePrevious Else Data1.Recordset.MoveFirst MsgBox "已是最前一条记录。" Exit Sub End If Case 2 If Data1.Recordset.EOF <> True Then Data1.Recordset.MoveNext Else Data1.Recordset.MoveLast MsgBox "已是最后一条记录。" Exit Sub End If Case 3 Data1.Recordset.MoveLast Case 4 If Command1(4).Caption = "添加记录" Then With Data1 .Recordset.Update End With MsgBox " 成功保存记录" Else Exit Sub End If Command1(4).Caption = "取消" Text1(0).SetFocus Case 5 Dim ii As Long ii = MsgBox("要更新现在这一记录吗?", vbOKCancel + _ vbQuestion + vbDefaultButton2, " 确认") If ii = vbOK Then On Error GoTo toerr
With Data1 .Recordset.Edit .Recordset.Update .Refresh End With Call ShowMDB_RecordsetCount MsgBox " 成功更新记录" Command1(4).Caption = "取消"
Exit Sub toerr: If Err.Number = 3022 Then MsgBox "学号,姓名只允许是唯一的,不能重复。" + Chr(13) + Chr(10) + "记录将不保存!再试", vbOKOnly + vbCritical, "确认" Command1(4).Caption = "取消" ElseIf Err.Number = 3020 Then Debug.Print Err.Description MsgBox "数据库未作改变,或未先点击编辑,更新无效。" Data1.UpdateControls Command1(4).Caption = "取消" End If Case 6 ' On Error Resume Next
With Data1 'AddNew 方法:只表示进入AddNew(及将添加一个记录时) 状态! _ 并不马上更新数据库记录集 .Recordset.AddNew End With '
Command1(4).Caption = "添加记录" Text1(0).SetFocus Case 7 Dim ff As Long ff = MsgBox("删除当前记录吗?", vbDefaultButton2 + vbYesNo + vbQuestion, "确认") If ff = vbYes Then Data1.Recordset.Delete If Data1.Recordset.EOF <> True Then Data1.Recordset.MoveNext Else Data1.Recordset.MoveFirst End If MsgBox " 删除成功" Call ShowMDB_RecordsetCount Else Exit Sub End If Case 8 Data1.Recordset.Edit Case 9 With CommonDialog1 If Dir("D:\mydocu~1\mypicture\myIcon", vbDirectory) <> "" Then .InitDir = "D:\mydocu~1\mypicture\myIcon" End If .Filter = "图片文件 *.Bmp *.Ico|*.ico;*.bmp|任意文件|*.*" .ShowOpen If .FileName <> "" Then Image1.Picture = LoadPicture(.FileName) End If
End With
End Select End SubPrivate Sub Command2_Click() 'On Error Resume Next
Data1.RecordSource = "select * from MyDataBase where stutID = 5" Data1.Refresh
End SubPrivate Sub Data1_Reposition() Debug.Print "rePtion" & Timer End SubPrivate Sub Data1_Validate(Action As Integer, Save As Integer) Debug.Print Action End SubPrivate Sub Form_Activate() Call ShowMDB_RecordsetCount End Sub Public Sub ShowMDB_RecordsetCount() With Data1 .Recordset.MoveLast 'Debug.Print .Recordset.RecordCount Label2.Caption = "现在有 " & _ .Recordset.RecordCount & " 条记录" .Recordset.MoveFirst End With End SubPrivate Sub Form_Load() With Data1 .DatabaseName = App.Path & "\tor.mdb" .RecordSource = "MyDataBase" .Refresh End With End SubPrivate Sub Image1_OLESetData(Data As DataObject, DataFormat As Integer) Debug.Print Data.GetFormats Debug.Assert DataFormat = vbCFBitmap End Sub
错误号:3011Microsoft Jet 数据库引擎找不到对象'select * from MyDataBase where stutID = 5'。请确定对象是否存在,并正确地写出它的名称和路径。
"select * from MyDataBase where stutID = '" & "5" &"'"
Data1.RecordSource = "select * from MyDataBase"改成这个测试一下,如果还是错误 就证明程序找不到相应的MDB数据库文件!
你可以用控件自动生成的sql语句试!
再进行自己代码正确与否的检查。你的Data控件主要连接的是Access数据库,
看起来怎么象Data1的属性设置问题呢。
Data1.RecordSource = "select * from MyDataBase where studID = '5'"
试一下。
Option Explicit
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
Data1.Recordset.MoveFirst
Case 1
If Data1.Recordset.BOF <> True Then
Data1.Recordset.MovePrevious
Else
Data1.Recordset.MoveFirst
MsgBox "已是最前一条记录。"
Exit Sub
End If
Case 2
If Data1.Recordset.EOF <> True Then
Data1.Recordset.MoveNext
Else
Data1.Recordset.MoveLast
MsgBox "已是最后一条记录。"
Exit Sub
End If
Case 3
Data1.Recordset.MoveLast
Case 4
If Command1(4).Caption = "添加记录" Then
With Data1
.Recordset.Update
End With
MsgBox " 成功保存记录"
Else
Exit Sub
End If
Command1(4).Caption = "取消"
Text1(0).SetFocus
Case 5
Dim ii As Long
ii = MsgBox("要更新现在这一记录吗?", vbOKCancel + _
vbQuestion + vbDefaultButton2, " 确认") If ii = vbOK Then
On Error GoTo toerr
With Data1
.Recordset.Edit
.Recordset.Update
.Refresh
End With
Call ShowMDB_RecordsetCount
MsgBox " 成功更新记录"
Command1(4).Caption = "取消"
Else
MsgBox "已选择取消,记录将不保存。", _
vbOKOnly + vbExclamation, "确认"
Data1.UpdateControls
Exit Sub
End If
Exit Sub
toerr:
If Err.Number = 3022 Then
MsgBox "学号,姓名只允许是唯一的,不能重复。" + Chr(13) + Chr(10) + "记录将不保存!再试", vbOKOnly + vbCritical, "确认"
Command1(4).Caption = "取消"
ElseIf Err.Number = 3020 Then
Debug.Print Err.Description
MsgBox "数据库未作改变,或未先点击编辑,更新无效。"
Data1.UpdateControls
Command1(4).Caption = "取消"
End If
Case 6
' On Error Resume Next
With Data1
'AddNew 方法:只表示进入AddNew(及将添加一个记录时) 状态! _
并不马上更新数据库记录集
.Recordset.AddNew
End With
'
Command1(4).Caption = "添加记录"
Text1(0).SetFocus
Case 7
Dim ff As Long
ff = MsgBox("删除当前记录吗?", vbDefaultButton2 + vbYesNo + vbQuestion, "确认")
If ff = vbYes Then
Data1.Recordset.Delete
If Data1.Recordset.EOF <> True Then
Data1.Recordset.MoveNext
Else
Data1.Recordset.MoveFirst
End If
MsgBox " 删除成功"
Call ShowMDB_RecordsetCount
Else
Exit Sub
End If
Case 8
Data1.Recordset.Edit
Case 9
With CommonDialog1
If Dir("D:\mydocu~1\mypicture\myIcon", vbDirectory) <> "" Then
.InitDir = "D:\mydocu~1\mypicture\myIcon"
End If
.Filter = "图片文件 *.Bmp *.Ico|*.ico;*.bmp|任意文件|*.*"
.ShowOpen
If .FileName <> "" Then
Image1.Picture = LoadPicture(.FileName)
End If
End With
End Select
End SubPrivate Sub Command2_Click()
'On Error Resume Next
Data1.RecordSource = "select * from MyDataBase where stutID = 5"
Data1.Refresh
End SubPrivate Sub Data1_Reposition()
Debug.Print "rePtion" & Timer
End SubPrivate Sub Data1_Validate(Action As Integer, Save As Integer)
Debug.Print Action
End SubPrivate Sub Form_Activate()
Call ShowMDB_RecordsetCount
End Sub
Public Sub ShowMDB_RecordsetCount()
With Data1
.Recordset.MoveLast
'Debug.Print .Recordset.RecordCount
Label2.Caption = "现在有 " & _
.Recordset.RecordCount & " 条记录"
.Recordset.MoveFirst
End With
End SubPrivate Sub Form_Load()
With Data1
.DatabaseName = App.Path & "\tor.mdb"
.RecordSource = "MyDataBase"
.Refresh
End With
End SubPrivate Sub Image1_OLESetData(Data As DataObject, DataFormat As Integer)
Debug.Print Data.GetFormats
Debug.Assert DataFormat = vbCFBitmap
End Sub
检查data1的databasename属性
自己调试一下,看看连没连上
并正确返回了记录集个数
并正确返回了记录集个数
'Data1.RecordSource = "select * from MyDataBase where studName _ Like " & chr(34) & "张" & chr(34)