Dani Valverde
Dani Valverde

Reputation: 409

How to fill a Word 2016 form with data stored in an Access 2016 database.?

I am working on a small project and I need to be able to fill a Word 2016 form with data stored in an Access 2016 database. To do so, I have created a form (Customers) with a button (Save). When clicking the button, the data from the form whould be saved to the table (Customers) and also sent to the Word form. This is the code I have following the instructions from Techrepublic.

Private Sub cmdPrint_Click()

'Print customer slip for current customer.

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 doc = appWord.Documents.Open("E:\Usuarios\Acrocephalus\Daniel\Projectes\AnemaDB\ProgramaControlDDD2015.docx", , True)

With doc

.FormFields("fldIDClient").Result = Me!IDClient

.FormFields("fldNomLocal").Result = Me!NomLocal

.FormFields("fldNomFiscal").Result = Me!NomFiscal

.FormFields("fldNIF_CIF").Result = Me!NIF_CIF

.FormFields("fldTipusVia").Result = Me!TipusVia

.FormFields("fldAdreca").Result = Me!Adreca

.FormFields("fldCP").Result = Me!CP

.FormFields("fldMunicipi").Result = Me!Municipi

.FormFields("fldProvincia").Result = Me!Provincia

.FormFields("fldFix").Result = Me!Fix

.FormFields("fldMobile").Result = Me!Mobile

.FormFields("fldemail").Result = Me!email
.Visible = True

.Activate

End With

Set doc = Nothing

Set appWord = Nothing

Exit Sub

errHandler:

MsgBox Err.Number & ": " & Err.Description

End Sub

However, when I fill in the data and click on the button I have 3 problems:

  1. It doesn't save any data into the table
  2. It doesn't fill the Word
  3. I have to press the button twice to open the form.

Can anyone help? Thanks!!

Dani

Upvotes: 0

Views: 4769

Answers (1)

CyberClaw
CyberClaw

Reputation: 445

Are you using the correct References in VBA?

On the VBA Editor press Tools > References, and put a check before Microsoft Work xx.0 Object Library

Upvotes: 1

Related Questions