Pulling a value from a form to create new records in append query (1 Viewer)

Liz A

Registered User.
Local time
Today, 00:43
Joined
Feb 1, 2006
Messages
17
So, I've been searching through this forum and can't seem to find the answer to this one. I would like to capture a value from a main form and have it used as a value in an append query, in order to populate a subform based on the main form, like so:

INSERT INTO tblTakenSurveys ( VisitID, SurveyQuestionID, ResponseCodeID) SELECT Forms![frmMyFormName]!VisitID, tblSurveyQuestions.SurveyQuestionID, 66
FROM SurveyQuestions WHERE SurveyID = 3;

Might help to explain some of the terms in this statement:
  • tblTakenSurveys is where I need the new data to be entered via the subform.
  • Forms![frmMyFormName]!VisitID is a textbox control bound to a PK in another table that has a one-to-many relationship with tblTakenSurveys.
  • tblTakenSurveys.ResponseCodeID is a foreign key that represents respondents actual answers to questions.
  • 66 is a value for a ResponseCodeID that stands for a dummy value meaning "data not yet entered"

As per advice I received from others on this forum, I have set a query like the above to run from a command button to populate the subform (in theory). But I'm sure I've done something wrong within the query because it will not return a value from the form "VisitID" control and therefore will not append the rows. Without the appended rows, my subform will not populate. And this has me running in circles...

I pasted the link to another thread below, where I originally received a lot of input as far as the table structure. I did not start this one, but my posts are the most recent (as of now anyway). Pat Hartman had given me a lot of the guidance here.

http://www.access-programmers.co.uk/forums/showthread.php?t=100176
 

Liz A

Registered User.
Local time
Today, 00:43
Joined
Feb 1, 2006
Messages
17
Nevermind! I figured it out and it was ridiculously simple. In case anyone else needs this, the correct query is:

INSERT INTO tblTakenSurveys ( VisitID, SurveyQuestionID, ResponseCodeID )
SELECT [DEMO Lab Session Info].VisitID, tblSurveyQuestions.SurveyQuestionID, 66
FROM tblSurveyQuestions, [DEMO Lab Session Info]
WHERE ((([DEMO Lab Session Info].VisitID)=Forms!MyFormName!VisitID) And ((tblSurveyQuestions.SurveyID)=3));

In other words, the value of the form control needed to be the criterion of the matching field.
 

Users who are viewing this thread

Top Bottom