| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1543 人关注过本帖
标题:我的数据库数据“新增”按钮虽然能记录,但不能保存
只看楼主 加入收藏
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
结帖率:84.62%
收藏
 问题点数:0 回复次数:17 
我的数据库数据“新增”按钮虽然能记录,但不能保存

我的数据库数据“新增”按钮虽然能记录,但不能保存!退出,再打开就没有了!

可以访问该数据库,其他CMD按钮如“下一条”“上一条”等功能均正常,
其“要求更新”的代码如下:

Private Sub Data1_Validate(Action As Integer, Save As Integer)
Static addrecord As Boolean
Dim msg
Select Case Action
Case 1, 2, 3, 4, 5, 11
If Save Then
msg = MsgBox("数据需要更新么?", vbYesNo)
If msg = vbNo Then Save = False

If addrecord Then
Data1.UpdateControls

End If

End If
If Action = 5 Then addrecord = True Else addrecord = False
Command2.Enabled = True
End Select
End Sub

执行“新增”结束后返回主菜单时,结果依然显示新增之前的数据,执行失败!

搜索更多相关主题的帖子: 数据库 按钮 保存 记录 
2006-10-13 23:14
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
...不要再用data控件了..要么用ado控件..要么用ado对象模型....
dim con as new adodb.connection
dim rst as new adodb.recordset
conn.open "Provider=Microsoft.Jet.Oledb.4.0;Data Source = D:\mdb\Northwind.mdb"
rst.open "select * from orders",conn, ...,...
rst.addnew
rst.fields("ddd").value = "dfadf"
rst.fields("ccc").value = "hhh"
rst.update

我的msn: myfend@
2006-10-14 09:15
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

难道就将DATA控件改为ADO控件么?再添加如上代码么?

你看,这怎么修改为用ADO打开ACCESS数据库呢?再进行编辑呢?

你看我打算用VB的CMD控制按钮(下一条、上一条、最后一条、第一条、删除。。。),(除了“新增”控件出错外,其余均正常)

再将“新增”按钮的控件代码改下就可以了么?

其代码如下:
Private Sub Command1_Click()

If Command1.Caption = "新增" Then
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
Command1.Caption = "确定"
Command2.Caption = "放弃"
If Data1.Recordset.RecordCount > 0 Then
Data1.Recordset.MoveLast
End If
Data1.Recordset.AddNew
Data1.Recordset.Update
Text1.SetFocus
Else
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
Text10.Text = ""
Text11.Text = ""
Text12.Text = ""
Text13.Text = ""
Text14.Text = ""
Text15.Text = ""
Command1.Caption = "新增"
Command2.Caption = "删除"
Command1.SetFocus
End If
End Sub
Private Sub Command2_Click()
Dim str1
If Command2.Caption = "放弃" Then
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Command1.Caption = "新增"
Command2.Caption = "删除"
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Text5.Enabled = False
Text6.Enabled = False
Text7.Enabled = False
Text8.Enabled = False
Text9.Enabled = False
Text10.Enabled = False
Text11.Enabled = False
Text12.Enabled = False
Text13.Enabled = False
Text14.Enabled = False
Text15.Enabled = False
Else
If Data1.Recordset.RecordCount = 0 Then
MsgBox "没有记录!", 32, "注意"
Exit Sub
Else
str1 = MsgBox("删除该记录么?", 17, "删除")
If str1 = 1 Then
Data1.Recordset.Delete
Data1.Refresh
If Data1.Recordset.RecordCount = 0 Then
MsgBox "记录数为零!"
Data1.Recordset.AddNew
End If
End If
End If
End If

End Sub


Private Sub Command3_Click()
If Data1.Recordset.RecordCount = 0 Then
MsgBox "没有记录!"
Else
If Data1.Recordset.BOF Then
MsgBox "这是第一条记录!"
Data1.Recordset.MoveFirst
Else
Data1.Recordset.MovePrevious
If Data1.Recordset.BOF = True Then
Data1.Recordset.MoveFirst
MsgBox "这是第一条记录!"
End If
End If
End If
End Sub

Private Sub Command4_Click()
If Data1.Recordset.RecordCount = 0 Then
MsgBox "没有记录!"
Else
If Data1.Recordset.EOF Then
Data1.Recordset.MoveLast
MsgBox "这是最后一条记录!"

