我用VB编一个简单的数据库操作软件,界面上有DATAGRID控件,用于显示数据库中的数据,界面上还有一个按钮,用于删除DATAGRID上的一条记录,按钮点击事件中先执行"DELETE FROM DATANAME WHERE NO='DATANO'",然后执行ADODC1.FRESH,但DATAGRID控件中内容无变化,不知为何?请各位高手指点一下,多谢!

解决方案 »

  1.   

    ADODC1.update或者你重新  select * from table 
    再显示一下
      

  2.   

    多谢,但ADODC1没有方法UPDATE,请指教!
      

  3.   

    哦,忘了你用的是adodc了,为什么不用ado那,你试试吧,好用的
    记得要引用adoDim WithEvents adoPrimaryRS As Recordset '数据库连接对象
    Private Sub Command1_Click()
       'strsql 是你的查询语句 你可以order by 排序的字段
       'strsql="select xh as 学号 from 表" 这里的学号就是你datagrid中列的标题   strsql="select 字段 from 表"
       Set Db = New Connection
       Db.CursorLocation = adUseClient
       '下面的连接数据字符串你要修改一下
       Db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\计划管理系统.mdb;Persist Security Info=False"
       Set adoPrimaryRS = New Recordset
       adoPrimaryRS.Open strsql, Db, adOpenStatic, adLockOptimistic
       Set DataGrid1.DataSource = adoPrimaryRS
    end sub在说datagrid控件不需要用sql语句也可以删除指定的记录选中一条记录,然后按键盘键delete就可以删除这条记录
      

  4.   

    dim conn as new adodb.connection 
    dim rs as new adodo.recordset
    private sub form_load()
       conn.cursorlocation =0
       dim rs as new adodo.recordset
       connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path  & "\DatabaseName.mdb;Persist Security Info=False"
       strsql="select * from tableName where fieldName=condition order by fieldName"
       if conn.state<>0 then conn.close
       conn.open connstr
       if rs.state<>0 then rs.close
       rs.open strsql,conn,3,3
       if rs.eof then 
       msgbox "no wanted record exist"
       rs.close 'release the data object
       set rs=nothing
       conn.close
       set conn=nothing
    else
       set datagrid1.datasource=rs
       datagrid1.refresh
    endif
    end sub
    'delete record from datagrid1
    private sub command1_click()
       if rs.recordcount=0 then
           msgbox "u can`t delete record 'cos none from rs"
           exit sub
       endif
       rs.delete
       datagrid1.refresh
    end sub
      

  5.   

    有点奇怪,我试的时候,需要刷新两次,否则要点两次按钮才有效果,你也可以试试:delete from table .....
    Adodc1.Refresh
    Adodc1.Refresh
      

  6.   

    我的代码如下,不知那里出错:
    Private Sub Cmd3_Click()'界面上的删除按钮事件函数
    Dim TmpId As String
    Dim Pos As Integer
    If Adodc1.Recordset.EOF = True Then
    MsgBox "Please choose!"
    Exit Sub
    End If
    Pos = Adodc1.Recordset.AbsolutePosition
    TmpId = Trim(Adodc1.Recordset.Fields(0))
    objBookInfo.Delete (TmpId)
    Call adoBinfoRefresh
    If Pos - 2 >= 0 Then
    Adodc1.Recordset.Move Pos - 2
    Else
    If Pos - 1 < Adodc1.Recordset.RecordCount Then
    Adodc1.Recordset.Move Pos - 1
    End If
    End If
    End Sub
    Private Sub adoBinfoRefresh()'更新界面函数
    Adodc1.RecordSource = "Select * From BookInfo"
    Adodc1.Refresh
    With DataGrid1
    .Columns(0).Width = 800
    .Columns(1).Width = 3600
    .Columns(2).Width = 1000
    .Columns(3).Width = 1400
    .Columns(4).Width = 1600
    End With
    End Sub
    Public Sub Delete(ByVal paraBNo As String)'表类中的删除记录函数
    SQLStmt = "DELETE FROM BookInfo WHERE BookNo =' Trim(paraBNo)'"
    SQLExt SQLStmt
    End Sub
    Private IsConnect As Boolean
    Private cnn As ADODB.Connection
    Private rs As ADODB.Recordset
    Public Sub DBConnect()
    If IsConnect = True Then
    Exit Sub
    End If
    Set cnn = New ADODB.Connection
    cnn.ConnectionString = CONSTR
    cnn.Open
    If cnn.State <> adStateOpen Then
    MsgBox "Connection is failed!"
    End
    End If
    IsConnect = True
    End Sub
    Public Sub DBDisconnect()
    If IsConnect = False Then
    Exit Sub
    End If
    cnn.Close
    Set cnn = Nothing
    IsConnect = False
    End Sub
    Public Sub SQLExt(ByVal SQLStmt As String)
    Dim cmd As Command
    Set cmd = New ADODB.Command
    DBConnect
    Set cmd.ActiveConnection = cnn
    cmd.CommandText = SQLStmt
    cmd.Execute
    Set cmd = Nothing
    DBDisconnect
    End Sub
    请各位高手指教!