Option Explicit
Private conn As ADODB.Connection
Private rs As ADODB.Recordset
Private strSQL As String
Private strCon As String
Private Sub cmdOK_Click()
If Me.txtName = "" Then
MsgBox "名称不能空"
Exit Sub
End If
If Me.txtPsw = "" Then
MsgBox "密码不能空"
Exit Sub
End If
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\TestDataGrid.mdb"
strSQL = "insert into [user] values ('" & Me.txtName & "','" & Me.txtPsw & "')"
Set conn = New ADODB.Connection
With conn
.Open strCon
.Execute strSQL
.Close
End With
'更新DataGrid1中的数据
'------------------------------
'这样OK!
'------------------------------
MsgBox "添加成功!"
Me.Adodc2.Refresh
'------------------------------
'这样不行
'------------------------------
' strSQL = "select * from [user] "
' Set rs = New ADODB.Recordset
' rs.Open strSQL, conn
' Set DataGrid1.DataSource = rs
' DataGrid1.Refresh
Set conn = Nothing
Set rs = Nothing
End Sub