对象关闭时不允许操作
1、Public Sub main() '定义一个公共主函数,用于连接数据库
cnnstr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;PWD=itxrgw;Initial Catalog=db_SPJXC;Data Source=."
cnn.Open (cnnstr)
End Sub
2、
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Key
Case "add" '添加
Case "del" '删除
Case "save" '保存
Call main
cnn.Execute ("insert into 经手人表 values('" & Text1(0).Text & "','" & Text1(1).Text & "','" & Text1(2).Text & "','" & Text1(3).Text & "','" & Text1(4).Text & "','" & Text1(5).Text & "')")
Set rs = ExecuteSQL("select * from 经手人表")
Set DataGrid1.DataSource = rs
DataGrid1.Refresh
Set rs = Nothing
cnn.Close
rs.Close
Case "cancel" ' 取消
' Case "find" ' 查询
' Case "close" '关闭
End Select
End Sub
3、如果我要程序随时执行cnn.execute 模块需要怎么设置
像执行查询一下,随时调用 ExecuteSQL(sql)。
Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset
Dim rs As New ADODB.Recordset
Dim cnn As New ADODB.Connection
cnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=itxrgw;Initial Catalog=db_SPJXC"
rs.Open Trim(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rs
End Function