HELP* SQL data into Word

J

JB

I have pop up box and asks say for someones initials. Then I want to
query an SQL db and merge the word document with data, e.g. address
details or phone number.

I know some SQL and VBA but can't get the document to connect via ODBC
and return and populate the results. Is there a better VBA way than
using ODBC? Can anyone help by supplying a bit of sample code to help
me get going?

I'm beating my head against a brick wall and desperate, DESPERATE for
a solution!!!!

HELP!!!!!

Many thanks in advance.
 
D

dsc

Private Sub UserForm_Initialize()
Dim i As Single
'The 1st list box contains 2 data columns
ListBox1.ColumnCount = 2
ListBox1.ColumnWidths = "175;300"
Dim MyArray(50, 2) As String

Dim myDataBase As Database
Dim myActiveRecord As Recordset
Dim NameString As String
Dim DefString As String

'Check to see if any text is selected
If Selection.Type = wdSelectionNormal Then
srchTxt = Selection

End If

'Open a database

Set myDataBase = OpenDatabase("\\Japanese\japanese c\DataBase.mdb")
'Access the first record from a particular table

Set myActiveRecord = myDataBase.OpenRecordset("Table_1", dbOpenTable)

'Loop through all the records in the table until the end-of-file marker is
reached
'Load values into MyArray

Do While Not myActiveRecord.EOF
'MsgBox (i)
If myActiveRecord.Fields("Field 1") = srchTxt Then
DefString = myActiveRecord.Fields("Field 2")
'MsgBox (DefString)
MyArray(i, 0) = DefString
If Not myActiveRecord.Fields("Field 3") = "" Then
NameString = myActiveRecord.Fields("Field 3")
End If
'MsgBox (NameString)
MyArray(i, 1) = NameString
i = i + 1
End If
myActiveRecord.MoveNext
Loop

'Then close the database
myActiveRecord.Close
myDataBase.Close
Set myActiveRecord = Nothing
Set myDataBase = Nothing

If i > 0 Then
'Load data into ListBox1
ListBox1.List() = MyArray

ElseIf i = 0 Then
'Clean up and close out
dscGlossaryForm3.Hide
dscGlossaryForm5.Show
End
End If

End Sub
 

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