有高手说先选择再插入,我用的是ADO,现在已经能插入了,代码如下:Dim Con As ADODB.Connection
    Dim rr As ADODB.Recordset
    Set Con = New ADODB.Connection
    strCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=result;Data Source=HZQJCZ;User Id=sa;Password=sa;"
    Con.Open strCon
    Set rr = New ADODB.Recordset
    '添加记录
    strsql = "select * from tiaoxiushoufei"
    rr.Open strsql, Con, adOpenKeyset, adLockOptimistic
    rr.AddNew
       
       rr!车牌号码 = Text20.Text
       rr!检测编号 = Text1.Text
       rr!调修费用 = Textfy.Text
       rr!日期 = Text34.Text
    rr.Update
    rr.Close
        Set rr = Nothing
       MsgBox "费用收取成功", vbOKOnly, "提示"怎么加语句,可以防止重复插入啊,不然最后结算统计时钱就多了,我需要用检测编号来判断是否重复,请高手呆板能够帮忙

解决方案 »

  1.   

    1,在数据库中将检测编号设置为主键,重复插入时会有错误提示
    2,con.execute "if (select count(*) from tiaoxiushoufei where 检测编号='"& text1.text &"')=0 insert into tiaoxiushoufei(车牌号码,检测编号,调修费用,日期) values('"& text20.text &"','"& text1.text &"',"& textfy.text &",'"& text34.text &"')"
      

  2.   

    设置主键倒是可以的前辈,我也设了,但是提示插入错误时太那个了,感觉有点丢人,嘿嘿,所以我想在插入时可以判断一下,也就是有人说的可以先 select再insert,但我没搞明白,所以麻烦你帮个忙,帮我把前面的程序里加几句可以不??就是判断的那句
      

  3.   

    Leftie 的第二个方法不就是你想要的效果吗?不过还可以分开来写,例如先select,如果count不等于0就msgbox一个信息,反之则插入数据!
      

  4.   

    这样试试。
        Dim Con     As ADODB.Connection
        Dim rr     As ADODB.Recordset
        Set Con = New ADODB.Connection
        Dim strsql As String
        
        strCon = "Provider=SQLOLEDB.1;Integrated   Security=SSPI;Persist   Security   Info=False;Initial   Catalog=result;Data   Source=HZQJCZ;User   Id=sa;Password=sa;"
        Con.Open strCon
        
        strsql = " If Exists (Select * From tiaoxiushoufei Where 车牌号码 = '" & Text20.Text & "') " _
                                & "   Update tiaoxiushoufei  " _
                                & "     Set 检测编号 = '" & Text1.Text & "', " _
                                & "         调修费用 = '" & Textfy.Text & "' , " _
                                & "         日期 = '" & Text34.Text & "'   " _
                                & " where 车牌号码 = '" & Text20.Text & "';" _
                                & " Else " _
                                & " Insert Into tiaoxiushoufei (车牌号码,检测编号,调修费用,日期) " _
                                & " Values ( " _
                                & "'" & Text20.Text & "', " _
                                & "'" & Text1.Text & "', " _
                                & "'" & Textfy.Text & "', " _
                                & "'" & Text20.Text & "') ;"
        Con.Execute strsql
        
        MsgBox 费用收取成功", vbOKOnly, "提示"
      

  5.   

    还是不行,能具体点吗??caofusheng 前辈的代码我拷贝回去了,提示出错,郁闷中
      

  6.   

        Dim Con     As ADODB.Connection
        Dim rs     As ADODB.Recordset
        Set Con = New ADODB.Connectionrs.open"select 检测编号 from tiaoxiushoufei where 检测编号='"&trim(text1)&"'",Con,   adOpenKeyset,   adLockOptimistic 
    if rs.recordcount>0 then
    '不执行插入
    MsgBox "编号已经存在", vbOKOnly, "提示"
    else
    '否则插入
    con.execute("insert into tiaoxiushoufei values('"&   text20.text   &"','"&   text1.text   &"',"&   textfy.text   &",'"&   text34.text   &"')")
    endif
      

  7.   


     
    Dim Con     As ADODB.Connection
    Dim Rs     As ADODB.Recordset
    Set Con = New ADODB.Connection
    Rs.Open "Select * from tiaoxiushoufei where 检测编号='"&   text1.text   &"'",Con,1,1
    If Rs.Eof then
    Rs.Close
    这里写插入代码 可用 con.Execute "insert into tablename(字段1,字段2,字段3) values(值1,值2,值3)"End if
      

  8.   

    Dim Con As New ADODB.Connection
    Dim Rs As New ADODB.Recordset
      

  9.   

    shuisjshuisj 和Plutoxkxu 哥,你俩给的代码都试了,都是提示我 对象变量或块变量没设置,显示问题在rs.open"select   检测编号   from   tiaoxiushoufei   where   检测编号='"&trim(text1)&"'",Con,       adOpenKeyset,       adLockOptimistic   
    这行有问题吗??
      

  10.   

    Dim   Con   As   New   ADODB.Connection 
    Dim   Rs   As   New   ADODB.Recordset 
    Con.Open "Provider=SQLOLEDB.1;Integrated   Security=SSPI;Persist   Security   Info=False;Initial   Catalog=result;Data   Source=HZQJCZ;User   Id=sa;Password=sa;" 
    Rs.open "select 检测编号 from tiaoxiushoufei where 检测编号='"&trim(text1)&"'",Con,adOpenKeyset,adLockOptimistic       
    if Rs.Recordcount<1  then
      Con.Execute   "insert into tablename(车牌号码,检测编号,调修费用,日期) values('"& text20.text &"','"& text1.text &"',"& textfy.text &",'"& text34.text &"')" 
    end if
    Rs.Close
    Set Rs =   Nothing
    Con.close
    Set Con=Nothing