Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO Query only returns first record

Status
Not open for further replies.

rmcmorris

Programmer
Nov 22, 2002
42
US
I have a VBA module in Excel where I am returning data from a database using ADO. Previously, the query would match a number variable. This worked fine and returned all of the records. Now I am changing the code to match a string variable instead on a different database field. When using an "=" operator for the match in the query, only the first matched record is returned to the ADO recordset. If I use the "LIKE" operator, all of the records are returned. Any ideas why this happens????

'This works fine with a number field.
MyNumber=123
rst.Open "SELECT MyTable.Field1, MyTable.Field2, MyTable.Field3 FROM MyTable WHERE (((MyTable.`NumField`)=" & MyNumber & "))", ADOcon, adOpenForwardOnly, adLockReadOnly

'This Does not work with an alpha field. It returns first
'matching record only.
MyText="abc"
rst.Open "SELECT MyTable.Field1, MyTable.Field2, MyTable.Field3 FROM MyTable WHERE (((MyTable.`TextField`)="' & MyText & "'))", ADOcon, adOpenForwardOnly, adLockReadOnly

'But using the LIKE operator in the Text field
'Works fine.
MyText="abc"
rst.Open "SELECT MyTable.Field1, MyTable.Field2, MyTable.Field3 FROM MyTable WHERE (((MyTable.`TextField`) LIKE "' & MyText & "'))", ADOcon, adOpenForwardOnly, adLockReadOnly

I have installed the Jet40 Service Pack 6. My OS is Windows 2000 Professional. I am using Excel 97.

Thanks,

Regan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top