请问如何调用公共函数来完成向基本数据库添加记录,并用datagrid立即显示添加的记录。
'在模块中的公共函数如下
Option Explicit
Public Function connstring() As String
connstring = "Provider = Microsoft.Jet.OLEDB.4.0;data source=d:\电脑应用 \visual basic课程设计\employee.mdb "
End Function
Public Function executesql(ByVal sql As String) As ADODB.Recordset
Dim conn As ADODB.Connection
Dim rstmember As ADODB.Recordset
Set conn = New ADODB.Connection
conn.CursorLocation = adUseClient
conn.ConnectionString = connstring
conn.Open
Set rstmember = New Recordset
rstmember.Open sql, conn, 2, 3
Set executesql = rstmember
End Function
'在窗体中调用函数
Private Sub cmdAdd_Click()
Dim rs As ADODB.Recordset
Dim wt As ADODB.Recordset
Dim sqltxt As String
Set rs = New Recordset
sqltxt = "select * from employee order by 员工编号 "
Set rs = executesql(sqltxt)
rs.AddNew
.........
Set DataGrid1.DataSource = rs
.......
End sub
[解决办法]
rs.AddNew
.........
rs.update
Set DataGrid1.DataSource = executesql(sqltxt)
.......