How do you dynamically create controls on a MS Access form?
Asked Answered
P

1

2

I am writing a survey (data input) form in MS Access. I need to dynamically display all the questions in the questions table in labels on my form. Each of these question labels will also be accompanied by a combo box that will display possible answers.

How can I add labels to a form dynamically?

Pickar answered 2/11, 2011 at 3:2 Comment(0)
B
8

See the CreateControl Method. For a label, use acLabel from the AcControlType Enumeration as the ControlType parameter for CreateControl.

However, this will get more complicated. In addition to creating the label control you will want to set its caption, position (Top and Left properties), and size (Width and Height properties). Perhaps other properties as well: Font Name; Font Size; Font Weight; etc.

It could be easier to structure your data source so it contains a row for each question and answer pair. Then present those rows in a continuous form with 2 controls: a text box for the question; and the combo box for the answer. Then you wouldn't need to bother with creating controls dynamically.

Bushtit answered 2/11, 2011 at 5:22 Comment(4)
Thanks for your reply. Are you suggesting that I just make a table with the questions as columns so that the form can generate this? That would be horrible database design but this is Access after all. :)Pickar
Not a separate column for each question. A Questions table with 2 columns: question_id; and question_text. An Answers table with person_id, question_id, and answer. Use a query to join the 2 tables on question_id and filter the result set on person_id. Or if you find it more convenient, display the questions in a main form and the answers in a subform. Whichever method you prefer should be a lot less effort than creating data controls at run time.Bushtit
I see. The problem I have is that for a single Survey I want the user to answer several questions. I cannot figure out how to create a Survey Form that has a subform that allows the user to input answers to many questions per survey form. Does that make sense? Thanks for your help.Pickar
In that case, check out Duane Hookom's At Your Survey application. His terms are "Steal ideas and methods but not the whole of the application." rogersaccesslibrary.com/forum/topic3.htmlBushtit

© 2022 - 2024 — McMap. All rights reserved.