PDA

View Full Version : Correct way to connect and retrieve Access data



gmaxey
02-10-2012, 11:13 PM
Hi,

I don't think I've posted here before. I mainly dabble in Word. I have a couple of Word projects that I have cobbled together over the years that reach out to Access (very basic stuff) to obtain data.

The other day I was reviewing one of the older ones and it was throwing an error due to a missing reference to the DAO 3.6 object library. I finally got it to work again but in my research (and I only understood about half of what I read) it seem seems that perhaps DAO may soon be a thing of the past.
Also, all the stuff I've done was back when the Access file extension was .mdb.

I was hoping someone would review my procedure and advise if I should be using a different method (different reference libarary, etc.)

My Access file is a simple table with a handful of records/fields>

Name Address Phone
Joe xx xx
Bill
Bob
Etc.

Private Sub Test()
Dim myDataBase As DAO.Database
Dim myActiveRecord As DAO.Recordset
Dim i As Long
Dim arrHolder(10, 2) As String
'Open the database to retrieve data
Set myDataBase = OpenDatabase("D:\Data Stores\sourceAccess.mdb")
'Define the first recordset
Set myActiveRecord = myDataBase.OpenRecordset("Table1", dbOpenForwardOnly)
i = 0
'Loop through all the records in the table until the EOF marker is reached.
Do While Not myActiveRecord.EOF
arrHolder(i, 0) = myActiveRecord.Fields("Employee Name")
arrHolder(i, 1) = myActiveRecord.Fields("Employee DOB")
arrHolder(i, 2) = myActiveRecord.Fields("Employee ID")
i = i + 1
'Get the next record
myActiveRecord.MoveNext
Loop
'Close the database and clean-up.
myActiveRecord.Close
myDataBase.Close
Set myActiveRecord = Nothing
Set myDataBase = Nothing
'Check result
For i = 0 To 10
Debug.Print arrHolder(i, 0) & " " & arrHolder(i, 1) & " " & arrHolder(i, 2)
Next i
End Sub

Thanks, Greg

HiTechCoach
02-12-2012, 11:16 PM
About DAO, it is far from being a thing of the past. With Access 2000 there was a push by Microsoft to start using ADO. Well that did not last long. By the time Access 2002 was released the shift was back to DAO. DAO is still going strong with Access 2007 and 2010 with the new .accdb format.