TryingMyBest
Registered User.
- Local time
- Today, 00:08
- Joined
- Nov 18, 2004
- Messages
- 54
Hope you can help with this one....I'm sure you can!
In my database I want to open a word document that has a table populated from data depending on the current record that's open.
The menu item to open the document and populate some bookmarks is as follows:
This is ok and opens the document, populating the necessary fields...except for the wbscode one but I know what's wrong with that...I've been messing!
Within the word document I picked up the following code from a link on this forum...
If the database is closed then the code runs and doesn't return any records as the query is based on a field on a form. If the database is open then the code stops with an error to the effect that the database is locked.
I pretty sure this is because it's trying to open the database rather than use the already open database....so what is the code to create a recordset from a query that is already populated with the data?
Cheers
Jo
In my database I want to open a word document that has a table populated from data depending on the current record that's open.
The menu item to open the document and populate some bookmarks is as follows:
Code:
Function ShowNCRProjectTeamReport()
'On Error Resume Next
Dim objWord As Object
Dim Reference As Object
Set Reference = [Forms]![frmNonConformities]![txtPSTName]
If IsNull(Reference) Then
MsgBox "Error"
Else
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Add Template:=(CurrentProject.Path & "\NCRTemplateTemp.dot")
objWord.ActiveDocument.Bookmarks.Item("ProjectTeamName").Range.Text = Reference
objWord.ActiveDocument.Bookmarks.Item("ProjectTeamName1").Range.Text = Reference
objWord.ActiveDocument.Bookmarks.Item("ProjectTeamName2").Range.Text = Reference
objWord.ActiveDocument.Bookmarks.Item("WBSCode").Range.Text = DLookup("WBSCode", "NonConformanceReport")
End If
End Function
This is ok and opens the document, populating the necessary fields...except for the wbscode one but I know what's wrong with that...I've been messing!
Within the word document I picked up the following code from a link on this forum...
Code:
Private Sub Document_New()
Dim dbe As DAO.DBEngine
Dim wks As DAO.Workspace
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strDBName As String
'On Error Resume Next
strDBName = "U:\APT\Release 3\Audit_Planning_Tool.mdb"
Set dbe = CreateObject("DAO.DBEngine.36")
Set wks = dbe.Workspaces(0)
Set dbs = wks.OpenDatabase(strDBName)
Set rst = dbs.OpenRecordset("NonConformanceReport")
Selection.GoTo What:=wdGoToBookmark, Name:="NCRNumber"
With rst
Do While Not .EOF
Selection.TypeText Text:=![NCRNumber]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![Description]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![DeficiencyLevel]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![AgreedAction]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![ActionOwner]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![AgreedCompletionDate]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![Progress]
Selection.MoveRight Unit:=wdCell
.MoveNext
Loop
.Close
End With
Selection.SelectRow
Selection.Rows.Delete
End Sub
If the database is closed then the code runs and doesn't return any records as the query is based on a field on a form. If the database is open then the code stops with an error to the effect that the database is locked.
I pretty sure this is because it's trying to open the database rather than use the already open database....so what is the code to create a recordset from a query that is already populated with the data?
Cheers
Jo