主题:Datagrid数据刷新难题,请高手!!
用以下代码连接数据库。当保存时,刚添加的数据不是立即在DATAGRID中出现,而是在第二次开FORM1时才显示刚增加的记录。要怎么样刷新记录呢?
谢谢详见附件!!
Private Server As String
Private User As String
Private Passwd As String
Private Dbname As String
Public Dbconn As New Connection
'Public dbconn As New Connection
Public Sub OpenConn()
connect:
On Error GoTo connerror
Dbconn.Open
Dbconn.CursorLocation = adUseClient
If Err.Number <> 0 Then
connerror:
Dim frm As New frm_DSConfig
frm.Show vbModal
Dbconn.ConnectionString = frm.connectionstr
Unload frm
GoTo connect
End If
End Sub
Public Sub CloseConn()
disconnect:
On Error Resume Next
Dbconn.Close
End Sub
Private Sub Class_Initialize()
Dim iniFilename As String
iniFilename = App.Path + "\bel.ini"
Server = iniaccess.readproperty(iniFilename, "DataSource", "Server")
User = tptool.decode(readproperty(iniFilename, "DataSource", "user"))
Passwd = tptool.decode(iniaccess.readproperty(iniFilename, "DataSource", "passwd"))
Dbname = iniaccess.readproperty(iniFilename, "DataSource", "dbname")
Me.Dbconn.ConnectionString = "Driver={sql server};pwd=" & Passwd & ";uid=" & User & ";database=" & Dbname & ";server=" & Server
sConnStr = Me.Dbconn.ConnectionString & ";dsn =" & Server & ""
'sConnStr = "driver={sql server};Data Source=" & Server & "; uid=" & user & ";PWD=" & passwd & ";DSQ=" & dbname
End Sub
在FORM1中:
Private Sub Form_Load()
'Dim Rsdb As New ADODB.Recordset
'qrySql = "select * from commoditylist"
' Rsdb.CursorLocation = adUseClient
' Rsdb.Open qrySql, DataEnv.Dbconn, adOpenDynamic, adLockReadOnly
' Set txt_belId.DataSource = Rsdb
' Set DataGrid1.DataSource = Rsdb
Dim rsTemp As New ADODB.Recordset
Dim strSql As String
Dim rsLeft As Recordset
Dim rsBottom As Recordset
strSql = "select * from commoditylist"
rsTemp.Open strSql, DataEnv.Dbconn, adOpenStatic, adLockReadOnly, adCmdText
DgMenu strSql, DataGrid1, rsLeft
If rsLeft.RecordCount = 0 Then
If rsButtom.State = 1 Then
rsButtom.Close
End If
End If
End Sub
谢谢详见附件!!
Private Server As String
Private User As String
Private Passwd As String
Private Dbname As String
Public Dbconn As New Connection
'Public dbconn As New Connection
Public Sub OpenConn()
connect:
On Error GoTo connerror
Dbconn.Open
Dbconn.CursorLocation = adUseClient
If Err.Number <> 0 Then
connerror:
Dim frm As New frm_DSConfig
frm.Show vbModal
Dbconn.ConnectionString = frm.connectionstr
Unload frm
GoTo connect
End If
End Sub
Public Sub CloseConn()
disconnect:
On Error Resume Next
Dbconn.Close
End Sub
Private Sub Class_Initialize()
Dim iniFilename As String
iniFilename = App.Path + "\bel.ini"
Server = iniaccess.readproperty(iniFilename, "DataSource", "Server")
User = tptool.decode(readproperty(iniFilename, "DataSource", "user"))
Passwd = tptool.decode(iniaccess.readproperty(iniFilename, "DataSource", "passwd"))
Dbname = iniaccess.readproperty(iniFilename, "DataSource", "dbname")
Me.Dbconn.ConnectionString = "Driver={sql server};pwd=" & Passwd & ";uid=" & User & ";database=" & Dbname & ";server=" & Server
sConnStr = Me.Dbconn.ConnectionString & ";dsn =" & Server & ""
'sConnStr = "driver={sql server};Data Source=" & Server & "; uid=" & user & ";PWD=" & passwd & ";DSQ=" & dbname
End Sub
在FORM1中:
Private Sub Form_Load()
'Dim Rsdb As New ADODB.Recordset
'qrySql = "select * from commoditylist"
' Rsdb.CursorLocation = adUseClient
' Rsdb.Open qrySql, DataEnv.Dbconn, adOpenDynamic, adLockReadOnly
' Set txt_belId.DataSource = Rsdb
' Set DataGrid1.DataSource = Rsdb
Dim rsTemp As New ADODB.Recordset
Dim strSql As String
Dim rsLeft As Recordset
Dim rsBottom As Recordset
strSql = "select * from commoditylist"
rsTemp.Open strSql, DataEnv.Dbconn, adOpenStatic, adLockReadOnly, adCmdText
DgMenu strSql, DataGrid1, rsLeft
If rsLeft.RecordCount = 0 Then
If rsButtom.State = 1 Then
rsButtom.Close
End If
End If
End Sub