Assigning value to control via dynamic name

E

Erik Nordstrom

Hi
I have stored values in a access database together with a portion of different control names in my word document. What I wan't to do is to assign the values in the database to the different controls (i.e. textbox, checkbox etc) dynamically by getting the names from the DB

example (pseudo code)

rs = GET_ALL RECORDS_IN_DATABAS
while NOT rs.EO
rs("VALUE_of_FIELD_with_control_name").TEXT = rs("VALUE_OF_FIELD_WITH_TEXT"
RS.MoveNex
Wen

If I knew the name of the control I would writ

txtTextField.Text = rs("VALUE_OF_FIELD_WITH_TEXT"

But with 280 fields I want to do this in a loop

Thank you in advanc
Eri
 
E

eowen

Just use the Controls collection of the Form in question

To use your pseudo-code example

rs = GET_ALL_RECORDS_IN_DATABAS
while not rs.EO
MyForm.Controls(rs("VALUE_of_FIELD_with_control_name")).Text = rs("VALUE_OF_FIELD_WITH_TEXT"
RS.MoveNex
Wen
 
E

Erik Nordstrom

Thanks eowen
I think I did a mistake in my description because these "controls" are not controls in a userform but rather controls in the sense that they were taken from the control toolbox in MS Word and placed in a word document. Thus there is no control collection in Thisdocument (if there is is how do I access it?)

All help appreciated in this matter since I can't find an answer anywhere


----- eowen wrote: ----

Just use the Controls collection of the Form in question

To use your pseudo-code example

rs = GET_ALL_RECORDS_IN_DATABAS
while not rs.EO
MyForm.Controls(rs("VALUE_of_FIELD_with_control_name")).Text = rs("VALUE_OF_FIELD_WITH_TEXT"
RS.MoveNex
Wen
 
E

eowen

Erik

Ah, I see. I have less experience there, but I think there is a CommandBars/CommandButtons/Commands collection that would function the same way. It is probably part of the Application object, not the ActiveDocument or ThisDocument object. I'd hunt around in that area of things

eowen
 
E

eowen

Actually, re-reading your posts again, I don't think I've pointed you in the right place. Nevertheless, what you should look for in the VBA object browser is some kind of collection that contains the controls in question. Such a collection probably exists, and you can probably access the controls within it by name using the CollectionName("name") syntax

If these controls are located within the document text itself, you may need to "walk" through the document range looking for them if there is not an overall collection for them

Good luck
eowen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top