My Account Subscribe Help About
Sign In | Register FREE
Friday, April 10, 2026
Man jailed for killing abused wife who jumped from bridgeCeasefire or no ceasefire, the Middle East's reshuffling is not yet doneMelania Trump denies ties to Jeffrey Epstein and urges hearing for survivorsSimple guide: How the Iran war is affecting the cost of holidays, food and clothesEU fingerprint and photo travel rules come into forceWant to help garden birds? Don't feed them in warmer months, says RSPBDublin Airport issues travel guidance as Irish fuel protests continueMen behind 'Tripadvisor for people smugglers' jailed for 19 yearsIran conflict must be 'line in sand' to build more resilient UK, Starmer saysBafta fell short in duty of care when racial slur was shouted, review findsExtra £5m pledged for patrolling places of worship'Endless fears': Even if fighting stops, the damage to Iran's children will endureLebanon thought there was a ceasefire - then Israel unleashed deadly blitzHow many ships are crossing the Strait of Hormuz?Has US achieved its war objectives in Iran?Can stats help you find the Grand National winner?This coat cost $248 in illegal tariffs. Will he ever get the money back?From a smuggled harmonica to Artemis' playlist - the history of music in spaceWeekly quiz: What might have made Paddington panic about his marmalade?How the Artemis crew will splash down on EarthLeBron and Bronny James record first son-to-father assist in NBA historyTen cases a day - 'blitz courts' could tackle the Crown Court backlog'I was in a slump - now my art is in Billie Eilish's house'Labrinth not involved in Euphoria's third seasonLava soars into air as Hawaii's Kilauea volcano erupts againWhite House staff told not to place bets on prediction marketsRussia and Ukraine agree to truce for Orthodox EasterBBC News appIs Defence Secretary Pete Hegseth waging a holy war against Iran?Defence secretary interview on Russian submarine operation
FDN » Visual Basic » VB6 Database Programming

VB6 Database Programming

VB6 Database Programming

Visual Basic 6 supports multiple data access technologies. ADO (ActiveX Data Objects) is the recommended approach for new development.

Adding the ADO Reference

  1. Go to ProjectReferences.
  2. Check Microsoft ActiveX Data Objects 2.x Library (use 2.8 if available).
  3. Click OK.

Connecting and Querying

Private Sub LoadEmployees()
    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim strConn As String

    strConn = "Provider=SQLOLEDB;" & _
        "Data Source=SQLSERVER01;" & _
        "Initial Catalog=FlamenetDB;" & _
        "Integrated Security=SSPI;"

    conn.Open strConn

    rs.Open "SELECT EmployeeID, FirstName, LastName, Email " & _
        "FROM Employees WHERE IsActive = 1 ORDER BY LastName", _
        conn, adOpenStatic, adLockReadOnly

    ' Populate a ListView
    lvEmployees.ListItems.Clear
    Do While Not rs.EOF
        Dim li As ListItem
        Set li = lvEmployees.ListItems.Add(, , rs("EmployeeID"))
        li.SubItems(1) = rs("FirstName") & " " & rs("LastName")
        li.SubItems(2) = rs("Email") & ""
        rs.MoveNext
    Loop

    rs.Close
    conn.Close
End Sub

Parameterized Commands

Private Function GetEmployeeByID(ByVal lngID As Long) As ADODB.Recordset
    Dim cmd As New ADODB.Command
    Set cmd.ActiveConnection = g_conn  ' Module-level connection
    cmd.CommandText = "SELECT * FROM Employees WHERE EmployeeID = ?"
    cmd.CommandType = adCmdText
    cmd.Parameters.Append cmd.CreateParameter("@id", adInteger, adParamInput, , lngID)
    Set GetEmployeeByID = cmd.Execute()
End Function

Data Binding

VB6 supports data binding with the ADO Data Control (ADODC). Set the ConnectionString and RecordSource properties at design time, then bind text boxes and grids to it using the DataSource and DataField properties. However, manual ADO code (as shown above) provides more control and better error handling.

Error Handling

Private Sub SaveData()
    On Error GoTo ErrorHandler

    conn.BeginTrans
    ' ... execute INSERT/UPDATE statements ...
    conn.CommitTrans
    MsgBox "Data saved.", vbInformation
    Exit Sub

ErrorHandler:
    conn.RollbackTrans
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
End Sub
« Back to Visual Basic « Back to FDN