vb怎么用mshflexgrid实时删除、清空数据库

weixin_46503551 2020-03-25 09:37:56
请教各位大神们,我这个为什么总是删除不了数据库的内容,只是把当前mshflexgrid表格上的数据删了,下次再查询的时候原来删除的内容依旧还在?我用了rs.Update MSHFlexGrid1.Refresh都没有反应,要么就是报错对象关闭时不允许操作。
还有清空的话清空不了mshflexgrid表格,只有下次再打开这个表格或者再次查询的时候才显示把表格清空了??

Private Sub Command2_Click()
'连接数据库
If conn.State = 0 Then
conn.ConnectionString = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" & _
App.Path & "\设计计算.mdb;Persist Security Info=False" '连接数据库的驱动字符串并打开数据库
conn.Open
End If

'删除
Dim sql As String
Set rs = New ADODB.Recordset

Set rs.ActiveConnection = conn
sql = "delete from [设计计算] where [管段编号]=' " & MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0) & " ' "
conn.Execute sql
MSHFlexGrid1.RemoveItem MSHFlexGrid1.Row
rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic

Dim i%
For i = 1 To MSHFlexGrid1.rows - 1
MSHFlexGrid1.TextMatrix(i, 0) = i
Next
conn.Close
Set conn = Nothing
End Sub

'清空
Private Sub Command12_Click()
Dim sql$
If conn.State = 0 Then
conn.ConnectionString = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" & _
App.Path & "\设计计算.mdb;Persist Security Info=False" '连接数据库的驱动字符串并打开数据库
conn.Open
End If

sql = " Delete from [设计计算] "
conn.Execute sql
MsgBox "已清空"
conn.Close
End Sub
...全文
544 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_46503551 2020-04-02
  • 打赏
  • 举报
回复
我用了你的代码没有任何反应,数据库也没有删除
weixin_46503551 2020-04-02
  • 打赏
  • 举报
回复
非常感谢楼上的小姐姐
weixin_46503551 2020-04-02
  • 打赏
  • 举报
回复
删除的第三行 弹出的是 数据库跟mshflexgrid都没删除
show147 2020-04-02
  • 打赏
  • 举报
回复
刚才你试时,数据库里一条数据都没有删除嘛,按理说应该删除了一条呀~
show147 2020-04-02
  • 打赏
  • 举报
回复
在 rs.find "管段编号='"& MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1) & "'" 后面放个 msgbox MsgBox rs.Fields("管段编号") 我再看看他能不能定位到要删除的数据
show147 2020-04-02
  • 打赏
  • 举报
回复
程序是能定位到记录的,但是你的数据库里编号有重复的字段 他查找出了三条,rs.delete 就出问题了 你把你的编号改成 100 102 103 等这种没有重复的再试试 我这边试了程序试了是没有问题能删除的
weixin_46503551 2020-04-02
  • 打赏
  • 举报
回复
假设我删的是第一行,然后依次弹出 但是mshflexgrid跟mdb数据库都没有删除
show147 2020-04-02
  • 打赏
  • 举报
回复
在 rs.find "管段编号='"& MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1) & "'" 后面放个 msgbox rs.RecordCount msgbox MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1) 看看程序能不能找到 "管段编号='"& MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1) & "'" 的记录 及MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1)是不是要删的编号
weixin_46503551 2020-04-02
  • 打赏
  • 举报
回复
正式运行的时候管段编号这个字段不会为空的,我这是只是调试运行一下试试有没有毛病
weixin_46503551 2020-04-02
  • 打赏
  • 举报
回复
可是数据库还是没删除
show147 2020-04-02
  • 打赏
  • 举报
回复
你这个管段编号 字段 很多都是空白字段 如果要删除这些字段也会出问题哦,我还以为你管段编号是个无重复值并且不为空的字段呢,你以编号来删数据感觉要出问题
show147 2020-04-02
  • 打赏
  • 举报
回复
把rs.find "管段编号='"& MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0) & "'" 改成 rs.find "管段编号='"& MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1) & "'" 因为rs没有找到管段编号为4的记录,所以指针没有指向的记录,当然要报错
weixin_46503551 2020-04-02
  • 打赏
  • 举报
回复
显示的是在rs.delete处报错
weixin_46503551 2020-04-02
  • 打赏
  • 举报
回复
弹出的竟然不是管段编号,是第一列序号然后点完确定后又报错了
show147 2020-04-02
  • 打赏
  • 举报
回复
在if rs.RecordCount>0 then rs.Delete前面加一句 msgbox MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0) 看看弹出的是不是你要删除的管段编号
show147 2020-04-02
  • 打赏
  • 举报
回复
把你的界面截个图看看嘛
show147 2020-04-02
  • 打赏
  • 举报
回复
你确定这个MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0) 对应的就是 你数据库中的 管段编号 字段吗?
show147 2020-04-01
  • 打赏
  • 举报
回复
把程序中删除里面
Set rs.ActiveConnection = conn
sql = "delete from [设计计算] where [管段编号]=' " & MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0) & " ' "
conn.Execute sql
MSHFlexGrid1.RemoveItem MSHFlexGrid1.Row
rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic
Dim i%
For i = 1 To MSHFlexGrid1.rows - 1
MSHFlexGrid1.TextMatrix(i, 0) = i
Next

这段改为
sql ="select * from 设计计算"
rs.Open sql, conn, adOpenKeyset, adLockBatchOptimistic
rs.MoveFirst
rs.find "管段编号='"& MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0) & "'"
if rs.RecordCount>0 then rs.Delete
rs.update
MSHFlexGrid1.Refresh
Set rs = Nothing
含全套代码+数据库文件 mdb private sub txtoldpassword lostfocus if oldpassword <> txtoldpassword text then msgbox "密码不正确 " "密码错误" txtoldpassword setfocus end if end sub private sub form unload cancel as integer set mclsmidtier nothing lusersrs close userrs close set lusersrs nothing set userrs nothing end sub private sub startpos cmdadduser enabled true cmddeleteuser enabled true lstusers tabindex 0 txtusername tabindex 1 txtpassword tabindex 2 txtreenter tabindex 3 txtoldpassword tabindex 4 cmdadduser tabindex 6 cmddeleteuser tabindex 7 cmdchangepassword tabindex 8 lblpassword top 600 txtpassword top 600 lblreenter top 960 txtreenter top 960 lbloldpassword top 1320 txtoldpassword 1320 lblnewpassword top 1680 lstusers enabled true txtusername enabled true lblpassword visible true txtpassword visible true lblnewpassword visible false lblreenter visible false txtreenter visible false lbloldpassword visible false txtoldpassword visible false end sub private sub changepos cmdadduser enabled false cmddeleteuser enabled false lblpassword visible false txtpassword visible true txtpassword top 960 lblnewpassword top 960 lblreenter top 1320 txtreenter top 1320 lbloldpassword top 600 txtoldpassword top 600 txtoldpassword tabindex 1 txtpassword tabindex 2 txtreenter tabindex 3 lstusers enabled false txtusername enabled false lblnewpassword visible true lblreenter visible true txtreenter visible true lbloldpassword visible true txtoldpassword visible true end sub private const margin size 60 " in twips " variables for data binding private datprimaryrs as adodb recordset " variables for enabling column sort private m isortcol as integ

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