I have almost forgotten all the VB I learned a few years ago, so I have been reviewing it these days. It is not very difficult to connect ADO database with VB.
The first step of connection (read carefully)
For beginners, the tutorial here is the most detailed. The first step to connect to the ADO database is what components should be added? The full name is Microsoft ADO Data Control 6.0 (SP6) (OLEDB) component.
In the Microsoft ADO Data Control 6.0 (SP6) (OLEDB) component there is a name: Adodc data control, you need to add it. Find ACCES in the Adodc data control data location.
The method referenced by the control (the value refers to the name)
Copy the code code as follows:
For i = 1 To Adodc1.Recordset.RecordCount
If Not Adodc1.Recordset.EOF Then
Combo1.AddItem Adodc1.Recordset.Fields("value").Value
Adodc1.Recordset.Movenext
End If
Next i
The author quoted by this code is ydl890406. When I was writing this thing in the VB group, I was asked to borrow it. Later I found that there were many errors. After y revised it several times, there were still errors, so I simply rewrote it. This is Later code. Time passed quickly and the VB group also disbanded. Later, for some unknown reason, many tutorials now use this code.
The second part is the AOD code connection. Since the second part involves the Recordset object and the Connection object, you can learn it yourself and write it yourself.
What are the Recordset object and the Connection object? The Connection object is the connection to the data source, and the Recordset object is the operation data.
Looking for Microsoft ADO Data Control 6.0 (SP6) (OLEDB) components
VB uses ADO to connect to SQL Server database
'Data source information constant Public Const conn As String = "Provider = SQLOLEDB.1;Password = sa; UserID = sa; Initial Catalog = StudentFiles; Data Source = localhost" Public Const CONNECT_LOOP_MAX = 10 'Perform the connect operation once and you can access the database Number of timesPrivate IsConnect As Boolean 'Mark whether the database is connectedPrivate Connect_Num As Integer 'Mark the number of times data is accessed after executing the Connect() functionPrivate cnn As ADDODB.Connection 'Connect object to connect to the databasePrivate re As ADDODB.Recordset 'Recordset object that saves the result set //Connect to the databasePrivate Sub Connect() 'If the connection mark If true, return. IF IsConnect = True Then Exit Sub End If Set cnn = New ADODB.Connection 'Key new is used to create a new object cnn cnn.ConnectionString = conn cnn.Open 'Determine the status of the connectionIf cnn.State <> adStateOpen Then MsgBox"Database Connection Failure" End End If 'Set the connection ID, indicating that it is connected to the database IsConnect = TrueEnd Sub' Disconnect the connection with the database Private Sub DisConnect() Dim rc As Long If IsConnect = False Then Exit Sub End If 'Close the connection cnn.Close 'Release cnn Set cnn = Nothing IsConnect = FalseEnd Sub'Use Connect_Num to control the data connection Public Sub DB_Connect() Connect_Num = Connect_Num + 1 ConnectEnd Sub'Use Connect_Num to control the data disconnect Public Sub DB_Disconnect ()If Connect_Num >= CONNECT_LOOP_MAX Then Connect_Num = 0 Disconnect End If End Sub'Force to close the api mode to access the Russian database, reset the counter Public Sub DBapi_Disconnect() Connect_Num = 0 DisconnectEnd Sub'Execute database operation language'byval It is passed by the value of the parameter, and the parameter will not change during the transfer process (that is, the parameter value is passed to the process instead of the address. This allows the process to access a copy of the variable, and the process cannot change the value of the variable. ); corresponding to it is byref, which means passing the value according to the address of the parameter. Byref can be omitted. Public Sub SQLExt(ByVal TmpSQLstmt As String) Dim cmd As New ADODB.Command 'Create Command object cmd DB_Connect 'Connect to the database Set cmd.ActiveConnection = cnn 'Set the ActiveConnect attribute of cmd and specify the database connection associated with it cmd.CommandText = TmpSQLstmt 'Set the command text to be executed'MsgBox TmpSQLstmt cmd.Execute 'Execute the command Set cmd = Nothing DB_DisConnect 'Disconnect Open the connection with the database End Sub'Execute the database query statement Public Function QueryExt(ByVal TmpSQLstmt As String ) As ADODB.Recordset Dim rst As New ADODB.Recordset 'Create Rescordset object rst DB_Connect 'Connect to the database Set rst.ActiveConnection = cnn 'Set the ActiveConnection property of rst and specify the connection to the database related to it rst.CursorType = adOpenDynamic 'Set the cursor type rst. LockType = adLockOptimistic 'Set lock type rst.Open TmpSQLstmt 'Open the recordset Set QueryExt = rst 'Return the recordsetEnd Function