请教下DataGrid无法同步刷新的问题,见下图。原意是左边点“保存”时插入数据,同时右边刷新datagrid显示刚插入的数据,但在点击运行时,第一次点保存所插入的数据在DataGrid中无法显示,插入第二组数据时才显示第一组数据,依此类推,最后一次点“保存”时的数据无法显示出来。
下面贴上代码,麻烦各位帮忙看下,先谢过了!
Public conn As New ADODB.Connection, rs As New ADODB.Recordset
Public C, DJ, SQLStr, i, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, insertSQL, ReSQL
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
SendKeys "{tab}"
End If
End Sub
Private Sub Form_Load()
C = App.Path & "\diamond.mdb"
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & C & ";Persist Security Info=False"
conn.Open
rs.CursorLocation = adUseClient
SQLStr = "select mc from dw where fl='gs'"
rs.Open SQLStr, conn, 3, 1
'combo读入数据库中的数据
If Not rs.BOF Then
For i = 0 To rs.RecordCount - 1
Combo1.AddItem (rs(0).Value)
rs.MoveNext
Next i
End If
rs.Close
SQLStr = "select mc from dw where fl='js'"
rs.Open SQLStr, conn, 3, 1
If Not rs.BOF Then
For i = 0 To rs.RecordCount - 1
Combo2.AddItem (rs(0).Value)
rs.MoveNext
Next i
End If
rs.Close
'预设datagrid的列宽
DataGrid1.Columns(0).Width = 1300
DataGrid1.Columns(1).Width = 1300
DataGrid1.Columns(2).Width = 1300
DataGrid1.Columns(3).Width = 1300
DataGrid1.Columns(4).Width = 1300
DataGrid1.Columns(5).Width = 1300
DataGrid1.Columns(6).Width = 1300
DataGrid1.Columns(7).Width = 1300
DataGrid1.Columns(8).Width = 1300
DataGrid1.Columns(9).Width = 1300
DataGrid1.Columns(10).Width = 1300
DataGrid1.Columns(11).Width = 1300
' DJ是本次插入数据的编号
DJ = "gs" + Format(DDate.Value, "yyyymmdd") + Trim(Left(Right(Now(), 8), 2)) + Trim(Left(Right(Now(), 8), 2)) + Trim(Right(Now(), 2))
ReSQL = "select * from st where djbh='" & DJ & "'"
Adodc1.RecordSource = ReSQL
Adodc1.Refresh
End Sub
Private Sub CmdADD_Click()
If Combo1.Text = "" Or Combo2.Text = "" Or Text2.Text = "" Or Text3.Text = "" Or Text7.Text = "" Or Text8.Text = "" Then
MsgBox ("输入错误!")
Combo1.SetFocus
Exit Sub
End If
A2 = Trim(DDate.Value)
A3 = Trim(Combo1.Text)
A4 = Trim(Combo2.Text)
A5 = Trim(Text1.Text)
A6 = Trim(Text2.Text)
A7 = Trim(Text3.Text)
A8 = Trim(Text4.Text)
A9 = Trim(Text5.Text)
A10 = Trim(Text6.Text)
A11 = Trim(Text7.Text)
A12 = Trim(Text8.Text)
insertSQL = "insert into st values('" & DJ & "','" & A2 & "','" & A3 & "','" & A4 & "','" & A5 & "','" & A6 & "','" & A7 & "','" & A8 & "','" & A9 & "','" & A10 & "','" & A11 & "','" & A12 & "')"
conn.Execute insertSQL
Combo1.SetFocus
Text1.Text = ""
Text2.Text = "0"
Text3.Text = "0"
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = "0"
Text8.Text = ""
Dim ReFreshSQL As String
ReFreshSQL = "select * from st where djbh='" & DJ & "'"
Adodc1.RecordSource = ReSQL
Adodc1.Refresh
' DataGrid1.Refresh
End Sub
Private Sub CmdExit_Click()
conn.Close
Set conn = Nothing
Unload Me
End Sub
……
下面是判断text文本框的输入是否正确的,略过……
再次感谢!
[此贴子已经被作者于2007-10-13 19:17:55编辑过]