i could also write the values to a temporary or not temporary table before printing them. my VBA isn't that strong tho. i'm not sure how to create a table object and write to it w VBA
I modified your code snippet to write to a temp table first. THIS IS UNTESTED CODE!! Try this on a COPY of your database.
Start by creating a table named "TmpFormDesc" with two text fields - "FormName" & "FormDesc"
Then look at the following code.
The gold text is basic declarations. You should already have these
The blue is what I added.
Code:
Option Compare Database 'should always have these two lines
Option Explicit
Public Sub Form2Word()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim varCheckBoxTagProp As String 'These I added to create the sub - you should already have them
Dim varControlName As String
Dim varFormName As String
Dim varFormDescription As String
'--------beg modified code---------
Set dbs = CurrentDb '<<- only needs to be set once
'clear the temp table
strSQL = "Delete * FROM TmpFormDesc;"
dbs.Execute strSQL, dbFailOnError
'fill the tmp table with form names and descriptions
For Each Ctrl In Me![sfmStateForms].Form.Controls
varCheckBoxTagProp = Ctrl.Tag
If TypeOf Ctrl Is CheckBox And varCheckBoxTagProp = "Mandatory" Then
If Ctrl.Value = True Then
varControlName = Ctrl.Name
strSQL = "SELECT FormDescription, FormName, ControlName "
strSQL = strSQL & " FROM tblFormDescriptions"
strSQL = strSQL & " WHERE ControlName = '" & varControlName & "';"
Set rst = dbs.OpenRecordset(strSQL)
varFormDescription = rst!FormDescription
varFormName = rst!FormName
'insert into the tmp table
strSQL = "INSERT INTO Account ( FormName, FormDesc ) "
strSQL = strSQL & " VALUES ('" & varFormName & "', '" & varFormDescription & "');"
dbs.Execute strSQL, dbFailOnError
End If
End If
Next
rst.Close
'open a sorted recordset
strSQL = "SELECT FormName, FormDesc"
strSQL = strSQL & " FROM TmpFormDesc"
strSQL = strSQL & " ORDER BY FormName"
Set rst = dbs.OpenRecordset(strSQL)
'check if there are records returned.
If Not (rst.BOF And rst.EOF) Then
rst.MoveLast
rst.MoveFirst
Do While Not rst.EOF
'write to Word
objWord.Selection.TypeText " " & rst!FormName & vbTab & Left(rst!FormDesc, 79) & vbCr
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
Set dbs = Nothing
'--------end modified code---------
End Sub
Remember - this is untested