Word Programming in VB6

I want to use word to display address informtaion in a database. That is , I want when I click cmdAddress to open the database, open a word document , insert a table of 5 rows and 4 columns in the word document and display the first 5 records in the recordset in the table.
each record in the recordset contains 4 fields. fname, lname, address, phone

I Want word to lok like this.
------------------------------------
|rs!fname|rs!lname|rs!address|rs!Phone|
------------------------------------
|fname2 | lname2| address2| Phone2 |
------------------------------------
|fname3 | lname3| address3| Phone3 |
------------------------------------
|fname4 | lname4| address4| Phone4 |
------------------------------------
|fname5 | lname5| address5| Phone5 |
------------------------------------

Private Sub cmdAddress_click()

Dim db As Database
Dim rs As recordset
Dim strDB As String
strDB = "c:\db1.MDB"
Set db = DBEngine.OpenDatabase(strDB)
Set rs = db.OpenRecordset("Address", dbOpenDynaset

Dim MyWord As Word.Application
Dim MyDoc As Word.document
Set MyWord = New Word.Application

Set MyDoc = MyWord.documents.Add()

add word a table of 5 rows and 4 columns called tble

for i = 1 to i = 5
    MyDoc.tble(i, 1) = rs!fname
    MyDoc.tble(i, 2) =rs!lname
    MyDoc.tble(i, 3) = rs!address
    MyDoc.table(i, 4) = rs!phone
next i


End Sub




yassin092898Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

timytleeCommented:
'Add a new table (Row x Col)
'at the end of the document.

Sub WordAddTable(TotRow As Integer, TotCol As Integer)

Dim WordRange As Word.Range
Dim LastTable As Integer
   
LastTable = ActiveDocument.Tables.Count
   
Set WordRange = ActiveDocument.Range(ActiveDocument.Range.End - 1, ActiveDocument.Range.End)

ActiveDocument.Tables.Add Range:=WordRange, NumRows:=TotRow, numColumns:=TotCol

End Sub
0
yassin092898Author Commented:
Can you rewrite the code I provided so that it accomplishes the following.

-Open the database
-create a word document
-insert a table of 5 rows and 4 columns in the word document
-insert each field of the recodset in the cells of the table in the word document.

The database recordset contains only 5 records each has 4 fields(fname, lname, address, phone)

Here is my first attempt. Note it might not be complete and the syntax is not correct in someplace.


Private Sub cmdAddress_click()

Dim db As Database
Dim rs As recordset
Dim strDB As String
strDB = "c:\db1.MDB"
Set db = DBEngine.OpenDatabase(strDB)
Set rs = db.OpenRecordset("Address", dbOpenDynaset

Dim MyWord As Word.Application
Dim MyDoc As Word.document
Set MyWord = New Word.Application

Set MyDoc = MyWord.documents.Add()

add word a table of 5 rows and 4 columns called tble

for i = 1 to i = 5
    MyDoc.tble(i, 1) = rs!fname
    MyDoc.tble(i, 2) =rs!lname
    MyDoc.tble(i, 3) = rs!address
    MyDoc.table(i, 4) = rs!phone
next i


End Sub

When all is done, I want a word document containing a tablse displaying the fields in the database recordset.

Please note I a beginner in VBA programming.
0
yassin092898Author Commented:
I don't really understand your solution and I am going to reject.

All I want is a VB Code that
1 - creates a word Document
2 - Inserts a table of 5 rows & 4 columns
3 - put texts in each field of the table in the document.

I would be happy to award you the points and top mark if you are anwer is complete and written for a beginner.

Thanks
0
timytleeCommented:
'well since you are a beginner,
'I hope you understand the code below:

Option Explicit
Dim db As Database
Dim rs As Recordset
Dim MyWord As Word.Application
Dim MyDoc As Word.Document
Dim WordRange As Word.Range
Dim LastTable As Integer
Dim MyTable As Word.Table

Sub WordAddTable(TotRow As Integer, TotCol As Integer)
   
    LastTable = ActiveDocument.Tables.Count
   
    Set WordRange = ActiveDocument.Range(ActiveDocument.Range.End - 1, ActiveDocument.Range.End)
    Set MyTable = ActiveDocument.Tables.Add(Range:=WordRange, NumRows:=TotRow, numColumns:=TotCol)
   
End Sub

Private Sub cmdAddress_click()
Dim i As Integer
'add word a table of 5 rows and 4 columns called tble
WordAddTable 5, 4
For i = 1 To 5
    MyTable.Cell(i, 1).Range.InsertAfter rs!fname
    MyTable.Cell(i, 2).Range.InsertAfter rs!lname
    MyTable.Cell(i, 3).Range.InsertAfter rs!Address
    MyTable.Cell(i, 4).Range.InsertAfter rs!phone
    rs.MoveNext
Next i


End Sub


Private Sub cmdCallWord_Click()
    Set MyWord = CreateObject("Word.Application")
    Set MyDoc = MyWord.Documents.Add
    MyWord.Visible = True
    MyWord.WindowState = wdWindowStateMaximize
End Sub

Private Sub cmdExit_Click()
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing
    Unload Me
End Sub

Private Sub cmdOpenDb_Click()
Dim strDB As String
strDB = "c:\db1.MDB"
Set db = OpenDatabase(strDB)
Set rs = db.OpenRecordset("Address", dbOpenDynaset)

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.