VBA for Adding Records to table based on criteria on another


This is what I would like to do and don’t know the best method:

1.  I have one table named “tblReferrals”. This contains a primary field called ReferralID.  It also has a yes/no field of “Scheduled” (meaning scheduled for next term).
2.  I have a second  table named “tblClassAndQtr”. Which has a field ReferralID from above table, as well as a QtrID field. (from another table).

I am creating a form, called "frmAddNewQtrToAllScheduled" where one can choose a specific QtrID.  This field on the form is called “MyNextQtrID”.  Then, after pressing the confirm button,  I’d like to have some code that--

Only IF the "Scheduled" field on TblReferrals,  is true, then…
Adds records to the second table, tblClassAndQtr, and for each new record then inserts the related ReferralID from the tblReferrals, and the MyNextQtriID from the form into the QtrID field.  

I am not sure how to begin doing this, am kind of a novice although am good at using code examples, and any assistance appreciated.  Thanks!
Who is Participating?

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

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.

Where does the value for QtrID come from?  How many rows do you want to insert?
Gustav BrockCIOCommented:
It sounds as simple to do, but I think you shouldn't expect any of us to build a complete test scenario.

So, upload a sample database and tell the expected result when having selected a next id.
Based on your description, my understanding is that you want to insert all records from tblReferrals where Scheduled = True, with user input from MyNextQtrID on your form as QtrID for all of the inserted records.

The query would look something like this:
INSERT INTO tblClassAndQtr (ReferralID, QtrID)  SELECT ReferralID, [UserInputForMyNextQtrID]  FROM tblReferrals WHERE Scheduled = True

Open in new window

The VBA to automate it would look like this:

Private Sub YourCommandButton _Click
   Dim s as String
    s = "INSERT INTO tblClassAndQtr (ReferralID, QtrID)  SELECT ReferralID, " & Me.MyNextQtrID  & " FROM tblReferrals WHERE Scheduled = True"
   CurrentDB.Execute s, dbFailOnError
End Sub

Open in new window

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
Not2ruthlessAuthor Commented:
Thanks mbizup!  I have never used an Insert query before but that's exactly what I was trying to get to! Have a great week...
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.