P
pietlinden
I'm getting wise... I'm using CreateTableFromRecordset from ADH 2002
Desktop.
The only difference between what I am doing and what is done in the
book is that instead of sending a recordset to Word and converting it
to a table in a bookmark, I am sending it to a cell in an existing
table.
I can send it to the cell without a problem but then the
ConvertToTable command fails. I get a 2x2 nested table at the top of
each cell, and the delimited string never gets converted to a table.
Also, this only works *at all* if I comment out the code to include
field names.
Here's the code I am using:
Private Sub cmdCreateWordReport_Click()
Dim dtmStartTime As Date
Dim lngRetVal As Long ' for playsound api
'--Word variables
Dim boolWord As Boolean 'Quit Word?
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim wrdRange As Word.Range
Dim rngAny As Word.Range
Dim aCell As Word.Cell
Dim aPara As Word.Paragraph
'--record start time (just for information...)
'--show the system is busy...
DoCmd.Hourglass True
dtmStartTime = Now
'--variables for processing the query objects
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rsAny As ADODB.Recordset
Dim strObjectToOpen As String '---either the XTB table or a
select query
Dim aSelected() As Variant ' to put the selected items into the
array
Dim varItem As Variant ' to process the listbox items
Dim varFieldTypeList As Variant 'to format columns in the
table (store the field types, since they imply an alignment
' Show the "busy" label
Me.lblProcessing.Visible = True
Me.Repaint
Set appWord = New Word.Application
appWord.Documents.Add Application.CurrentProject.Path &
"\SummaryReport.dot"
Set docWord = appWord.ActiveDocument
With docWord.Bookmarks
.Item("StudyID").Range = GetCustomProperty("StudyID")
.Item("StudyID").Range.Bold = wdToggle
.Item("StudyManager").Range = GetCustomProperty
("StudyManager")
.Item("StudyTitle").Range = GetCustomProperty("StudyTitle")
.Item("StudyTitle").Range.Bold = wdToggle
End With
' Get details from database and create a table
' in the document
' Get an array filled with the selected items.
aSelected = mmp.SelectedItems
Set cnn = CurrentProject.Connection
Set rsAny = New ADODB.Recordset
For Each varItem In aSelected
'---the only queries that get into the collection are
dbQSelect and dbQCrosstab
If Left$(varItem, 2) = "--" Then
'it's a section, so put in the first column of the current
row
docWord.Tables(1).Cell(docWord.Tables(1).Rows.Count,
1).Range = Replace(varItem, "-", "")
Else
'--it's a normal query, so process as usual
If DBEngine(0)(0).QueryDefs(varItem).Type = dbQSelect Then
rsAny.Open varItem, cnn, adOpenStatic, adLockReadOnly,
adCmdStoredProc
Else
DBEngine(0)(0).Execute "DELETE * FROM
TEMP_XTB_RESULTS;", dbFailOnError
DBEngine(0)(0).Execute "qappXTB_to_TempTable",
dbFailOnError
rsAny.Open "TEMP_XTB_RESULTS", cnn, adOpenStatic,
adLockReadOnly, adCmdTable
End If
'-- Replace the underscores in query names with spaces
docWord.Tables(1).Cell(docWord.Tables(1).Rows.Count,
2).Range = Replace(varItem, "_", " ")
' this does not convert the delimited list in the cell to
a table.
' It does successfully write the delimited string into the
cell, though...
With CreateTableFromRecordset( _
docWord.Tables(1).Cell(docWord.Tables
(1).Rows.Count, 3).Range, _
rsAny, _
True)
End With
docWord.Tables(1).Rows.Add
If rsAny.State = adStateOpen Then
rsAny.Close
End If
End If
Next varItem
Set rsAny = Nothing
'--system is no longer busy!
DoCmd.Hourglass False
appWord.Visible = True
Set docWord = Nothing
Set appWord = Nothing
Me.SetFocus
Me.lblProcessing.Visible = False
Me.Repaint
lngRetVal = fPlayStuff("C:\WINDOWS\MEDIA\tada.wav")
MsgBox "Creating the report took " & DateDiff("s", dtmStartTime,
Now) & " seconds.", vbOKOnly + vbInformation
End Sub
For some odd reason, in the cells on which I use
CreateTableFromRecordset, I get a two-column nested table with nothing
in the cells and then a delimited list of the result of the GetString
command.
If I try to include the field names when I call the function, it
errors out in here...
For Each fldAny In rstAny.Fields
cField = cField + 1
.Cell(1, cField).Range.Text = fldAny.Name
Next
It throws "Error 5941: The requested member of the collection does not
exist." (Because rsAny is no longer open? But that doesn't make
sense, because the contents of the recordset get written to the
cell...)
Any clue what I am doing wrong?
Thanks!
Pieter
Desktop.
The only difference between what I am doing and what is done in the
book is that instead of sending a recordset to Word and converting it
to a table in a bookmark, I am sending it to a cell in an existing
table.
I can send it to the cell without a problem but then the
ConvertToTable command fails. I get a 2x2 nested table at the top of
each cell, and the delimited string never gets converted to a table.
Also, this only works *at all* if I comment out the code to include
field names.
Here's the code I am using:
Private Sub cmdCreateWordReport_Click()
Dim dtmStartTime As Date
Dim lngRetVal As Long ' for playsound api
'--Word variables
Dim boolWord As Boolean 'Quit Word?
Dim appWord As Word.Application
Dim docWord As Word.Document
Dim wrdRange As Word.Range
Dim rngAny As Word.Range
Dim aCell As Word.Cell
Dim aPara As Word.Paragraph
'--record start time (just for information...)
'--show the system is busy...
DoCmd.Hourglass True
dtmStartTime = Now
'--variables for processing the query objects
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rsAny As ADODB.Recordset
Dim strObjectToOpen As String '---either the XTB table or a
select query
Dim aSelected() As Variant ' to put the selected items into the
array
Dim varItem As Variant ' to process the listbox items
Dim varFieldTypeList As Variant 'to format columns in the
table (store the field types, since they imply an alignment
' Show the "busy" label
Me.lblProcessing.Visible = True
Me.Repaint
Set appWord = New Word.Application
appWord.Documents.Add Application.CurrentProject.Path &
"\SummaryReport.dot"
Set docWord = appWord.ActiveDocument
With docWord.Bookmarks
.Item("StudyID").Range = GetCustomProperty("StudyID")
.Item("StudyID").Range.Bold = wdToggle
.Item("StudyManager").Range = GetCustomProperty
("StudyManager")
.Item("StudyTitle").Range = GetCustomProperty("StudyTitle")
.Item("StudyTitle").Range.Bold = wdToggle
End With
' Get details from database and create a table
' in the document
' Get an array filled with the selected items.
aSelected = mmp.SelectedItems
Set cnn = CurrentProject.Connection
Set rsAny = New ADODB.Recordset
For Each varItem In aSelected
'---the only queries that get into the collection are
dbQSelect and dbQCrosstab
If Left$(varItem, 2) = "--" Then
'it's a section, so put in the first column of the current
row
docWord.Tables(1).Cell(docWord.Tables(1).Rows.Count,
1).Range = Replace(varItem, "-", "")
Else
'--it's a normal query, so process as usual
If DBEngine(0)(0).QueryDefs(varItem).Type = dbQSelect Then
rsAny.Open varItem, cnn, adOpenStatic, adLockReadOnly,
adCmdStoredProc
Else
DBEngine(0)(0).Execute "DELETE * FROM
TEMP_XTB_RESULTS;", dbFailOnError
DBEngine(0)(0).Execute "qappXTB_to_TempTable",
dbFailOnError
rsAny.Open "TEMP_XTB_RESULTS", cnn, adOpenStatic,
adLockReadOnly, adCmdTable
End If
'-- Replace the underscores in query names with spaces
docWord.Tables(1).Cell(docWord.Tables(1).Rows.Count,
2).Range = Replace(varItem, "_", " ")
' this does not convert the delimited list in the cell to
a table.
' It does successfully write the delimited string into the
cell, though...
With CreateTableFromRecordset( _
docWord.Tables(1).Cell(docWord.Tables
(1).Rows.Count, 3).Range, _
rsAny, _
True)
End With
docWord.Tables(1).Rows.Add
If rsAny.State = adStateOpen Then
rsAny.Close
End If
End If
Next varItem
Set rsAny = Nothing
'--system is no longer busy!
DoCmd.Hourglass False
appWord.Visible = True
Set docWord = Nothing
Set appWord = Nothing
Me.SetFocus
Me.lblProcessing.Visible = False
Me.Repaint
lngRetVal = fPlayStuff("C:\WINDOWS\MEDIA\tada.wav")
MsgBox "Creating the report took " & DateDiff("s", dtmStartTime,
Now) & " seconds.", vbOKOnly + vbInformation
End Sub
For some odd reason, in the cells on which I use
CreateTableFromRecordset, I get a two-column nested table with nothing
in the cells and then a delimited list of the result of the GetString
command.
If I try to include the field names when I call the function, it
errors out in here...
For Each fldAny In rstAny.Fields
cField = cField + 1
.Cell(1, cField).Range.Text = fldAny.Name
Next
It throws "Error 5941: The requested member of the collection does not
exist." (Because rsAny is no longer open? But that doesn't make
sense, because the contents of the recordset get written to the
cell...)
Any clue what I am doing wrong?
Thanks!
Pieter