i need a code for adodb which can link my form in vb to a table in ms access which contains
name and id as the fields.help please!! thanx

Dani AI

Generated

wanted a simple ADODB-style link between a VB form and an Access table (Name / ID). and gave classic ADODB examples that work in VB6, and pointed to extra resources. The replies are a good start, but a few practical points are missing now that users commonly run newer Access formats and 64-bit systems.

A modern, reliable option in VB.NET is to use System.Data.OleDb, fill a DataTable, and bind it to form controls. The example below shows the pattern: open a connection, fill a DataTable with the table rows, bind to a grid, and copy selected row values into TextBox controls. Using blocks ensure connections are closed automatically.

Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Path\YourDatabase.accdb;"
Using cn As New System.Data.OleDb.OleDbConnection(cs)
    cn.Open()
    Using da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [People]", cn)
        Dim dt As New DataTable()
        da.Fill(dt)
        DataGridView1.DataSource = dt
    End Using
End Using

If DataGridView1.CurrentRow IsNot Nothing Then
    Dim rv = CType(DataGridView1.CurrentRow.DataBoundItem, DataRowView).Row
    txtName.Text = If(rv.IsNull("Name"), "", rv("Name").ToString())
    txtID.Text = If(rv.IsNull("ID"), "", rv("ID").ToString())
End If

Troubleshooting and practical notes:

  • Use the ACE provider for .accdb files; older .mdb files use the legacy Jet engine. See connection string examples at .
  • On modern Windows, the Access Database Engine driver may not be installed or may be 32-bit only. If the provider is missing, install the engine (for example, the 2010 redistributable) or compile your app to x86. Download: .
  • Always parameterize UPDATE/INSERT commands and wrap connections in Using blocks to avoid leaks.
  • If staying with VB6/ADODB (as in earlier replies), add a reference to the Microsoft ActiveX Data Objects library, handle errors, and explicitly close/dispose recordsets and connections.

These points complement the ADODB snippets already posted and address common deployment pitfalls encountered years after a thread was written.

Recommended Answers

All 3 Replies

Function GetRecord()

Dim ConnectionStr As String
Dim Uconnection As ADODB.Connection
Dim rs As ADODB.Recordset


ConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabasePath & "\databasefilename.mdb;Persist Security Info=False"
Set Uconnection = New Connection
Uconnection.ConnectionString = ConnectionStr
Uconnection.CommandTimeout = 120000
Uconnection.Open
Set rs = New Recordset
rs.CursorLocation = adUseClient
strQry="Select Name,Id from tablename"
rs.Open strQry, Uconnection, adOpenDynamic, adLockOptimistic
strQry = ""
Set GetRecord = rs

End Function

this if your using a DE...

dim rs as new adodb.recordset
set rs = new adodb.recordset

if rs.state = 1 then rs.close
rs.open "SELECT NAME, ID FROM TABLE", DataEnvironment1.Connection1, adOpenDynamic, adLockOptimistic

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.