Results 1 to 10 of 10
  1. #1
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44

    Automation Access-Word (wrestling with code... and he's winning!)

    Hi all. I am new in this forum and a newbie on Access (so please be kind and clear to me). I am from Italy, and I am building a database for my office.
    In one of my forms I have three buttons leading to a word model (.odtx) with bookmarks. Through an automation, Access populate the Word model with the records included in the main form (based on a teachers anagraphical data table) and in a related subform (based on a lectures and classes for each teacher data table). Everything looks like that (fake data as example, three buttons right side):



    Click image for larger version. 

Name:	form.jpg 
Views:	26 
Size:	139.5 KB 
ID:	33055

    The code managing the automation Access-Word is the following (I kept only the essential lines, without all bookmarks repetition):

    Code:
    Private Sub ESPORTA_OCCASIONALE_Click()
    Dim Wrd As Word.Application, Doc As Word.Document
    Dim Rst As DAO.Recordset
    Dim Modello As String, NomeFile As String, i As Integer
    Dim Record As String, SQL As String
    Dim Tbl As String * 1
    Dim TotRiga As Currency, TOTALE As Currency
    Dim ReplSel As Boolean
        
         Modello = CurrentDb.Name
        Modello = Left(Modello, Len(Modello) - Len(Dir(Modello))) & "modello_occasionale.dotx"
    
        On Error Resume Next 'gestione errori step by step
        Set Wrd = GetObject(, "Word.Application")
        If Err.Number = 429 Then
        Set Wrd = CreateObject("Word.Application")
        End If
        
        On Error GoTo 0
        
        Wrd.Visible = True
        Wrd.Activate
        ReplSel = Wrd.Options.ReplaceSelection
        Wrd.Options.ReplaceSelection = True
        
        Set Doc = Wrd.Documents.Add(Modello)
        Doc.Activate
        
        Doc.Bookmarks("Nome").Select
        Wrd.Selection.TypeText Me.Nome
    
        Doc.Bookmarks("Cognome").Select
        Wrd.Selection.TypeText Me.Cognome
        
        'ECC...
    
    SQL = "SELECT * FROM Anagrafica_art_corsi WHERE ID_Anagrafica_docenti =" & Me.ID_Anagrafica_docenti & _
              " ORDER BY ANNO, DATA_CONTRATTO;"
    
    Set Rst = CurrentDb.OpenRecordset(SQL)
    
    With Rst
                TOTALE = 0
                .MoveLast
                While Not .EOF
    
        Doc.Bookmarks("Corso").Select
        Wrd.Selection.TypeText !Corso
          
        Doc.Bookmarks("Materia").Select
        Wrd.Selection.TypeText !MATERIA
     
                    .MoveNext 'passa al prossimo record
                Wend 'Not .EOF
            End With 'Rst
            Rst.Close: Set Rst = Nothing
    
        Wrd.Application.WordBasic.MsgBox "Esportazione terminata", "Esportazione dati da Access"
    End Sub
    Everything works really well for all fields-bookmarks except the last two: "Corso" and "Materia". Both are managed in the source table by two different comboboxes related to two different tables:

    1) Anagrafica corsi, three columns: ID_Anagrafica_corsi; Course abbreviation; Course name full lenght

    Click image for larger version. 

Name:	tab2.jpg 
Views:	24 
Size:	136.9 KB 
ID:	33056

    2) Anagrafica materie, two columns: ID_Anagrafica_materie; Nome_materia

    Click image for larger version. 

Name:	tab3.jpg 
Views:	26 
Size:	136.3 KB 
ID:	33057

    These are the relations between them all:

    Click image for larger version. 

