VBA to look through table and find record that two fields have certain criteria

Hello,
I have a form that i need to display a record on open.
This record is from a table with a column "ID" There maybe duplicate ID numbers but i need the code to find the ID and if the "servicecode" field has certain text ("Discount" for instance) then to show that record. I tried the following but I have got something wrong as it does not search for the combination.
If invs!ID = Forms!InvoiceAlterView!InvoicedetailsAlterView.Form!invdetailID And Left(invs!servicecode, 10) = "Discounted" Then

Have I explained this....????
iainmacleodAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
try

If invs!ID = Forms!InvoiceAlterView!InvoicedetailsAlterView.Form!invdetailID And invs!servicecode Like '*Discount*'  Then
iainmacleodAuthor Commented:
Thanks for your quick reply.
That still does not find the record, however I think there is a more fundamental error in my process.
I will have to review it and repost.
Thanks
iainmacleodAuthor Commented:
I actually think the stem of this problem is that Forms!InvoiceAlterView!InvoicedetailsAlterView is a continuous form and when I am doing my IF statement it is finding the first INVDETAILID on the form not necessarily the record that I have clicked on....
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

PatHartmanCommented:
If you are using a continuous form to open records on a different form, the continuous form MUST include the uniqueID of the records it is displaying.  Otherwise, how will you get the other form to open to the record you want?
Rey Obrero (Capricorn1)Commented:
<when I am doing my IF statement it is finding the first INVDETAILID on the form not necessarily the record that I have clicked on.... >

try saving the Record information to variables after you have selected the record, then refer to this variables in your IF statement..

what version of access are you using?
iainmacleodAuthor Commented:
Thanks all. Using 2010.
I am looking at redesigning the way I have the forms setup.
PatHartmanCommented:
If you have multiple records for "101 Main street", the ONLY way to open a specific one in a different form is if you have a UNIQUE ID.
Rey Obrero (Capricorn1)Commented:
try this in the current event of the continuous form "InvoicedetailsAlterView"

Tempvars.add "detID", me.invdetailID.value

use the variable in your IF statement

If invs!ID = Tempvars!detID And invs!servicecode Like '*Discount*'  Then

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.