PC Review


Reply
Thread Tools Rate Thread

Code to fill Word form for multiple records from Access

 
 
rollininclover
Guest
Posts: n/a
 
      16th Mar 2010
This is the code I've been working with, but it freezes Access:

Private Sub Command66_Click()
'Print Physician Profile.
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set rs = Db.OpenRecordset
rst.Open Me.RecordSource, CurrentProject.Connection
'Cycle through records to fill Word form fields
Do While Not rst.EOF
Set doc = appWord.Documents.Open("Z:\My Documents\ACCESS\Access to
Word.doc", , True)
With doc
..FormFields("fldMnemonic").Result = rst!Mnemonic
..FormFields("fldCMAMIPct").Result = rst!CMAMIPct
..FormFields("fldCMCHFPct").Result = rst!CMCHFPct
..FormFields("fldCMPNEPct").Result = rst!CMPNEPct
..FormFields("fldCMSCIPPct").Result = rst!CMSCIPPct
..FormFields("fldCMAMIOPPct").Result = rst!CMAMIOPPct
..FormFields("fldCMSURGOPPct").Result = rst!CMSURGOPPct
..FormFields("fldCMAMINum").Result = rst!CMAMINum
..FormFields("fldCMCHFNum").Result = rst!CMCHFNum
..FormFields("fldCMPNENum").Result = rst!CMPNENum
..FormFields("fldCMSCIPNum").Result = rst!CMSCIPNum
..FormFields("fldCMAMIOPNum").Result = rst!CMAMIOPNum
..FormFields("fldCMSURGOPNum").Result = rst!CMSURGOPNum
..FormFields("fldCMAMIDen").Result = rst!CMAMIDen
..FormFields("fldCMCHFDen").Result = rst!CMCHFDen
..FormFields("fldCMPNEDen").Result = rst!CMPNEDen
..FormFields("fldCMSCIPDen").Result = rst!CMSCIPDen
..FormFields("fldCMAMIOPDen").Result = rst!CMAMIOPDen
..FormFields("fldCMSURGOPDen").Result = rst!CMSURGOPDen
..Visible = True
..Activate
..SaveAs "4Q 09 " & Me!Mnemonic
rst.MoveNext
End With
Loop
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub


 
Reply With Quote
 
 
 
 
rollininclover
Guest
Posts: n/a
 
      16th Mar 2010
Hi, John, thanks for the help. I changed rs to rst, but Access only produces
one Word form and then locks up. Any other ideas?

Thanks, again, for your help.

"J_Goddard via AccessMonster.com" wrote:

> Hi -
>
> You are using Set rs = Db.OpenRecordset, but then you use rst to open and
> loop. The Set statement I showed should be Set rst = Db.OpenRecordset.
>
> I recommend you use Option Explicit in all your code modules, to force all
> variables to be explitly declared. You code would not compile if you had
> used it - you would be amazed at how many errors it catches.
>
> John
>
>
> rollininclover wrote:
> >This is the code I've been working with, but it freezes Access:
> >
> >Private Sub Command66_Click()
> >'Print Physician Profile.
> >Dim appWord As Word.Application
> >Dim doc As Word.Document
> >'Avoid error 429, when Word isn't open.
> >On Error Resume Next
> >Err.Clear
> >'Set appWord object variable to running instance of Word.
> >Set appWord = GetObject(, "Word.Application")
> >If Err.Number <> 0 Then
> >'If Word isn't open, create a new instance of Word.
> >Set appWord = New Word.Application
> >End If
> >Set rs = Db.OpenRecordset
> >rst.Open Me.RecordSource, CurrentProject.Connection
> >'Cycle through records to fill Word form fields
> >Do While Not rst.EOF
> >Set doc = appWord.Documents.Open("Z:\My Documents\ACCESS\Access to
> >Word.doc", , True)
> >With doc
> >.FormFields("fldMnemonic").Result = rst!Mnemonic
> >.FormFields("fldCMAMIPct").Result = rst!CMAMIPct
> >.FormFields("fldCMCHFPct").Result = rst!CMCHFPct
> >.FormFields("fldCMPNEPct").Result = rst!CMPNEPct
> >.FormFields("fldCMSCIPPct").Result = rst!CMSCIPPct
> >.FormFields("fldCMAMIOPPct").Result = rst!CMAMIOPPct
> >.FormFields("fldCMSURGOPPct").Result = rst!CMSURGOPPct
> >.FormFields("fldCMAMINum").Result = rst!CMAMINum
> >.FormFields("fldCMCHFNum").Result = rst!CMCHFNum
> >.FormFields("fldCMPNENum").Result = rst!CMPNENum
> >.FormFields("fldCMSCIPNum").Result = rst!CMSCIPNum
> >.FormFields("fldCMAMIOPNum").Result = rst!CMAMIOPNum
> >.FormFields("fldCMSURGOPNum").Result = rst!CMSURGOPNum
> >.FormFields("fldCMAMIDen").Result = rst!CMAMIDen
> >.FormFields("fldCMCHFDen").Result = rst!CMCHFDen
> >.FormFields("fldCMPNEDen").Result = rst!CMPNEDen
> >.FormFields("fldCMSCIPDen").Result = rst!CMSCIPDen
> >.FormFields("fldCMAMIOPDen").Result = rst!CMAMIOPDen
> >.FormFields("fldCMSURGOPDen").Result = rst!CMSURGOPDen
> >.Visible = True
> >.Activate
> >.SaveAs "4Q 09 " & Me!Mnemonic
> >rst.MoveNext
> >End With
> >Loop
> >Set doc = Nothing
> >Set appWord = Nothing
> >Exit Sub
> >errHandler:
> >MsgBox Err.Number & ": " & Err.Description
> >End Sub

>
> --
> John Goddard
> Ottawa, ON Canada
> jrgoddard at cyberus dot ca
>
> Message posted via AccessMonster.com
> http://www.accessmonster.com/Uwe/For...dules/201003/1
>
> .
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Taking a Word form and using it to fill in parts of an Access form AmandaH Microsoft Access 8 27th Jul 2006 08:26 PM
Fill in Word Form using information stored in Access Sub-Form =?Utf-8?B?ZHVtZHVt?= Microsoft Access VBA Modules 1 29th Jun 2006 04:58 PM
How do I merge multiple Access records into a word form letter? =?Utf-8?B?U3VtbWVyNzI=?= Microsoft Access 7 12th Mar 2006 04:39 AM
Access 2000 form - fill in multiple controls with single input =?Utf-8?B?QXJjaGlkdWRl?= Microsoft Access Forms 0 1st May 2005 04:10 PM
Can Access be used to fill out a form in Word? =?Utf-8?B?Sm9lbCBOb3J3b29k?= Microsoft Access 1 30th Sep 2004 12:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:01 PM.