CodeItBetter Programming Another VB Programming Blog

How to Use Access data to make a Word table

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'Database - How to Use Access data to make a Word table
'This code uses data in an Access database to make a table in a Word document.
'It creates an ADO Connection and opens the database. It uses the Connection's
'Execute method to fetch the records of interest and loops through the
'Recordset 's Fields to add their names to a text string separated by tabs. Next
'the code calls the Recordset's GetString method to grab all of the data at
'once, separating fields with tabs and records with vbCrLf. At this point, the
'code has the data so it closes the Recordset and Connection.
'
'The program then makes a Range pointing to the end of the Word document, adds
'the text it has generated, and calls the Range's ConvertToTable method to make
'the table.

Sub ListCustomers()
    Dim db_name As String
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim new_field As ADODB.Field
    Dim txt As String
    Dim new_range As Range
 
    ' Compose the database name.
    db_name = Me.Path & "\Customers.mdb"
 
    ' Connect to the database.
    Set conn = New ADODB.Connection
    conn.Mode = adModeRead
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & db_name
    conn.Open
 
    ' Select all of the records in the Customers table.
    Set rs = conn.Execute("SELECT DISTINCT ContactName, Street, City, State, " & "Zip, Phone " & _
        "FROM CustomerAddresses ORDER BY ContactName")
 
    ' Add a row containing the field names.
    For Each new_field In rs.Fields
        txt = txt & vbTab & new_field.Name
    Next new_field
    txt = Mid$(txt, 2) & vbCrLf     ' Remove leading tab.

    ' Get the Recordset's data as a single string
    ' with vbTab between fields and vbCrLf between rows.
    txt = txt & rs.GetString(ColumnDelimeter:=vbTab, RowDelimeter:=vbCrLf, NullExpr:="<null>")
 
    ' Close the Recordset and Connection.
    rs.Close
    conn.Close
 
    ' Make a Range at the end of the Word document.
    Set new_range = ActiveDocument.Range
    new_range.Collapse wdCollapseEnd
 
    ' Insert the text and convert it to a table.
    new_range.InsertAfter txt
    new_range.ConvertToTable vbTab
 
    ' Autofit to the contents.
    new_range.Tables(1).AutoFitBehavior wdAutoFitContent
 
    ' Add a blank line.
    Set new_range = ActiveDocument.Range
    new_range.Collapse wdCollapseEnd
    new_range.InsertParagraph
    new_range.Collapse wdCollapseEnd
    new_range.InsertParagraph
End Sub
Filed under: Database Leave a comment
Comments (1) Trackbacks (0)
  1. can you give the detail like what control used,user defined etc.
    coz I already used it but seem not functioning + some error “Use-Defined type not defined”…in “Dim new_range As Range”

    Tq..


Leave a comment


 

No trackbacks yet.