learning ADO; finding a record

  • Thread starter Christopher Glaeser
  • Start date
C

Christopher Glaeser

The code below uses ADO to "sequentially" search tblTasks for a matching key
and then extract the value of another field in that record to open a form.
Given that TaskID is the primary key of tblTasks, what is the efficient ADO
code to find that record? Any other commens on improving this code also
welcome.

Private Sub ctMDay0_AppDblClick(ByVal nIndex As Integer)

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open "tblTasks", CurrentProject.Connection

rst.MoveFirst
Do Until rst.EOF()
If rst!TaskID = Me.ctMDay0.AppointData(nIndex) Then
DoCmd.OpenForm "frmWorkOrder", , , "WorkOrderID=" & rst!WorkOrderID
Exit Do
End If
rst.MoveNext
Loop
rst.Close

End Sub
 
D

Dirk Goldgar

Christopher Glaeser said:
The code below uses ADO to "sequentially" search tblTasks for a
matching key and then extract the value of another field in that
record to open a form. Given that TaskID is the primary key of
tblTasks, what is the efficient ADO code to find that record? Any
other commens on improving this code also welcome.

Private Sub ctMDay0_AppDblClick(ByVal nIndex As Integer)

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.Open "tblTasks", CurrentProject.Connection

rst.MoveFirst
Do Until rst.EOF()
If rst!TaskID = Me.ctMDay0.AppointData(nIndex) Then
DoCmd.OpenForm "frmWorkOrder", , , "WorkOrderID=" &
rst!WorkOrderID Exit Do
End If
rst.MoveNext
Loop
rst.Close

End Sub

It will usually be faster to open the recordset on a query that returns
only the one record you want. I'm not an ADO expert, as I use DAO for
most things, but I expect that your code would look something like this:

Dim rst As ADODB.Recordset
Dim strSQL As String

strSQL = _
"SELECT WorkOrderID FROM tblTasks " & _
"WHERE TaskID = " & Me.ctMDay0.AppointData(nIndex)

Set rst = New ADODB.Recordset
With rst
.Open strSQL, CurrentProject.Connection
If Not .EOF Then
DoCmd.OpenForm "frmWorkOrder", , , _
"WorkOrderID=" & !WorkOrderID
.Close
End With
 
C

Christopher Glaeser

It will usually be faster to open the recordset on a query that returns
only the one record you want. I'm not an ADO expert, as I use DAO for
most things, but I expect that your code would look something like this:

Thanks! Did a minor tweak (added an End If) and it works great.

Best,
Christopher
 
C

Christopher Glaeser

Very good. I'm glad my error was one that you could spot easily.

Yes, and it is exremely helpful to know how to build and use these queries
in code. Now I can go back and improve some forms that will benefit from
ADO coding. Thanks again.

Best,
Christopher
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top