How do I get data from MySQL ?

B

bchin

Is there a way to get data from a MySQL database? Then can I insert the data
into Word in some non-table format? I want to be able to place some data
within paragraphs of my document.
 
L

Leigh

Sure. In a VBA module, add a reference to "Microsoft ActiveX Data
Objects 2.x Library" (Tools, References). Then in your code, do
something like this:

Sub DataInsert()
Dim loConnection As New ADODB.Connection
Dim loRecordset As New ADODB.Recordset
Dim loRange As Word.Range

loConnection.Open ("MyDsnName")
With loRecordset
Set .ActiveConnection = loConnection
..Open ("SELECT * FROM MyTableName")
Set loRange = Selection.Range.Duplicate
While Not .EOF
loRange.InsertAfter .Fields("MyFieldName") & vbCrLf
..MoveNext
Wend
..Close
End With
loConnection.Close
Set loRange = Nothing
Set loRecordset = Nothing
Set loConnection = Nothing
End Sub

This code assumes you have a Data Source Name called "MyDsnName"
already defined, and that it has a table or view in it called
MyTableName with a field called MyFieldName.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top