vb 如何利用datagrid自动获取数据并按enter键修改

yedxlyq517 2016-09-10 11:39:12

我用vb datagrid连接ado控件做个录入数据系统
我这个系统要实现的功能之一是单击任意行datagrid的数据,textbox就自动获得初始值,然后按增加可以增加,按删除可以删除
当然了textbox也可以编辑。
另外就是单击datagrid单元格 可以修改,但是要按enter键确认才能更新数据。设置datagrid更新属性为真,就会直接更新进去,一直在解决这个问题。

以下是部分测试代码,运行结果是增加的时候数据是空的。



Private Sub Form_Load()
Combo1.AddItem "板号"
Combo1.AddItem "元器件型号"
Combo1.AddItem "物料编码"
For i = 0 To 7
Text3(i) = ""
Text2 = ""
Next
End Sub

Private Sub Command1_Click()
If Text1.Text = "" Then
MsgBox "请输入查询内容:", vbInformation, "提示"
End If
Adodc1.RecordSource = "select 物料编码,工序,元器件型号,用量,封装,备注,类型,版号 from bom表 where 版号 like '%" & Text1.Text & "%'"
Adodc1.Refresh

If Combo1.Text = "元器件型号" Then
Adodc1.RecordSource = "select * from bom表 where 元器件型号 like '%" & Text1.Text & "%'"
Adodc1.Refresh
ElseIf Combo1.Text = "物料编码" Then
Adodc1.RecordSource = "select * from bom表 where 物料编码 like '%" & Text1.Text & "%'"
Adodc1.Refresh
Else
Adodc1.RecordSource = "select * from bom表 where 版号 like '%" & Text1.Text & "%'"
Adodc1.Refresh
End If
Text1.SetFocus
Text1.Text = ""

If Adodc1.Recordset.BOF And Adodc1.Recordset.EOF Then
MsgBox "没有你要找的BOM!", vbInformation + vbOKOnly, "提示"
End If
End Sub
Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
Text2.Text = DataGrid1.Text
Dim i As Integer
For i = 0 To 7
Text3(i).Text = DataGrid1.Columns(i)
Next
End Sub


Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Call Command3_Click
End If
End Sub
Private Sub Command3_Click()

If Text2.Text = "" Then

MsgBox "内容不能为空", 48, "提示"
Else
If a = MsgBox("数据已更改,是否存盘?", vbYesNo) Then
a = vbYes
DataGrid1.Text = Text2.Text

Adodc1.Recordset.Update
Adodc1.Refresh
Else
Exit Sub
End If
End If

End Sub

Private Sub Command2_Click()
If Text3(0).Text = "" Or Text3(1) = "" Or Text3(2) = "" Or Text3(3) = "" Or Text3(4) = "" Or Text3(6) = "" Or Text3(7) = "" Then
MsgBox "关键信息不能为空", 48, "提示"
Else
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("物料编码") = Trim(Text3(0).Text)
Adodc1.Recordset.Fields("工序") = Trim(Text3(1).Text)
Adodc1.Recordset.Fields("元器件型号") = Trim(Text3(2).Text)
Adodc1.Recordset.Fields("用量") = Val(Text3(3).Text)
Adodc1.Recordset.Fields("封装") = Trim(Text3(4).Text)
Adodc1.Recordset.Fields("备注") = Trim(Text3(5).Text)
Adodc1.Recordset.Fields("类型") = Trim(Text3(6).Text)
Adodc1.Recordset.Fields("版号") = Trim(Text3(7).Text)
Adodc1.Recordset.Update
Adodc1.Refresh
MsgBox "添加成功", 48, "提示"
End If
End Sub


Private Sub Text4_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 Or (KeyAscii >= 48 And KeyAscii <= 57) Or (InStr(1, Text1, ",") = 0 And KeyAscii = 8) Then
Else
KeyAscii = 27
End If

End Sub

Private Sub Text3_KeyPress(Index As Integer, KeyAscii As Integer)
KeyAscii = xy(Index, KeyAscii)
End Sub

Function xy(Index As Integer, KeyAscii As Integer) As Integer
xy = 0
If KeyAscii = 8 Or KeyAscii = 46 Then
xy = KeyAscii '如果输入是del或退格,允许
Else
If Index = 3 And KeyAscii > 47 And KeyAscii < 58 Then
xy = KeyAscii 'i=0的文本框只能输入数字0-9
ElseIf Index = 1 Or Index = 2 Or Index = 4 Or Index = 5 Or Index = 6 Or Index = 7 Or Index = 0 Then
xy = KeyAscii 'i=0,1,2,4,5,6,7的文本框随便输入
End If
End If
End Function



...全文
2606 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2017-01-24
  • 打赏
  • 举报
回复
Private Sub Form_Load()
    Combo1.AddItem "板号"
    Combo1.AddItem "元器件型号"
    Combo1.AddItem "物料编码"
    Text2 = ""
    For i = 0 To 7 
        Text3(i) = ""
    Next
    Set DataGrid1.DataSource = Adodc1
End Sub
L2642730 2017-01-23
  • 打赏
  • 举报
回复
你的文本框没有指定数据源,如set text1.datasource=dbconn '这个是你的sql连接变量,然后再关联对应的字段
ah_2056 2016-12-20
  • 打赏
  • 举报
回复
vsflexgird为什么不用呢,这么好的东西。
  • 打赏
  • 举报
回复
QQ2776478814,我写过这些系统很多年了
test2002 2016-09-10
  • 打赏
  • 举报
回复
我是自己通过修改flexgrid控件实现任意数据库表的查询、增加、修改、删除等通用操作

1,217

社区成员

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

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