Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can Someone Explain How to Use ADO

Status
Not open for further replies.

TVR

Programmer
Oct 14, 2002
1
MT
Well basically i'm still new to vb and i need to Build a program using ADO as a Connection for a database.
Can someone help me by explaining it or even better giving me an example. I'd be so thankfull.
PS: my VB for Dummies doesnt explain it. (heheh)
 
Look in VBHelp Index
Try this: ADO, code examples in VB

Or, on MSDN

Let me know if this helps
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Here is one example of function, which retrieve all data from table as recordset.

Hope this help.

' Declarations
Private m_objConnection As ADODB.Connection
Private m_objComm As ADODB.Command
Private m_ConnProvider As String
Private m_Path As String
Private strSQL As String

' Function has two optional parameters:
' a_updatable which means that you can update returning recordset if is true and
' a_sorted which means that returning recordset is sorted if is true

Public Function ReadData(Optional ByVal a_updatable As Boolean = False, Optional ByVal a_sorted As Boolean = False) As ADODB.Recordset

Dim GetRecords As New ADODB.Recordset

' Creating connection object
Set m_objConnection = New ADODB.Connection

' Creating connection string
m_connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\Data\db01.mdb;"
m_connStr = m_connStr & "Persist Security Info=False"
m_objConnection.ConnectionString = m_connStr
If a_updatable = True Then
m_objConnection.Mode = adModeShareExclusive
Else
m_objConnection.Mode = adModeShareDenyWrite
End If
On Error GoTo OpenData_Err
m_objConnection.Open
On Error GoTo 0

' Creating communication object to get data from database
' With comm object it is more flexible than directly assigned SQL string to recordset
Set m_objComm = New ADODB.Command
Set m_objComm.ActiveConnection = m_objConnection

' Table name is Tab_data and has Data_key as one od data fields
strSQL = "SELECT * FROM Tab_data"
If a_sorted = True Then
strSQL = strSQL & " ORDERED BY Data_key"
End If

' Create new returning recordset object
Set GetRecords.ActiveConnection = m_objConnection
m_objComm.CommandText = strSQL
m_objComm.CommandType = adCmdText
Set GetRecords.Source = m_objComm

On Error GoTo GetRecordSet_Err

' On this way you can open recordset on different and flexible way
GetRecords.Open strSQL, , adOpenKeyset, adLockPessimistic, adCmdText
Set GetRecordSet = GetRecords
On Error GoTo 0

Exit Function

OpenData_Err:
Set m_objConnection = Nothing
Set m_objComm = Nothing
' 100000 is your Error number
Err.Raise 100000, "ReadData", "Cannot open Database"
Exit Function

GetRecordSet_Err:
Set m_objConnection = Nothing
Set m_objComm = Nothing
' 100001 is your Error number
Err.Raise 100001, "ReadData", "Cannot get recordset"
End Function


' Calling this function

Sub CallThisFunction ()

'Declarations
Dim records As ADODB.Recordset

On Error GoTo ...
records = ReadData

On Error Goto 0

... your code

' You must destroy returning object from ReadData
Set records = Nothing


End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top