执行以下SQL语句时出现“类型不匹配” 
txtsQl = "delete from bustab_cashincome where id=" & CLng(frmSell.msgList.TextMatrix(intcount, 1))
Set mrc = ExecuteSQL(txtsQl, Msgtext)
其中ID为在某ACCESS数据表的字段。类型为自动编号,但仍然可以删除该条记录。
MSGLIST为表格控件。CLng(frmSell.msgList.TextMatrix(intcount, 1))的取值正确。

解决方案 »

  1.   

    Access删除不是delete from的,而是delete * from的,中间有个*号的。
      

  2.   

    ExecuteSQL这是一个函数吧,在这个函数里有问题
      

  3.   

    Option Explicit'工程->引用->Microsoft ActiveX Data Objects 2.0 Library (后面为版本号)
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.RecordsetPrivate Sub Combo1_Click()
        cn.execute "delete from bustab_cashincome where id = " & frmSell.msgList.TextMatrix(intcount, 1)
    End SubPrivate Sub Form_Load()
        cn.ConnectionString = "DSN=SCM_ACCESS;pwd=ORIENTSCM;uid=ADMIN"
        cn.Open
        '查询字符串可以上这里查
        'http://www.connectionstrings.com/
    End SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    On Error Resume Next
        rs.Close
        Set rs = Nothing
        cn.Close
        Set cn = Nothing
    End Sub
      

  4.   

    SQL语句本来就是字符串,所以不用CLNG转换,你转换完之后 ,VB处理的时候还会自动的转换成STRING~~不过原因不在这,你把SQL语句输出来到查询分析器中执行一下,看看有没有问题,如果SQL语句正确,可能就是你执行的方式不对了,建议用EXECUTE来执行,它一般执行没有返回结果的SQL语句,记录集一般执行有返回结果的SQL语句!!
      

  5.   

    ExecuteSQL函数如下:其中ConnectString为连接字符串,它应该没有问题,我编的数据库程序全都用它的, huangjianyou(小健)说 的我试过了还是不行
    Public Function ExecuteSQL(ByVal SQL _
       As String, MsgString As String) _
       As ADODB.Recordset
    'executes SQL and returns Recordset
       Dim cnn As ADODB.Connection
       Dim rst As ADODB.Recordset
       Dim l As Integer
       Dim sTokens() As String
       
       On Error GoTo ExecuteSQL_Error
       If SQL = "" Then MsgBox ("SQL为空")
       sTokens = Split(SQL)
       Set cnn = New ADODB.Connection
       cnn.Open ConnectString
       If InStr("INSERT,DELETE,UPDATE", _
          UCase$(sTokens(0))) Then
          cnn.Execute SQL
          Set ExecuteSQL = cnn
          MsgString = sTokens(0) & _
             " query successful"
           Else
          Set rst = New ADODB.Recordset
          rst.Open Trim$(SQL), cnn, _
             adOpenKeyset, _
             adLockOptimistic
          'rst.MoveLast     'get RecordCount
          Set ExecuteSQL = rst
          MsgString = "查询到" & rst.RecordCount & " 条记录 "
              End If
    ExecuteSQL_Exit:
       Set rst = Nothing
       Set cnn = Nothing
       Exit Function
       
    ExecuteSQL_Error:
       MsgString = "查询错误: " & _
          Err.Description
          MsgBox (MsgString)
     Resume ExecuteSQL_Exit
    End Function
      

  6.   

    何必写个函数这么复杂:
    Dim cnn As new ADODB.Connection
    Cnn.ConnectionString =ConnectString
    cnn.Open 
    txtsQl = "delete * from bustab_cashincome where id=" & CLng(frmSell.msgList.TextMatrix(intcount, 1))    ' 这句话如果没问题的话
    cnn.Execute txtsQl 
    cnn.close
    set cnn=nothing
      

  7.   

    这样试试吧:
    txtsQl = "delete * from bustab_cashincome "& _
        "where id="& frmSell.msgList.TextMatrix(intcount, 1) &"
      

  8.   

    txtsQl = "delete * from bustab_cashincome "& _
        "where id='"& frmSell.msgList.TextMatrix(intcount, 1) &"'"
    加个单引号
      

  9.   

    错误在这个地方:Set ExecuteSQL = cnn
    ExecuteSQL是一个返回记录集的函数,怎么能给他赋cnn呢?
    改成Set ExecuteSQL = nothing