Results 1 to 8 of 8

Thread: [RESOLVED] ms access create and feed a table in ms word

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    586

    Resolved [RESOLVED] ms access create and feed a table in ms word

    I'm sure I've seen this somewhere, but I can't find where now...

    I have an ms access application , and want to create a word document with a table based on a query.

    I know how to add single values, and how to go over a query values, but how do I create a table that is as long as the number of the records in my query and feed its values?

    I can do this very ugly with something like:
    VB Code:
    1. Set qdf = CurrentDb.QueryDefs("query1")
    2.     qdf("my_station_no") = station_no
    3.     Set rs = qdf.OpenRecordset
    4.    
    5.     rs.MoveFirst
    6.      If rs.EOF Then goto bypass  
    7.        MyDoc.variables("Item1") = Nz(rs("item_type_name"), "")
    8.        MyDoc.variables("Amount1") = Nz(rs("ramount"), "")
    9.     rs.MoveNext
    10.     If rs.EOF Then  goto bypass  
    11.    
    12.        MyDoc.variables("Item2") = Nz(rs("item_type_name"), "")
    13.        MyDoc.variables("Amount2") = Nz(rs("ramount"), "")
    14.     rs.MoveNext
    and so on as long as I have records, but needless to say I don't want it this way.

    anyway I can create an array of fields in ms word lined up in a table, or something similar?
    Last edited by Hack; May 15th, 2006 at 01:39 PM. Reason: Added [vbcode] [/vbcode] tags for more clarity.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: ms access create and feed a table in ms word

    Goto?

    You just need a simple loop (and do not need MoveFirst - recordsets always go there by default), eg:
    VB Code:
    1. Set qdf = CurrentDb.QueryDefs("query1")
    2.     qdf("my_station_no") = station_no
    3.     Set rs = qdf.OpenRecordset
    4.  
    5. Dim lngRow as Long
    6.     lngRow = 1
    7.     Do While Not rs.EOF
    8.        MyDoc.variables("Item" & lngRow) = Nz(rs("item_type_name"), "")
    9.        MyDoc.variables("Amount" & lngRow) = Nz(rs("ramount"), "")
    10.        rs.MoveNext
    11.        lngRow = lngRow + 1
    12.     Loop
    13. '..everything after "bypass"

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    586

    Re: ms access create and feed a table in ms word

    OK , what about the word documet - how do I create a table with Item0..n and Amout0..n?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: ms access create and feed a table in ms word

    I presumed you already had that, otherwise I would have recommended what I am about to - create a table with one row, and add a new row just before the "Loop" line (if not at EOF).

    Instead of setting "MyDoc.variables("name")", you would just set "MyTable.Cells(row, col)" (or something like that - I haven't used Word in a while).

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    586

    Re: ms access create and feed a table in ms word

    do you mean add the table line in word from access?
    I guess I could probably record a macro in word to use its code for creating a new line from access... I'll try it soon

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: ms access create and feed a table in ms word

    That's exactly what I meant, and would have suggested the macro if I had been more awake!

    If you have problems converting the macro to code for your program, post it here and we'll show you whats needed.

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: ms access create and feed a table in ms word

    Here is an example of building a table dynamically in word. The code in this example uses an array to build the table, but it can be easily adapted to work with a recordset.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    586

    Re: ms access create and feed a table in ms word

    exactly what I need!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width