
我用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
uj5u.com熱心網友回復:
我是自己通過修改flexgrid控制元件實作任意資料庫表的查詢、增加、修改、洗掉等通用操作uj5u.com熱心網友回復:
QQ2776478814,我寫過這些系統很多年了uj5u.com熱心網友回復:
vsflexgird為什么不用呢,這么好的東西。uj5u.com熱心網友回復:
你的文本框沒有指定資料源,如set text1.datasource=dbconn '這個是你的sql連接變數,然后再關聯對應的欄位uj5u.com熱心網友回復:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/62419.html
