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

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

可以访问该数据库,其他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
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
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
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

ADODC1控件的ConnectionStr-->使用连接字符串---》生成---》Microsoft Jet 4.0 OLE DB Provider
测试连接---》成功!

另外每个TEXT的datasource : adodc1

执行之,出现:

[ADODC]没有指定记录源;[ADO]没有为命令对象设置命令

实时错误:-2147217865(80040e37)Microsoft Jet 数据库引擎找不到输入表或查询“db3”确定它是否存在,以及它的名称拼写是否正确

黄条覆盖在:Rst.Open "Select * From db3", Conn, adOpenKeyset, adLockPessimistic, adCmdText

2006-10-17 21:44
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 
又把ADODC1的RecordRource又设置为1--adCmdText

但还是错误状况不变!
2006-10-17 21:59
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

你的那个怎么没有ADOC控件呢?

我又将ADODC1属性commandtype 1--adCmdText
coursortype 1--adOpenKeyset
locktype 2--adLockPessimistic
RecordRource 1--adCmdText

Rst.Open "Select * From AddRecord", conn, adOpenKeyset, adLockPessimistic, adCmdText

这语句里的,“Select * From AddRecord”conn,怎么设置呢?

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



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

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