Else
Data1.Recordset.MoveNext
If Data1.Recordset.EOF = True Then
Data1.Recordset.MoveLast
MsgBox "这是最后一条记录!"
End If
End If
End If
End Sub

Private Sub Command5_Click()
If Data1.Recordset.RecordCount = 0 Then
MsgBox "没有记录!"
Else
Data1.Recordset.MoveFirst
End If
End Sub

Private Sub Command6_Click()
If Data1.Recordset.RecordCount = 0 Then
MsgBox "没有记录!"
Else
Data1.Recordset.MoveLast
End If
End Sub

Private Sub Command7_Click()
main.Show
Unload txlqqmc

End Sub

Private Sub Data1_Validate(Action As Integer, Save As Integer)
Static addrecord As Boolean
Dim msg
Select Case Action
Case 1, 2, 3, 4, 5, 11
If Save Then
msg = MsgBox("数据需要更新么?", vbYesNo)
If msg = vbNo Then
Save = 0
If addrecord Then
Data1.UpdateControls

End If
End If
End If
If Action = 5 Then addrecord = True Else addrecord = False
Command2.Enabled = True
End Select
End Sub

Private Sub Form_Activate()
If Data1.Recordset.RecordCount = 0 Then
Data1.Recordset.AddNew
Data1.Recordset.Update
Else
Data1.Recordset.MoveLast
Data1.Recordset.MoveFirst
End If
End Sub

如何修改呢?

谢谢!

2006-10-14 17:06
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 

这个..当然不是说..你将Data控件改为Ado就能完全地解决问题...只是个建议,,不要再去使用Data这个老古董..它的确会衔生出很多问题来..而且对以后的程序升级..也是一个灾难...
...我用Ado对象写了一个例子..你慢慢参考一下...


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'-------------------程序员:purana-----------------------
'-------------------时间:2006.10.14---------------------
'-------------------E-Mail: myfend_liang@yahoo.com.cn---
'-------------------模块功能:对Ado对象模型的操作--------
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Option Explicit
Private Conn As ADODB.Connection
Private Rst As New ADODB.Recordset

Private Sub CmdAdd_Click()
CmdDelete.Enabled = False
CmdPrevious.Enabled = False
CmdNext.Enabled = False
CmdFirst.Enabled = False
CmdLast.Enabled = False

'Set Rst = CreateObject("ADODB.Recordset")
If (Rst.State = adStateOpen) Then
Rst.Close
End If
Rst.CursorLocation = adUseClient
Rst.Open "Select * From AddRecord", Conn, adOpenKeyset, adLockPessimistic
Rst.AddNew
Rst.Fields("Field1").Value = Text1.Text
Rst.Fields("Field2").Value = Text2.Text
Rst.Update
MsgBox "添加成功"

CmdDelete.Enabled = True
CmdPrevious.Enabled = True
CmdNext.Enabled = True
CmdFirst.Enabled = True
CmdLast.Enabled = True
End Sub

Private Sub CmdDelete_Click()
CmdAdd.Enabled = False
CmdPrevious.Enabled = False
CmdNext.Enabled = False
CmdFirst.Enabled = False
CmdLast.Enabled = False



'Rst.CursorLocation = adUseClient
If MsgBox("要删除吗?", vbExclamation + vbYesNo) = vbYes Then
Rst.Delete
End If

MsgBox "删除成功"

CmdAdd.Enabled = True
CmdPrevious.Enabled = True
CmdNext.Enabled = True
CmdFirst.Enabled = True
CmdLast.Enabled = True
End Sub

Private Sub CmdFirst_Click()
Rst.MoveFirst
Text1.Text = Rst.Fields("Field1").Value
Text2.Text = Rst.Fields("Field2").Value
End Sub

Private Sub CmdLast_Click()
Rst.MoveLast
Text1.Text = Rst.Fields("Field1").Value
Text2.Text = Rst.Fields("Field2").Value
End Sub

Private Sub CmdNext_Click()
If (Not Rst.EOF) Then
Rst.MoveNext
If (Not Rst.EOF) Then
Text1.Text = Rst.Fields("Field1").Value
Text2.Text = Rst.Fields("Field2").Value
Else
Rst.MoveLast
Text1.Text = Rst.Fields("Field1").Value
Text2.Text = Rst.Fields("Field2").Value
End If
End If
End Sub

Private Sub CmdPrevious_Click()

