| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 650 人关注过本帖, 1 人收藏
标题:添加数据的代码写好了,可是修改记录的代码怎么写都显示错误。
只看楼主 加入收藏
sankes
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2009-7-19
结帖率:100%
收藏(1)
已结贴  问题点数:10 回复次数:3 
添加数据的代码写好了,可是修改记录的代码怎么写都显示错误。
下面是添加记录的代码。请教修改记录的代码。
Private Sub cmdAdd_Click()
    Dim sc As Integer

        If Not (Testtxt(txtNo.Text) Or Testtxt(txtName.Text) Or Testtxt(txtSex.Text) Or Testtxt(txtBookTime.Text) Or Testtxt(txtTel.Text) Or Testtxt(txtEnrolTime.Text)) Then
            MsgBox "带 * 号为必填项", vbOKOnly + vbExclamation, "警告"
            txtNo.SetFocus
        Exit Sub
        End If
            sc = MsgBox("确实要添加这条记录吗?", vbOKCancel, "提示信息")
        If sc = 1 Then
            Dim conn As New ADODB.Connection
            Dim rs As New ADODB.Recordset
            Dim Str1 As String
            Dim Str2 As String
            Dim Str3 As String
                Str1 = "Provider=Microsoft.Jet.OLEDB.4.0;"
                Str2 = "Data Source=C:\Users\sAnKes\Desktop\大学生心理健康中心\data.mdb;"
                Str3 = "Jet OLEDB:Database Password="
                conn.Open Str1 & Str2 & Str3
                strSQL = "select * from 预约者"
                rs.Open strSQL, conn, 3, 3
                rs.AddNew
                rs!编号 = txtNo.Text
                rs!姓名 = txtName.Text
                rs!性别 = txtSex.Text
                rs!所在学院 = txtCollege.Text
                rs!所在专业 = txtSpeciality.Text
                rs!联系电话 = txtTel.Text
                rs!预约时间 = txtBookTime.Text
                rs!登记时间 = txtEnrolTime.Text
                rs!备注 = txtRemark.Text
                rs.Update
                rs.Close
                conn.Close
                MsgBox ("添加记录成功!")
                Adodc1.Refresh
        End If
            txtNo.Text = ""
            txtName.Text = ""
            txtSex.Text = ""
            txtCollege.Text = ""
            txtSpeciality.Text = ""
            txtTel.Text = ""
            txtBookTime.Text = ""
            txtEnrolTime.Text = ""
            txtRemark.Text = ""
   
End Sub

Private Sub cmdEdit_Click()
Dim sc As Integer
    Frame2.Visible = True
    Frame1.Visible = False
    cmdAdd.Enabled = False
    cmdDelect.Enabled = False
    cmdPrint.Enabled = False
    cmdPutout.Enabled = False
    cmdEdit.Caption = "保存"
   
    txtNo.Text = Text2(0)
    txtName.Text = Text2(1)
    txtSex.Text = Text2(2)
    txtCollege.Text = Text2(3)
    txtSpeciality.Text = Text2(4)
    txtTel.Text = Text2(5)
    txtBookTime.Text = Text2(6)
    txtEnrolTime.Text = Text2(7)
    txtRemark.Text = Text2(8)
   
        If Not (Testtxt(txtNo.Text) Or Testtxt(txtName.Text) Or Testtxt(txtSex.Text) Or Testtxt(txtBookTime.Text) Or Testtxt(txtTel.Text) Or Testtxt(txtEnrolTime.Text)) Then
            MsgBox "带 * 号为必填项", vbOKOnly + vbExclamation, "警告"
            txtNo.SetFocus
        Exit Sub
        End If

            Dim conn As New ADODB.Connection
            Dim rs As New ADODB.Recordset
            Dim Str1 As String
            Dim Str2 As String
            Dim Str3 As String
                Str1 = "Provider=Microsoft.Jet.OLEDB.4.0;"
                Str2 = "Data Source=C:\Users\sAnKes\Desktop\大学生心理健康中心\data.mdb;"
                Str3 = "Jet OLEDB:Database Password="
                conn.Open Str1 & Str2 & Str3
                strSQL = "select * from 预约者"
                rs.Open strSQL, conn, 1, 3
                rs!编号 = 1
                rs!姓名 = txtName.Text
                rs!性别 = txtSex.Text
                rs!所在学院 = txtCollege.Text
                rs!所在专业 = txtSpeciality.Text
                rs!联系电话 = txtTel.Text
                rs!预约时间 = 2004 - 2 - 2
                rs!登记时间 = 2004 - 2 - 2
                rs!备注 = txtRemark.Text
                rs.Update
                rs.Close
                conn.Close
                Adodc1.Refresh

End Sub
搜索更多相关主题的帖子: 修改数据 
2009-08-20 12:37
三断笛
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:31
帖 子:1621
专家分:1617
注 册:2007-5-24
收藏
得分:0 

strSQL = "select * from 预约者"这句执行以后Recordset指定第一条记录,也就是说你后面的Update修改的只是第一条记录而已,如果你要修改特定编号的数据你就得移到那条数据或者用strSQL = "select * from 预约者 where 编号你的编号",此时再修改。而且修改前你需要判断该查询是否有记录,如果记录数为0则不能修改。另外,你还可以直接用update语句进行修改,这样比较快。
2009-08-20 14:19
sankes
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2009-7-19
收藏
得分:0 
直接用update语句?能大概举个例子吗?
2009-08-20 15:09
三断笛
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:31
帖 子:1621
专家分:1617
注 册:2007-5-24
收藏
得分:10 
修改编号为XX的姓名为"新姓名"<br>sql="update 表名 set 姓名='新姓名' where 编号=xx"<br>conn.execute sql
2009-08-20 19:38
快速回复:添加数据的代码写好了,可是修改记录的代码怎么写都显示错误。
数据加载中...
 
   



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

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