Here is a simple example to query an Sql Database and pass the result to a property. You need to set a reference to Microsoft ActiveX DataObjects 2.8 Library.
CODE:
Option Explicit
Private MyRecordSet As ADODB.Recordset
Private MyConnection As New ADODB.Connection
Sub Test()
Dim RequeteSql As String
Dim ReponseSql As Variant
' Query.
RequeteSql = "SELECT Emplacement FROM RepertoireTravail WHERE Nom = 'EmplacementSolin' "
' Result from the query.
ReponseSql = GetSqlData(RequeteSql, "VentesProduction")
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
Dim oPropSets As PropertySets
Set oPropSets = oDoc.PropertySets
Dim oPropSet As PropertySet
Set oPropSet = oPropSets.Item("Design Tracking Properties")
Dim oProp As Property
Set oProp = oPropSet.Item("Project")
' Set the property value.
oProp.Value = ReponseSql(0, 0)
oDoc.Save
End Sub
Public Function GetSqlData(ByRef Requete, ByRef sCatalog) As Variant
Set MyRecordSet = New ADODB.Recordset
MyConnection.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog= " + sCatalog + ";Data Source=ASTON-SVR1"
MyConnection.ConnectionTimeout = 500
MyConnection.Open
Dim sQueryType As String
sQueryType = UCase(Mid(Requete, 1, 6))
If sQueryType = "DELETE" Or sQueryType = "INSERT" Or sQueryType = "UPDATE" Then
MyConnection.Execute Requete
If Err = 0 Then
GetSqlData = True
Else
GetSqlData = False
End If
ElseIf sQueryType = "SELECT" Then
On Error Resume Next
MyRecordSet.Open Requete, MyConnection
Dim ListeRecord As Variant
ListeRecord = MyRecordSet.GetRows()
If VarType(ListeRecord) = vbEmpty Then
GetSqlData = Empty
Else
GetSqlData = ListeRecord
End If
Err.Clear
End If
End Function
Ludesroc