我设置了一个添加按钮,点击按钮进行添加操作,执行完INSERT的SQL语句后,再执行一次DataGrid的数据绑定操作,但在绑定的过程中,我用断点调试发现,绑定记录集后自动会引发RowColChange事件,同时在RowColChange事件中我查找的列就找不到了。
这个是界面:相关代码:
RowColChange事件,用户每点击一次单元格,查询相关记录字段,填充TextBox控件Private Sub AutoList_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    Command2.Enabled = False
    OpenConn
    Dim AutoID As Integer
    On Error Resume Next
    AutoID = AutoList.Columns("编号").CellText(AutoList.Book)    '这行用了On Error处理,如果不处理会出错
    Dim Rs As New ADODB.Recordset
    Set Rs = Conn.Execute("SELECT * FROM auto_Auto WHERE AutoID = " & AutoID)
    OwnerID.BoundText = Rs("OwnerID")
    Brand.Text = Rs("Brand")
    Factory.Text = Rs("Factory")
    Model.Text = Rs("Model")
    Exit Sub
End Sub
insert/update/del操作
Private Sub Command2_Click()
    Dim Sql As String
    Sql = "INSERT INTO auto_Auto(Brand,Model,Factory,OwnerID) VALUES ('" & Brand.Text & "','" & Model.Text & "','" & Factory.Text & "'," & OwnerID.BoundText & ")"
    OpenConn
    Conn.Execute (Sql)
    BindData
    MsgBox "添加完成", vbOKOnly + vbExclamation, "完成"
End SubPrivate Sub Command3_Click()
    Dim AutoID As Integer
    AutoID = AutoList.Columns("编号").CellText(AutoList.Book)
    Dim Sql As String
    Sql = "UPDATE auto_Auto SET Brand = '" & Brand.Text & "',Model = '" & Model.Text & "',Factory = '" & Factory.Text & "',OwnerID = " & OwnerID.BoundText & " WHERE AutoID = " & AutoID & ""
    OpenConn
    Conn.Execute (Sql)
    BindData
    MsgBox "编辑完成", vbOKOnly + vbExclamation, "完成"
End SubPrivate Sub Command4_Click()
    Dim AutoID As Integer
    AutoID = AutoList.Columns(0).CellText(AutoList.Book)
    Dim Sql As String
    OpenConn
    Conn.Execute ("DELETE FROM auto_Auto WHERE AutoID = " & AutoID)
    BindData
    MsgBox "删除完成", vbOKOnly + vbExclamation, "完成"
End Sub
DataGrid绑定
Private Sub BindData()
    '绑定DataGrid
    Dim Rs1 As New ADODB.Recordset
    Sql = "SELECT     dbo.auto_Auto.AutoID, dbo.auto_Owner.OwnerName, dbo.auto_Auto.Brand, dbo.auto_Auto.Factory, dbo.auto_Auto.Model FROM   dbo.auto_Auto INNER JOIN                      dbo.auto_Owner ON dbo.auto_Auto.OwnerID = dbo.auto_Owner.OwnerID"
    Rs1.Open Sql, Conn, 1, 1
    Set AutoList.DataSource = Rs1
    AutoList.Columns(0).Width = 121
    AutoList.Columns(0).Caption = "编号"
    AutoList.Columns(1).Width = 120
    AutoList.Columns(1).Caption = "车主姓名"
    AutoList.Columns(2).Width = 139
    AutoList.Columns(2).Caption = "品牌"
    AutoList.Columns(3).Width = 132
    AutoList.Columns(3).Caption = "型号"
    AutoList.Columns(4).Width = 195
    AutoList.Columns(4).Caption = "生产厂家"
End Sub
主要代码和界面都在上面了,我就是想问一下,当添加或编辑时,在RowColChange事件中只能用On error处理么,有没有别的办法呢?

解决方案 »

  1.   

    本帖最后由 bcrun 于 2013-07-09 10:10:33 编辑
      

  2.   

    我是要在点击Datagrid上的单元格的时候,把这个记录的各字段值放到控件中让用户修改,修改完成再点击编辑按钮完成编辑,可能我的思路不对吧
      

  3.   


    Private Sub BindData()
        '绑定DataGrid
        Dim Rs1 As New ADODB.Recordset
        Private Sub BindData()
        '绑定DataGrid
        Dim Rs1 As New ADODB.Recordset
        Sql = "SELECT A.AutoID As 编号, B.OwnerName As 车主姓名, A.Brand As 品牌, A.Factory As 生产厂家, A.Model As 型号 FROM dbo.auto_Auto As A INNER JOIN dbo.auto_Owner As B ON A.OwnerID = B.OwnerID"
        Rs1.Open Sql, Conn, 1, 1
        Set AutoList.DataSource = Rs1
    End SubPrivate Sub Form_Load()
        AutoList.Columns(0).Width = 121
        AutoList.Columns(1).Width = 120
        AutoList.Columns(2).Width = 139
        AutoList.Columns(3).Width = 132
        AutoList.Columns(4).Width = 195
    End Sub