Home > Software engineering >  Questions about VB connection and query, thank you all for the warrior
Questions about VB connection and query, thank you all for the warrior

Time:10-14

Now command1 is used to implement the data connection and displayed in the datagrid1, using Command2 implements the query text2 content, and the query results show in datagrid1, I would like to ask experts, a second piece of code is too complicated, I don't each operation (query add, etc.) to write these again? Thank you, we give a detailed answer, finally, the code, thank you very much!!!!!!

Private Sub Command1_Click ()
Dim Cn As New ADODB. Connection
Dim Rst As New ADODB. You
Dim SqlStr As String
Cn. The Open ". The Provider=Microsoft Jet. The OLEDB. 4.0; Data Source="& amp; App. The Path & amp; "\ db2. MDB; Persist Security Info=False "
SqlStr="Select * From table 1"
Rst. CursorLocation=adUseClient
Rst. Open SqlStr, Cn, adOpenDynamic adLockOptimistic, adCmdText 'Open recordset
Set DataGrid1. The DataSource to DataGrid modify data source=Rst '
End Sub

Private Sub Command2_Click ()
Dim Cn As New ADODB. Connection
Dim Rst As New ADODB. You
Dim SqlStr As String
Cn. The Open ". The Provider=Microsoft Jet. The OLEDB. 4.0; Data Source="& amp; App. The Path & amp; "\ db2. MDB; Persist Security Info=False "
SqlStr="Select * From table 1 where name='" & amp; Text2. Text & amp; "'
"Rst. CursorLocation=adUseClient
Rst. Open SqlStr, Cn, adOpenDynamic adLockOptimistic, adCmdText 'Open recordset
Set DataGrid1. The DataSource to DataGrid modify data source=Rst '
End Sub

CodePudding user response:

SqlStr="Select * From table 1 where name='" & amp; Text2. Text & amp; "'
"
Put including the names of all the fields in a combobox,
The code is amended as:

SqlStr="Select * From table 1 where" & amp; Combo1. Text & amp;"='" & amp; Text2. Text & amp; "'"
  • Related