Home > Software engineering >  Vb how to utilize the datagrid to get data and press the enter key to modify automatically
Vb how to utilize the datagrid to get data and press the enter key to modify automatically

Time:09-22


I use vb datagrid connected ado controls do a input data system
I am one of the functions of the system to achieve click any line of the datagrid data, a textbox is automatically to obtain the initial value, and then click add can increase, delete can delete
You can edit the textbox too,
Another is click the datagrid cell can be modified, but have to press the enter key to confirm to update the data, set up the datagrid update attribute is true, will update directly into, has been to solve this problem,

The following is a part of the test code, run results is increased when the data is empty,



 Private Sub Form_Load () 
Combo1. AddItem "number plate"
Combo1. AddItem "component model"
Combo1. AddItem "material code"
For I=0 To 7
Text3 (I)=""
Text2=""
Next
End Sub

Private Sub Command1_Click ()
If Text1. Text="" Then
MsgBox "please enter a query content:", vbInformation, "tip"
End the If
Adodc1. RecordSource="select material code, process, component types, dosage, encapsulation, note, type, plate number from bom table where plate number like '%" & amp; Text1. Text & amp; "% '
"Adodc1. Refresh

If Combo1. Text="component model" Then
Adodc1. RecordSource="select * from bom table where components models like '%" & amp; Text1. Text & amp; "% '
"Adodc1. Refresh
ElseIf Combo1. Text="material code" Then
Adodc1. RecordSource="select * from bom table where the material coding like '%" & amp; Text1. Text & amp; "% '
"Adodc1. Refresh
The Else
Adodc1. RecordSource="select * from bom table where plate number like '%" & amp; Text1. Text & amp; "% '
"Adodc1. Refresh
End the If
Text1. SetFocus
Text1. Text=""

If Adodc1. You. BOF And Adodc1. You. EOF Then
MsgBox "without the BOM you are looking for!" , vbInformation + vbOKOnly, "tip"
End the If
End Sub
Private Sub DataGrid1_RowColChange (LastRow As the Variant, ByVal LastCol As Integer)
Text2. Text=DataGrid1. Text
Dim As Integer I
For I=0 To 7
Text3 (I). The Text=DataGrid1. Columns (I)
Next
End Sub


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

If Text2. Text="" Then

MsgBox "content can't be empty", 48, "tip"
The Else
If a=MsgBox (" data has changed, whether to save?" , Then vbYesNo)
A=vbYes
DataGrid1. Text=Text2. Text

Adodc1. You. Update
Adodc1. Refresh
The Else
The Exit Sub
End the If
End the If

End Sub

Private Sub Command2_Click ()
If Text3 (0). The Text="" Or Text3 (1)=" "Or Text3 (2)=" "Or Text3 (3)=" "Or Text3 (4)=" "Or Text3 (6)=" "Or Text3 (7)=" "Then
MsgBox "key information can't be empty", 48, "tip"
The Else
Adodc1. You. AddNew
Adodc1. You. Fields (" code ")=Trim (Text3 (0). The Text)
Adodc1. You. Fields (" process ")=Trim (Text3 (1). The Text)
Adodc1. You. Fields (" component model ")=Trim (Text3 (2). The Text)
Adodc1. You. Fields (" dosage ")=Val (Text3 (3). The Text)
Adodc1. You. Fields (" packaging ")=Trim (Text3 (4). The Text)
Adodc1. You. Fields (" note ")=Trim (Text3 (5). The Text)
Adodc1. You. Fields (" type ")=Trim (Text3 (6). The Text)
Adodc1. You. Fields (" plate number ")=Trim (Text3 (7). The Text)
Adodc1. You. Update
Adodc1. Refresh
MsgBox "add success", 48, "tip"
End the If
End Sub


Private Sub Text4_KeyPress (KeyAscii As Integer)
If KeyAscii=46 Or (KeyAscii & gt;=48 And KeyAscii & lt;=57) Or (InStr (1, Text1, ", ")=0 And KeyAscii=8) Then
The Else
KeyAscii=27
End the If

End Sub

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

The Function y (Index As an Integer, KeyAscii As Integer) As an Integer
X, y=0
If KeyAscii=8 Or KeyAscii=46 Then
Y=KeyAscii 'if the input is del or backspace, allow
The Else
If the Index=3 And KeyAscii & gt; 47 And KeyAscii & lt; 58 Then
I=0, y=KeyAscii 'text box can only input Numbers 0-9
ElseIf Index Or Index==1 Or 2 Index=4 Or Index=5 Or Index=6 Or Index=7 Or Index=0 Then
I=0, y=KeyAscii ',1,2,4,5,6,7 text box input casually
End the If
End the If
End the Function



CodePudding user response:

I am myself by changing the flexgrid control implement any database table query, add, modify, delete, and so on common operations

CodePudding user response:

QQ2776478814, I have written these systems for many years

CodePudding user response:

Vsflexgird why not, such a good thing,

CodePudding user response:

Your text box does not specify the data source, such as the set text1. The datasource=dbconn 'is this your SQL connection variables, and then the corresponding field

CodePudding user response:

 Private Sub Form_Load () 
Combo1. AddItem "number plate"
Combo1. AddItem "component model"
Combo1. AddItem "material code"
Text2=""
For I=0 To 7
Text3 (I)=""
Next
The Set DataGrid1. The DataSource=Adodc1
End Sub
  • Related