Name:	relaz.jpg 
Views:	25 
Size:	89.6 KB 
ID:	33058

    Given all that, my problem is that when the automation meets the two comboboxes, instead of populating Word with the data I need (full lenght name course, and name of the subject), it uses in one case the course abbreviation and in the other case the ID number.
    It's like the automation didn't manage to get into the comboboxes columns and choose the right one to take the data from to populate the word .dotx
    Useless so far to specify the needed columns like, for example:

    - !Corso.Column(1) or
    - Me.Corso.Column(1) or similar (i tried several solutions alike)

    Probably because of my weak skills in VBA, I can't understand if the problem is in the Wrd.Selection.TypeText code, or in the way I defined the new SQL or... what?

    Italian forums on Access didn't find any solution, except insulting me for my weak skills. Is there anyone around the world who can understand my explanation and give a help?

    Sorry if I described something in the wrong way or with the wrong terms... Thank you in advance.

    Davide

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Your code is (likely) fine, your SQL needs tweaking. You need to join to those 2 tables so you can make the full text field a field in your recordset. Play with a query in design view and then you can copy the relevant SQL changes to your code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hi Davide,

    Your relationships look a bit weird. I would have expected the Anagrafica_Corsi lookup table to be joined to the CORSO field in the Anagrafica_art_corsi and similarly the ID_anagrafica_materie to be joined to the MATERIA field. As Paul said you need to bring the fields in your recordset before you can use them to populate Word with them. Right now you only have the fields from the Anagrafica_art_corsi table with looks like both contain the code and ID not the full names.

    Cheers,
    Vlad

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That does look odd, I hadn't looked at the relationship window.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    By the way, welcome to the site and I got a chuckle out of the thread title.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44
    Quote Originally Posted by pbaldy View Post
    Your code is (likely) fine, your SQL needs tweaking. You need to join to those 2 tables so you can make the full text field a field in your recordset. Play with a query in design view and then you can copy the relevant SQL changes to your code.
    Thanks for advising. I am trying to understand with my weak competences your suggestions.
    You're saying I should join in a query the 2 tables: are you meaning the ones behind form and subform? Or should I add in the query also the two used for lookup?
    Once joined I should refer to the query in the SQL in the code, did i got it right?
    Thanks again.

  7. #7
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44
    Quote Originally Posted by Gicu View Post
    Hi Davide,

    Your relationships look a bit weird. I would have expected the Anagrafica_Corsi lookup table to be joined to the CORSO field in the Anagrafica_art_corsi and similarly the ID_anagrafica_materie to be joined to the MATERIA field. As Paul said you need to bring the fields in your recordset before you can use them to populate Word with them. Right now you only have the fields from the Anagrafica_art_corsi table with looks like both contain the code and ID not the full names.

    Cheers,
    Vlad
    Hi Vlad! Actually at the beginning relations was as you suggested. Then a guy in an Italian forum suggested me to chance them as they are now. I was not so persuaded it was a logic solution, but he sounded wiser than me, so I tried. Actually it didn't solve anything, so now I'll get back establishing the original relations, the way you suggested. Thanks for your advise!

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by davideitaly View Post
    Thanks for advising. I am trying to understand with my weak competences your suggestions.
    You're saying I should join in a query the 2 tables: are you meaning the ones behind form and subform? Or should I add in the query also the two used for lookup?
    Once joined I should refer to the query in the SQL in the code, did i got it right?
    Thanks again.
    No, I mean the table currently in your recordset and the tables that contain the text value you want. Generally:

    SELECT Table1.FieldName, Table2.TextFieldName FROM Table1 INNER JOIN Table2 ON Table1.KeyField = Table2.KeyField WHERE..."

    I don't really understand your tables to be more specific, but perhaps you can post the updated relationships?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    davideitaly is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Mar 2018
    Posts
    44
    Quote Originally Posted by pbaldy View Post
    No, I mean the table currently in your recordset and the tables that contain the text value you want. Generally:

    SELECT Table1.FieldName, Table2.TextFieldName FROM Table1 INNER JOIN Table2 ON Table1.KeyField = Table2.KeyField WHERE..."

    I don't really understand your tables to be more specific, but perhaps you can post the updated relationships?
    Ok, got it. Thank for your advise. Actually I needed to make a query, and I did it including all the involved tables: the two sources of mask and submask, and the other two used for lookup.
    Then I modify the original SQL adding the one resulting from the query:

    Code:
    SQL  = "FROM Anagrafica_materie INNER JOIN (Anagrafica_corsi INNER JOIN  (Anagrafica_docenti INNER JOIN Anagrafica_art_corsi ON  Anagrafica_docenti.[ID_Anagrafica_docenti] =  Anagrafica_art_corsi.ID_Anagrafica_docenti) ON  Anagrafica_corsi.Sigla_corso = Anagrafica_art_corsi.CORSO) ON  Anagrafica_materie.ID_materie = Anagrafica_art_corsi.MATERIA" & _
          "ORDER BY Anagrafica_art_corsi.DATA_CONTRATTO;"
    Anyway that was not enough. I obviously had to change the set recordset line from this:

    Code:
    Set Rst = CurrentDb.OpenRecordset(SQL)
    To that:

    Code:
    Set Recordset = CurrentDb.OpenRecordset("Select * from sqlbookmark")
    where "sqlbookmark" is the name of the new query I did.

    Now it works perfectly.

    Thank you again.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Word Automation
    By comfygringo in forum Import/Export Data
    Replies: 0
    Last Post: 08-27-2013, 08:49 PM
  2. Automation to Word Crash
    By besuchanko in forum Programming
    Replies: 5
    Last Post: 03-02-2013, 08:54 PM
  3. Replies: 2
    Last Post: 04-19-2012, 12:43 PM
  4. Word Automation in Windows 7
    By tmbowden in forum Access
    Replies: 0
    Last Post: 01-23-2012, 01:17 PM
  5. Access and Word Automation
    By djreyrey in forum Forms
    Replies: 1
    Last Post: 01-08-2010, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums