Categories

Tools

bitdefender

1&1 Web Hosting

Example of how you can use, programmatically, data from a Microsoft Access database in Word, using Visual Basic


This simple example written in Visual Basic, allows you to read data from an Access database, using the stored procedure spSelectCustomers, and insert the extracted data into a new Word document.

Code:

Sub OpenForm()
    Dim wordDocument As Document
    Dim dbNorthwind As DAO.Database
    Dim recordsetCustomers As Recordset
    Dim intRecords As Integer

    Set wordDocument = Documents.Add
    Set dbNorthwind = OpenDatabase(Name:="C:\Database\Northwind.mdb")
    Set recordsetCustomers = dbNorthwind.OpenRecordset(Name:="spSelectCustomers")
    
    wordDocument.Content.InsertAfter Text:="List of Customers"
    wordDocument.Content.InsertParagraphAfter
    
    For intRecords = 0 To recordsetCustomers.RecordCount - 1
        wordDocument.Content.InsertAfter Text:=recordsetCustomers.Fields(0).Value & " - "
        wordDocument.Content.InsertAfter Text:=recordsetCustomers.Fields(1).Value & " ( "
        wordDocument.Content.InsertAfter Text:=recordsetCustomers.Fields(2).Value & " ) "
        recordsetCustomers.MoveNext
        wordDocument.Content.InsertParagraphAfter
    Next intRecords
    
    recordsetCustomers.Close
    dbNorthwind.Close
    
End Sub
Posted in VBScript by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.