读书人

VB+access 防止输入重复的记录号解决方

发布时间: 2012-02-22 19:36:55 作者: rapoo

VB+access 防止输入重复的记录号
各位高手:
小弟是一名初学者,在试做添加记录,要求添加的编号不能与已经输入的编号重复,小弟搞了半天弄不出来,希望各位高手给予赐教,小弟不胜感激。
记录号在gzb的表单中是主索引。
我的代码:
Adodc1.RecordSource = "select*from gzb where 编号='" & Text1.Text & "'"
If Adodc1.Recordset("编号") = Text1.Text Then
MsgBox "编号存在"
Adodc1.Refresh
Else
Adodc1.Recordset.Update
End If


刚一运行Adodc1.RecordSource = "select*from gzb where 编号='" & Text1.Text & "'"就报错。请大家不惜赐教。


[解决办法]
select * from gzb where 编号
[解决办法]
Adodc1.RecordSource = "select * from gzb where 编号='" & Text1.Text & "'"

Adodc1.RecordSource = "select * from gzb where 编号=" & Val(Text1.Text)
[解决办法]

探讨
Adodc1.RecordSource = "select * from gzb where 编号='" & Text1.Text & "'"

Adodc1.RecordSource = "select * from gzb where 编号=" & Val(Text1.Text)

[解决办法]
数据库里面把这个表的"记录号"字段设为关键字段不可重复即可.
当你的程序试图向表中添加一个已经存在的记录号时会返回一个错误, 你只要捕捉这个错误号就可以了.
[解决办法]
VB code
    Adodc1.ConnectionString = "" '请给Adodc1.ConnectionString赋值    Adodc1.RecordSource = "select * from gzb where 编号='" & Text1.Text & "'"    Adodc1.Refresh    If Adodc1.Recordset.RecordCount > 0 Then        MsgBox "编号存在"    ElseIf Adodc1.Recordset.RecordCount = 0 Then        '执行写数据表记录    End If 

读书人网 >VB

热点推荐