Home > Article > Computer Tutorials > Using the ADO method in VB to query data tables
Easy to use controls.
The prerequisite is to connect the adodc control to the database normally
The datagrid control is bound to the adodc control
Private Sub Command1_Click()
'Connect to the database
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\database.mdb"
'Inquire
Adodc1.RecordSource = "select * from table where name like '" & Text1.Text & "' and major like '" & Text2.Text & "'"
Adodc1.Refresh
End Sub
First quote ADO: Project--Reference--Select MS ActiveX Data Objects
Code:
Dim Access_Con As Connection
Set Access_Con = New Connection
Access_Con.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=" & App_Path & "\FHLdata.mdb;"
Access_Con.Open
Dim rs As Recordset
Set rs = New Recordset
rs.Open "select * from User Management", Access_Con
''''The query results will be in rs. You can bind rs to the datagrid to view the data, etc.
When creating a new project, select the data project. At this time, the necessary controls for database programming have been loaded in the toolbox on the left side of the VB6 integrated debugging environment.
Then add the ADODC control and DATAGRID control in the FORM1 form, select ADODC1 for the property DATASOURCE of DATAGRID1, open the ADODC1 control property page to use the connection string, select Generate, select MICROSOFT jet 4.0 OLE DB Provider in the provider option, and then Press to connect to the database, etc. There is a long string in the blank text window using the connection string on the ADODC1 control property page. Note that this string can be copied to the program code for programming. There is a command text (SQL) writing window in the data source of the ADODC1 control property page to write SQL query language. The SQL statements in this window can be copied to the program code for programming.
The main structure of SQL query language is:
Select query field from table name Where query condition statement [sort statement or grouping statement]
The query fields must be separated by (Spanish) commas or replaced by an * sign. It is recommended that the sorting statement in the above query be best used.
SQL query language will tell you if there is an error when the program is running, just make the corresponding changes. I usually press the above connection first, and use "SELECT * FROM TabelName" for SQL debugging. There is no problem. Use a button hole to copy the connection string and SQL query language that need to be copied in the CLICK event of the button control. Save it for later use. Then delete the ADODC1 hole piece, add the ADODC1 control and add other content.
The following are code examples:
Private Sub Form_Load()
Text1 = ""
With Adodc1
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select name from b1 order by name"
Adodc1.Refresh
DataGrid1.Refresh
End With
End Sub
Private Sub Text1_Change()
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select name from b1 where name >='" & Text1 & "' order by name"
Adodc1.Refresh'Update query
DataGrid1.Refresh
End Sub
The above is the detailed content of Using the ADO method in VB to query data tables. For more information, please follow other related articles on the PHP Chinese website!