Re: Return Mutiple Results From Access Dao
Hiya, I get an error, Item not found in this collection
and it falls on this line:
Cells(r, 1).Value = ![ID] & " " & ![post1] & " " & ![post2]
Any ideas?
EDIT *******************************
Not sure, but i think the error happens if the field in the database is empty?
The next stage of this, is I need to know how many results it's found before it starts moving the record to a sheet?[hr]*[/hr] Auto Merged Post;[dl]*[/dl]Right... think im getting the hang of the code bit now.... I think!
It is searching each field. But I am really looking for it to match whats on the form to a record. as an example:
If i search post1 and road it will return all post1's and roads, rather than return only records that hold both post1 and road in the same record? does this make sense.. lol!!
Anyway, i wanted it to ready the top 10 results that match the form critria and at the mo, because of how its searching im getting alot more.[hr]*[/hr] Auto Merged Post;[dl]*[/dl]Right, I have been doing a lot of messing, and I now have this:
' SQL statement to query the database with
strSQL = "SELECT [post1], [post2], [road], [town], [city], [plant], [tzone] FROM [SouthWest] WHERE ([post1] Like '" & formPost1 & "' OR [post2] Like '" & formPost2 & "' AND [road] Like '" & formRoad & "' OR [town] Like '" & formTown & "' OR [city] Like '" & formCity & "') ORDER BY ([road] Like '" & formRoad & "') DESC;"
'Initiate new DAO session, connect to the given database
'and open the above SQL query results
'MS Access will NOT be visible
Set objEngine = New DBEngine
Set objDb = objEngine.OpenDatabase(STR_DB_NAME)
Set objRs = objDb.OpenRecordset(strSQL)
'Takes the first result from the query and prints the fields
'on to the XL spreadsheet
r = 1
i = 0
'If an error goto msgbox
'On Error GoTo errorHandler
' Search for the records with the above critira
With objRs
'No record was found, so stops looking and ends sub
i = objRs.RecordCount
If i = 0 Then
MsgBox "No Address found!", , "CSOS Error"
Set objRs = Nothing
Set objDb = Nothing
Set objEngine = Nothing
searchform.disbox.Text = "Done!"
searchform.Hide
Exit Sub
End If
.MoveFirst ' find first record
Worksheets("Front").Cells(r, 1).Value = ![post1] & " " & ![post2] & " " & ![road] & " " & ![town] & " " & ![city]
Worksheets("Front").Cells(r, 2).Value = ![plant]
Worksheets("Front").Cells(r, 3).Value = ![tzone]
Do Until objRs.EOF ' keeping searching and pulling records till the critria not meet
r = r + 1
Worksheets("Front").Cells(r, 1).Value = ![post1] & " " & ![post2] & " " & ![road] & " " & ![city]
Worksheets("Front").Cells(r, 2).Value = ![plant]
Worksheets("Front").Cells(r, 3).Value = ![tzone]
.MoveNext
Loop
End With
Display More
But it is still not searching how i would like it to. If i put in the form:
post1 = BS21
and
road = close
I would want it bring up, only records that have Close and BS21 in them, but it's not doing that. I have tried this:
' SQL statement to query the database with
strSQL = "SELECT [post1], [post2], [road], [town], [city], [plant], [tzone] FROM [SouthWest] WHERE ([post1] Like '" & formPost1 & "' AND [post2] Like '" & formPost2 & "' AND [road] Like '" & formRoad & "' AND [town] Like '" & formTown & "' AND [city] Like '" & formCity & "') ORDER BY ([road] Like '" & formRoad & "') DESC;"
But this does not bring up a single record.... I am abit lost now, tried searching the web and the help files, but nothing like I am trying to do seems to be about. I even as you can see put a ORDER BY so I only get the 'Close' matching records at the top, and that don't work either....
Please... any ideas ?
Thanks in advance