Private Sub cmdAdd_Click()
   rs.AddNew
   rs.Fields.Item("图书编号").Value = Trim(txtNo.Text)
   rs.Fields.Item("图书作者").Value = Trim(txtAuthor.Text)
   rs.Fields.Item("图书价格").Value = FormatCurrency(Trim(txtPrice.Text))
   rs.Fields.Item("图书书名").Value = Trim(txtName.Text)
   rs.Fields.Item("图书类别").Value = Trim(cmbCategory.Text)
   rs.Update
   MsgBox "已经将该书添加到数据库!", vbInformation, "添加新书"
   txtNo.Text = txtNo.Text + 1
   txtAuthor.Text = ""
   txtPrice.Text = ""
   txtName.Text = ""
End Sub
这段代码用于添加新书,请问如下功能该如何实现?
1、当输入的图书编号与数据库中的重复时无法添加并给出提示
2、只有所有项都输入数据后才能添加成功否则提示数据不完整并要求输入完整的信息

解决方案 »

  1.   

    2、对每一个text进行判断是否空值
       是则退出!
      

  2.   

    2、
    像这样:
        If Text1.Text = "" Then MsgBox "text1不能为空!", vbQuestion: Exit Sub
        If Text2.Text = "" Then MsgBox "text2不能为空!", vbQuestion: Exit Sub
        If Text3.Text = "" Then MsgBox "text3不能为空!", vbQuestion: Exit Sub
        If Text4.Text = "" Then MsgBox "text4不能为空!", vbQuestion: Exit Sub
        If Text5.Text = "" Then MsgBox "text5不能为空!", vbQuestion: Exit Sub
      

  3.   


    on error goto myerr
        你的代码
    exit sub
    myerr:
    msgbox"错误!"
      

  4.   

    1)
    on error Goto A1
    *****
    A1:
    If Err.Number = -2147217900 Then
      msgbox "编号为"+Trim(txtNo.Text)+"图书已经在数据库中!",vbinformation,"Information"
    end if 
    主键重复的错误号为“-2147217900”出错就不添加退出过程就可以达到你的目的。
    2)
    if Text1.Text = "" or Text2.Text = "" or Text3.Text = "" or Text5.Text = "" then
     msgbox "数据不完整,请输入完整的信息!",vbInformation,"Information"
     text1.setfocus
    end if
      

  5.   

    修改后的代码如下:
    Private Sub cmdAdd_Click()
    On Error GoTo Error:
       rs.AddNew
       rs.Fields.Item("图书编号").Value = Trim(txtNo.Text)
       rs.Fields.Item("图书作者").Value = Trim(txtAuthor.Text)
       rs.Fields.Item("图书价格").Value = FormatCurrency(Trim(txtPrice.Text))
       rs.Fields.Item("图书书名").Value = Trim(txtName.Text)
       rs.Fields.Item("图书类别").Value = Trim(cmbCategory.Text)
       rs.Update
       MsgBox "已经将该书添加到数据库!", vbInformation, "添加新书"
       txtNo.Text = txtNo.Text + 1
       txtAuthor.Text = ""
       txtPrice.Text = ""
       txtName.Text = ""
       Exit Sub
    Error:
       If txtNo.Text = "" Then
          MsgBox "图书编号不能为空", vbInformation, "数据不完整"
          txtNo.SetFocus
       End If
       If Err.Number = -2147467259 Then
          MsgBox "编号为" & Trim(txtNo.Text) & "的图书信息已经存在,请重新输入", vbInformation, "编号重复"
          txtNo.SetFocus
       End If
       
       If cmbCategory.Text = "" Then
          MsgBox "图书类别不能为空", vbInformation, "数据不完整"
       End If
       If txtPrice.Text = "" Then
          MsgBox "图书价格不能为空", vbInformation, "数据不完整"
       End If
       If txtAuthor.Text = "" Then
          MsgBox "图书作者不能为空", vbInformation, "数据不完整"
       End If
       If txtName.Text = "" Then
          MsgBox "图书书名不能为空", vbInformation, "数据不完整"
       End If
    End Sub
    修改后2已实现,现在的问题在于1:不论输入的编号是否重复始终提示
    MsgBox "编号为" & Trim(txtNo.Text) & "的图书信息已经存在,请重新输入", 
    vbInformation, "编号重复",不知哪还有问题?
      

  6.   

    试一下这个:
    Private Sub cmdAdd_Click()
    On Error Resume Next:
       If txtno.Text = "" Then
          MsgBox "图书编号不能为空", vbInformation, "数据不完整"
          txtno.SetFocus
       Exit Sub
       End If
       
       
       If cmbCategory.Text = "" Then
          MsgBox "图书类别不能为空", vbInformation, "数据不完整"
       Exit Sub
       End If
       
       If txtPrice.Text = "" Then
          MsgBox "图书价格不能为空", vbInformation, "数据不完整"
       Exit Sub
       End If
       
       If txtAuthor.Text = "" Then
          MsgBox "图书作者不能为空", vbInformation, "数据不完整"
       Exit Sub
       End If
       
       If txtName.Text = "" Then
          MsgBox "图书书名不能为空", vbInformation, "数据不完整"
       Exit Sub
       End If
       
       '*********************
       '对重复的图书编号进行处理
       Dim con As ADODB.Connection
       Dim str_mdbpath As String
       Dim rst_number As ADODB.Recordset
       Set con = New ADODB.Connection
       Set rst_number = New ADODB.Recordset
       str_mdbpath = "Provider=Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=\\127.0.0.1\**.mdb;" _
    & "Persist Security Info=False;Jet oledb:database password=hy'ssonyericssont238"
        con.Open str_mdbpath
        rst_number.Open ("select 图书编号 from tab_table where  图书编号='" & txtno.Text & "'"), con, adOpenKeyset, adLockOptimistic
             If rst_number.RecordCount <> 0 Then
             MsgBox "该图书编号已存在!", , "提示"
             txt_imei.SetFocus
             Exit Sub
             End If
       '*****************************
       rs.Fields.item("图书编号").Value = Trim(txtno.Text)
       rs.Fields.item("图书作者").Value = Trim(txtAuthor.Text)
       rs.Fields.item("图书价格").Value = FormatCurrency(Trim(txtPrice.Text))
       rs.Fields.item("图书书名").Value = Trim(txtName.Text)
       rs.Fields.item("图书类别").Value = Trim(cmbCategory.Text)
       rs.Update
       MsgBox "已经将该书添加到数据库!", vbInformation, "添加新书"
       txtno.Text = txtno.Text + 1
       txtAuthor.Text = ""
       txtPrice.Text = ""
       txtName.Text = ""
       Exit Sub   
    End Sub