If (Not Rst.BOF) Then
Rst.MovePrevious
If (Not Rst.BOF) Then

Text1.Text = Rst.Fields("Field1").Value
Text2.Text = Rst.Fields("Field2").Value
Else
Rst.MoveFirst
Text1.Text = Rst.Fields("Field1").Value
Text2.Text = Rst.Fields("Field2").Value
End If
End If
End Sub

Private Sub Form_Load()
Dim ConString As String
ConString = "Provider=Microsoft.Jet.OleDb.4.0;Persist Security Info = False;" _
& "Data Source =" & App.Path & "\AddRecord_Demo.mdb"

Set Conn = CreateObject("ADODB.Connection")
With Conn
.ConnectionString = ConString
.Open
End With

Rst.CursorLocation = adUseClient
Rst.Open "Select * From AddRecord", Conn, adOpenKeyset, adLockPessimistic, adCmdText
End Sub


goSXp5YM.zip (9.84 KB) 我的数据库数据“新增”按钮虽然能记录,但不能保存



我的msn: myfend@
2006-10-14 18:07
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
我模仿修改了,

编译执行时,出现对话框:

编译错误,用户定义类型未定义


蓝条覆盖在第二行:"Private Conn As ADODB.Connection"?




2006-10-14 21:41
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

“新增”(COMMAND1)控件如下代码:

Option Explicit
Private Conn As ADODB.Connection
Private Rst As New ADODB.Recordset
Private Sub Command1_Click()

If Command1.Caption = "新增" Then
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
Command1.Caption = "确定"
Command2.Caption = "放弃"


If (Rst.State = adStateOpen) Then
Rst.Close
End If
Rst.CursorLocation = adUseClient
Rst.Open "Select * From AddRecord", Conn, adOpenKeyset, adLockPessimistic
Rst.AddNew
Rst.Fields("姓名:").Value = Text1.Text
Rst.Fields("单位名称:").Value = Text2.Text
Rst.Fields("办公电话:").Value = Text3.Text
Rst.Fields("办公电话二:").Value = Text4.Text
Rst.Fields("手机号码:").Value = Text5.Text
Rst.Fields("手机号码二:").Value = Text6.Text
Rst.Fields("宅电:").Value = Text7.Text
Rst.Fields("宅电二:").Value = Text8.Text

Rst.Fields("邮箱:").Value = Text9.Text
Rst.Fields("邮箱二:").Value = Text10.Text
Rst.Fields("QQ号:").Value = Text11.Text
Rst.Fields("QQ号二:").Value = Text12.Text
Rst.Fields("地址:").Value = Text13.Text
Rst.Fields("邮政编码:").Value = Text14.Text
Rst.Fields("其他:").Value = Text15.Text

Rst.Update
MsgBox "添加成功"
Text1.SetFocus
Else
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command6.Enabled = True
Command7.Enabled = True
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
Text10.Text = ""
Text11.Text = ""
Text12.Text = ""
Text13.Text = ""
Text14.Text = ""
Text15.Text = ""
Command1.Caption = "新增"
Command2.Caption = "删除"
Command1.SetFocus
End If
End Sub

2006-10-14 22:00
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
后来,我在工程---》引用---》又添加了:“Microsoft ActiveX Data Objects Recordset 2.8 Library(这大概就是ADO控件了)

执行之,出现实时错误:“-2451378,(13542176)”
文件名无效!
调试,黄条显现在 “.open”上!
2006-10-14 22:15
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

在Access 2000中用ADO
要在Access 2000中正确使用ADO你必须在VB工程中添加正确的引用。

1.打开一个新工程
2.在工程菜单中选择引用
3.在弹出来的对话框中选择Microsoft ActiveX Data Objects 2.1 Library.
4.接着你还必须选中Microsoft ADO Ext. 2.1 for DDL and Security.
5.最后选中Microsoft Jet and Replication Objects 2.1 Library.
6.单击“确定”
做完上述的步骤之后,你就可以用ADODC控件去访问Access 2000数据库了。

我也照着上面提供的步骤使ADO连接数据库!

2006-10-14 22:42
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

我在窗口田间了ADODC控件,如何设置它的属性,和编写代码呢?

2006-10-14 22:52
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
得分:0 
.........我调试成功..

我的msn: myfend@
2006-10-15 09:35
快速回复:我的数据库数据“新增”按钮虽然能记录,但不能保存
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017913 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved