PDA

View Full Version : Looping in a Word VBA procedure



Roderick
06-03-2013, 08:00 AM
I have a Word 2010 template and in the XML I'm using shared callbacks for my labels, quicktips and supertips. Here, I'm just going to concentrate on the shared getLabel callback.

This is the code I've created with kind help from users of this forum:
Dim dbRibbonData As DAO.Database
Dim rdShippers As Recordset
Dim intRecords As Integer
Dim myString As String
Dim strSQL As String
Dim myLanguage As String
Dim myFieldCode As String
Dim rxMyLabel As String

'control.id comes from my Ribbon XML shared callback
rxMyLabel = control.id

Set dbRibbonData = OpenDatabase _
(Name:="D:\DATA Samsung\myClient\RibbonX Project\" _
& "RibbonData.accdb")

'here the language is set manually - later it will be programatically set
myLanguage = "French"

'search the database in the 'Template_Labels' table in the 'French' field for the 'control.id' record
strSQL = "Select " & myLanguage & " FROM [Template_Labels] where [Field_Code] = '" & rxMyLabel & "'"

Set rdShippers = dbRibbonData.OpenRecordset(strSQL, dbOpenDynaset)

'return the value back to the Ribbon XML
returnedVal = rdShippers.Fields(0)

rdShippers.Close
dbRibbonData.Close
Now, if instead of rxMyLabel = control.id as above, I remove control.id and enter a required field code I get the correct answer back in the 'returnedVal' variable. But this is just a one-shot effort.

What I now want to do is to create a loop in the above procedure so that as 'control.id' loops through the XML it also loops through the database and returns the correct value by means of the 'returnedVal' variable.

My problem is that I don't know where to put my loop statements in the code above for this to work correctly. I should think it would also be a Do. . .Loop, perhaps?

Can anyone offer a suggestion, please?

Roderick

gmaxey
06-03-2013, 12:30 PM
Instead of going to your database every time you need a bit of data, why don't you go to your databas once when the ribbon loads and stored all its contents in an array. This way, as your GetLabel, GetTip etc. events index you can get the value from the appropriate array index.

Roderick
06-03-2013, 01:00 PM
Thanks, Greg, for your suggestion. It's only that I felt more secure using a "tangible" asset (so to speak).

I'll look into this point of arrays.

Roderick