不能连续删除,删除第一个的时候可以,第二个就提示对象关闭时不允许操作
在模块里Option Explicit
Public cnn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public cnnstr As String
Public i As Integer
Public Function ExecuteSQL(ByVal sql As String) As ADODB.Recordset
'**********************************************************
'功能: 执行后台SQL语句,
'参数: SQL:SQL语句
'返回值: 如果是查询语句则返回记录集
'返回类型:记录集
'**********************************************************
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=itxrgw;Initial Catalog=db_SPJXC;Data Source=."
rs.Open Trim(sql), cnn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rs
End Function
Public Sub main() '定义一个公共主函数,用于连接数据库
If cnn.state = adStateOpen Then
cnn.Close
Else
cnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=itxrgw;Initial Catalog=db_SPJXC"
End If
End Sub
工具栏
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Key
Case "add" '添加
tlbState Toolbar1, True
Call MyAdd
Case "del" '删除
Set rs = ExecuteSQL("select * from 经手人表 where 经手人编号='" & Text1(0).Text & "'")
If rs.RecordCount > 0 Then
Call main
cnn.BeginTrans
cnn.Execute ("Delete 经手人表 where 经手人编号='" & Text1(0).Text & "'")
MsgBox "删除成功"
Set rs = ExecuteSQL("select * from 经手人表")
Set MSHFlexGrid1.DataSource = rs
End If
Case "save" '保存
Call main
cnn.BeginTrans
cnn.Execute ("insert into 经手人表 values('" & Text1(0).Text & "','" & Text1(1).Text & "','" & Text1(2).Text & "','" & Text1(3).Text & "','" & Text1(4).Text & "','" & Text1(5).Text & "')")
MsgBox "保存成功"
Set rs = ExecuteSQL("select * from 经手人表")
Set MSHFlexGrid1.DataSource = rs
MSHFlexGrid1.Refresh
tlbState Toolbar1, False
Case "cancel" ' 取消
tlbState Toolbar1, False
Call MyClear
MyFlag = False
' Case "find" ' 查询
' Mystr = InputBox("请输入要查询的货品名称", "企业进销存管理系统", "J000001")
' Adodc1.RecordSource = "select * from tb_in where in_numid ='" + Mystr + "'"
' Adodc1.Refresh
' Call JionBack
' Adodc1.RecordSource = "select * from tb_in"
' Adodc1.Refresh
' MyFlag = False
Case "close" '关闭
Unload Me
End Select
End Sub