Automation Access-Word in presence of comboboxes

davideItaly

New member
Local time
Today, 22:07
Joined
Mar 14, 2018
Messages
3
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). I would post an image but I can't as I am new here, but I hope explanation is clear.

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

2) Anagrafica materie, two columns: ID_Anagrafica_materie; Nome_materia


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.

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?

Thank you in advance.

Davide
 

Users who are viewing this thread

Back
Top Bottom