MNRaghu,
Below is the code that I use to open and close the ADO object to connect to the database. Following that is the code that prompts the user for a selection on screen, checks for Object Data and reads the OD if found. While this does not show how to write data from the database to the OD I hope that it is enough to give you a good primer on where to look. By the way this function was written entirely from the help files in Map on ActiveX autoation and API help.
I hope this helps you.
Bart
***************************BEGIN CODE***********************
'---------------------------------------------------------------------------------------
' Module : modDataBase
' DateTime : 6/3/2005 10:29
' Author : bart
' Purpose :This holds the Database connection string and deals with opening and _
closing the connection
'---------------------------------------------------------------------------------------
Option Explicit
Dim sConn As String
Public oConn As ADODB.Connection
'Make sure you have a reference in the project to MS Active Data Objects 2.x (I use 2.8)
Public Sub OpenDB
Set oConn = New ADODB.Connection
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyData\Database.mdb;" & _
"Persist Security Info=False"
oConn.Open sConn
Exit Sub
CheckError:
MsgBox "Database Connection Error" & vbCrLf & Err.Number & " - " & Err.Description
'LocErr = True
Exit Sub
Resume Next
End Sub
Public Sub CloseDB()
If oConn.State = 1 Then
oConn.Close
Set oConn = Nothing
End If
End Sub
The following code prompts the user to select an object with Object Data Attached, reads the value of the object data and querries the database for records matching the value of the Object Data
Public NEW_TLNUM As String
Public rsCable As ADODB.Recordset
Public Sub GetCableID()
Dim ODrcs As ODRecords
Dim boolVal As Boolean
Dim returnObj As AcadObject
Dim basePnt As Variant
Dim i As Integer
Dim answer As Variant
On Error GoTo errorhandler
'**********************************NEW****************
Dim amap As Object
Dim ODfdfs As ODFieldDefs
Dim ODfdf As ODFieldDef
Dim ODtb As ODTable
Dim ODrc As ODRecord
Set amap = ThisDrawing.Application. _
GetInterfaceObject("AutoCADMap.Application.2")
Set ODfdfs = amap.Projects(ThisDrawing).MapUtil.NewODFieldDefs
ThisDrawing.Utility.GetEntity returnObj, basePnt, "Select an object"
If returnObj.ObjectName = "AcDbBlockReference" Then
GetNodePic returnObj.Handle
Exit Sub
End If
' Add Column Headings and Defaults
Set ODfdf = ODfdfs.Add("CAB_ID", "Cab Disc", "23", 0)
Set ODfdf = ODfdfs.Add("Route_ID", "Rout Disc", "66", 1)
Set ODrcs = amap.Projects.Item(ThisDrawing). _
ODTables.Item("CABLE").GetODRecords
'Prompt user to select an object
boolVal = ODrcs.Init(returnObj, True, False)
For i = 0 To ODrcs.Record.Count - 1
NEW_TLNUM = ODrcs.Record.Item(i).Value
Next i
'*****************GET CABLE******************
Const conErrInvalidNull = 94
Const conErrNotObject = 438
Const conErrNoRecord = 3021
OpenDB 'open Connection
Set rsCable = New ADODB.Recordset
sSQL = "Select * from CABLE where CAB_ID = '" & NEW_TLNUM & "'" 'build SQL query to find record based on Cable ID
On Error GoTo errorhandler
rsCable.Open sSQL, oConn, , adLockOptimistic 'Finds record in Database based on CAB_ID
If Not rsCable.EOF Then 'If record is found then load record into form
Load frmCable
frmCable.Show
Exit Sub
End If
errorhandler:
Select Case Err
Case conErrNotObject
MsgBox ("This is not a cable."), vbExclamation
Case conErrInvalidNull
Resume Next
Case conErrNoRecord
Resume
Case Else
MsgBox ("Unexpected error :" & Err.Description), vbExclamation
End Select
End Sub
********************************END CODE************************