PDA

View Full Version : vba Sub to show many items from dlookup



jetbuster
06-15-2012, 08:37 AM
Private Sub DealID_AfterUpdate()
Dim strFilter As String
Me!MID = DLookup("MID", "DealContent", strFilter)
Exit_ProductID_AfterUpdate:
Exit Sub
End Sub

The problem with the above code is it will only put the first result found in Me!MID

any ideas?

MacroShadow
06-15-2012, 09:24 AM
DLookup only returns the first match, that is only one of its shortcomings. Use ELookup instead.

You will find the function here (http://www.everythingaccess.com/tutorials.asp?ID=Extended%2FReplacement-DLookup()).

jetbuster
06-15-2012, 09:36 AM
It still only returns one of the 3 results it can find with DealID
There are up to 3 entries with same DealID as a foreign key

Private Sub DealID_AfterUpdate()
Dim strFilter As String
strFilter = "DealID = " & Me!DealID
Me!MID = ELookup("MID", "DealContent", strFilter)
Exit_ProductID_AfterUpdate:
Exit Sub
End Sub

MacroShadow
06-24-2012, 12:46 AM
Try something along these lines:

Dim db As Database, rs As Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT [TheField] FROM [TheTable] WHERE TheWhereCondition ")
With rs
While Not .EOF
Debug.Print ![the field]
.MoveNext
Wend
End With