【问题标题】:How to add new record with primary key constraint VB6.0 & SQL Server如何使用主键约束 VB6.0 & SQL Server 添加新记录
【发布时间】:2012-08-18 17:05:21
【问题描述】:

我想创建一个消息框,只要txt_unit 等于数据库中的unit_no,就会触发该消息框。这是在数据库中插入新数据的过程中。我正在使用 VB6 和 SQL Server 2005。
这是我的代码:

If txt_unit.Text = 'same unit number that exist in the DB' Then
MsgBox "Duplicate Record", vbCritical, "Duplicate"  
txt_unit.Text = ""  
txt_unit.SetFocus

这将在数据输入期间为用户节省时间。
提前致谢

【问题讨论】:

  • 为什么不查询数据库并获取值。然后比较一下?或者您可以通过将文本框的值传递给查询本身来进行比较!

标签: sql-server-2005 vb6


【解决方案1】:

浏览网页寻找答案后,我看到了解释 VB6 中的错误处理的网站。
我现在终于可以继续前进了。代码如下:

On Error GoTo err_CmdAdd_Click

con.Open _
    "Provider = sqloledb;" & _  
    "Data Source=server;" & _  
    "Initial Catalog=database;" & _  
    "User ID=username;" & _  
    "Password=password;"


If img_edit.BorderStyle = 1 Then

Set rs = con.Execute("insert into a_owner values('" & txt_unit.Text & "', " _
& " '" & txt_tower.Text & "'  )")

MsgBox "Record added successfully...", vbInformation

ado1.Refresh

exit_err_CmdAdd_Click:  
Exit Sub

err_CmdAdd_Click:

MsgBox "Duplicate Record!", vbCritical, "Duplicate"
txt_unit.Text = ""  
txt_unit.SetFocus

End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    相关资源
    最近更新 更多