Private Sub Command4_Click() '修改
If Command4.Caption = "修改" Then
On Error GoTo ES
'将datagrid选中的数据赋值到text和combo里
Text7.Text = DataGrid1.Columns(2).CellText(DataGrid1.Book)
Text1.Text = Mid(DataGrid1.Columns(3).CellText(DataGrid1.Book), 6, 5)
Text2.Text = Left(DataGrid1.Columns(4).CellText(DataGrid1.Book), Len(DataGrid1.Columns(4).CellText(DataGrid1.Book)) - 3)
Text3.Text = Mid(DataGrid1.Columns(5).CellText(DataGrid1.Book), 6, 5)
Text4.Text = Left(DataGrid1.Columns(6).CellText(DataGrid1.Book), Len(DataGrid1.Columns(6).CellText(DataGrid1.Book)) - 3)
Text8.Text = DataGrid1.Columns(0).CellText(DataGrid1.Book)
Combo1.Text = DataGrid1.Columns(1).CellText(DataGrid1.Book)
Command4.Caption = "确定"
Debug.Print DataGrid1.Row
ES:
Else
Command4.Caption = "修改"
Dim RS As Recordset
Dim tpSQL As String
tpSQL = "date=cdate('" & DataGrid1.Columns(0) & "') and date1=cdate('" & DataGrid1.Columns(3) & "')"
tpSQL = tpSQL & " and time1=cdate('" & DataGrid1.Columns(4) & "') and id=" & DataGrid1.Columns(2)
tpSQL = "select * from 停时统计 where " & tpSQL
With Adodc1
.RecordSource = tpSQL
.Refresh
End With
Set RS = Adodc1.Recordset
If RS.EOF Then Exit Sub
With RS
.Fields("date") = CDate(Text8.Text)
.Fields("ycqk") = Combo1.Text
.Fields("id") = CInt(Text7.Text)
.Fields("date1") = CDate(Text1.Text)
.Fields("time1") = CDate(Text2.Text)
.Fields("date2") = CDate(Text3.Text)
.Fields("time2") = CDate(Text4.Text)
.Update
End With
RS.Close
End If
'刷新adodc和datagrid
With Adodc1
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 停时统计 where date = cdate('" & Text8.Text & "') and ycqk = '" & Combo1.Text & "' order by id"
Adodc1.Refresh
DataGrid1.Refresh
End With
End Sub请高手们帮我看一下上面的程序有没有什么问题,运行时能修改数据,但是ID号不是我改的那个ID号,经常默认为1或2,老是把第1条或第2条记录覆盖了。急盼回复!!
If Command4.Caption = "修改" Then
On Error GoTo ES
'将datagrid选中的数据赋值到text和combo里
Text7.Text = DataGrid1.Columns(2).CellText(DataGrid1.Book)
Text1.Text = Mid(DataGrid1.Columns(3).CellText(DataGrid1.Book), 6, 5)
Text2.Text = Left(DataGrid1.Columns(4).CellText(DataGrid1.Book), Len(DataGrid1.Columns(4).CellText(DataGrid1.Book)) - 3)
Text3.Text = Mid(DataGrid1.Columns(5).CellText(DataGrid1.Book), 6, 5)
Text4.Text = Left(DataGrid1.Columns(6).CellText(DataGrid1.Book), Len(DataGrid1.Columns(6).CellText(DataGrid1.Book)) - 3)
Text8.Text = DataGrid1.Columns(0).CellText(DataGrid1.Book)
Combo1.Text = DataGrid1.Columns(1).CellText(DataGrid1.Book)
Command4.Caption = "确定"
Debug.Print DataGrid1.Row
ES:
Else
Command4.Caption = "修改"
Dim RS As Recordset
Dim tpSQL As String
tpSQL = "date=cdate('" & DataGrid1.Columns(0) & "') and date1=cdate('" & DataGrid1.Columns(3) & "')"
tpSQL = tpSQL & " and time1=cdate('" & DataGrid1.Columns(4) & "') and id=" & DataGrid1.Columns(2)
tpSQL = "select * from 停时统计 where " & tpSQL
With Adodc1
.RecordSource = tpSQL
.Refresh
End With
Set RS = Adodc1.Recordset
If RS.EOF Then Exit Sub
With RS
.Fields("date") = CDate(Text8.Text)
.Fields("ycqk") = Combo1.Text
.Fields("id") = CInt(Text7.Text)
.Fields("date1") = CDate(Text1.Text)
.Fields("time1") = CDate(Text2.Text)
.Fields("date2") = CDate(Text3.Text)
.Fields("time2") = CDate(Text4.Text)
.Update
End With
RS.Close
End If
'刷新adodc和datagrid
With Adodc1
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 停时统计 where date = cdate('" & Text8.Text & "') and ycqk = '" & Combo1.Text & "' order by id"
Adodc1.Refresh
DataGrid1.Refresh
End With
End Sub请高手们帮我看一下上面的程序有没有什么问题,运行时能修改数据,但是ID号不是我改的那个ID号,经常默认为1或2,老是把第1条或第2条记录覆盖了。急盼回复!!
Text1.Text =
Text2.Text =
Text3.Text =
Text4.Text =
Text7.Text = Adodc1.Recordset!字段7
Text1.Text = Adodc1.Recordset!字段1
Text2.Text = Adodc1.Recordset!字段2
Text3.Text = Adodc1.Recordset!字段3
Text4.Text = Adodc1.Recordset!字段4