Dim db As DAO.Database
Dim rsFields As DAO.Recordset
Dim qdData As DAO.QueryDef
Set db = CurrentDb()
Set qdFields = db.QueryDefs!qFetchFieldNames
qdFields.Parameters![EnterDataSourceName] = "tblMembers"
qdFields.Parameters![EnterDocID] = DocumentID
Set rsFields = qdFields.OpenRecordset(dbOpenDynaset, dbSeeChanges)
''''' You may need a loop here if your query selects more than one record for printing.
GoSub FillFields
ExitSub:
Exit Sub
FillFields:
Do While rsFields.EOF = False
Select Case rsFields!FieldType
Case "Memo"
If IsNull(rsData.Fields(rsFields!FieldDescription)) Then
WordDoc.FormFields(rsFields!BookMarkName).Result = ""
Else
WordDoc.Bookmarks(rsFields!BookMarkName).Range.Fields(1).Result.Text = rsData.Fields(rsFields!FieldDescription)
End If
Case "Ckbox"
If IsNull(rsData.Fields(rsFields!FieldDescription)) Then
Else
WordDoc.FormFields(rsFields!BookMarkName).CheckBox.Value = rsData.Fields(rsFields!FieldDescription)
End If
Case "Date"
If IsDate(rsData.Fields(rsFields!FieldDescription)) Then
WordDoc.FormFields(rsFields!BookMarkName).Result = Format(rsData.Fields(rsFields!FieldDescription), "mmmm dd, yyyy")
Else
WordDoc.FormFields(rsFields!BookMarkName).Result = ""
End If
Case "Phone"
If IsNumeric(rsData.Fields(rsFields!FieldDescription)) Then
WordDoc.FormFields(rsFields!BookMarkName).Result = Format(rsData.Fields(rsFields!FieldDescription), "\(###\) 000\-0000")
Else
WordDoc.FormFields(rsFields!BookMarkName).Result = ""
End If
Case Else
If IsNull(rsData.Fields(rsFields!FieldDescription)) Then
WordDoc.FormFields(rsFields!BookMarkName).Result = ""
Else
WordDoc.FormFields(rsFields!BookMarkName).Result = rsData.Fields(rsFields!FieldDescription)
End If
End Select
rsFields.MoveNext
Loop
Return
I will have to puzzle on this for a while. A lot for me to chew on. From reading the code it seems I have to deletes some case and add for name, address, etc.Forget the first part where I described the complex solution that allows users to define new fields. You don't need that. Read the second part where I described the simple solution.
Depending on whether you are printing one document at a time or multiple copies of the same document, you would open a recordset created from a query that either selects ONE record or many records. Then you would loop through the recordset. For each record, you would use GoSub to fill the field. The following code may be more complicated than you need. In my behind the scenes field definition table (which is used as the RowSource for the combo in the form I pasted above), it has a type code that says what type of data you have. I used a combination of bookmarks, formfields, and checkboxes and each is filled differently. The formfield and checkbox controls are the "old" style so I'm not sure the code works with the "new" style. I never checked it. You can try it and let me know. Anyway. It loops through the fields collection of the recordset so make sure you only include fields you want to use.
I think you can format dates and phone numbers in the query and not need to do it in the case below so you can get rid of most of the case.
I extracted this code from a very complicated procedure. I think this is all you need.
Code:Dim db As DAO.Database Dim rsFields As DAO.Recordset Dim qdData As DAO.QueryDef Set db = CurrentDb() Set qdFields = db.QueryDefs!qFetchFieldNames qdFields.Parameters![EnterDataSourceName] = "tblMembers" qdFields.Parameters![EnterDocID] = DocumentID Set rsFields = qdFields.OpenRecordset(dbOpenDynaset, dbSeeChanges) ''''' You may need a loop here if your query selects more than one record for printing. GoSub FillFields ExitSub: Exit Sub FillFields: Do While rsFields.EOF = False Select Case rsFields!FieldType Case "Memo" If IsNull(rsData.Fields(rsFields!FieldDescription)) Then WordDoc.FormFields(rsFields!BookMarkName).Result = "" Else WordDoc.Bookmarks(rsFields!BookMarkName).Range.Fields(1).Result.Text = rsData.Fields(rsFields!FieldDescription) End If Case "Ckbox" If IsNull(rsData.Fields(rsFields!FieldDescription)) Then Else WordDoc.FormFields(rsFields!BookMarkName).CheckBox.Value = rsData.Fields(rsFields!FieldDescription) End If Case "Date" If IsDate(rsData.Fields(rsFields!FieldDescription)) Then WordDoc.FormFields(rsFields!BookMarkName).Result = Format(rsData.Fields(rsFields!FieldDescription), "mmmm dd, yyyy") Else WordDoc.FormFields(rsFields!BookMarkName).Result = "" End If Case "Phone" If IsNumeric(rsData.Fields(rsFields!FieldDescription)) Then WordDoc.FormFields(rsFields!BookMarkName).Result = Format(rsData.Fields(rsFields!FieldDescription), "\(###\) 000\-0000") Else WordDoc.FormFields(rsFields!BookMarkName).Result = "" End If Case Else If IsNull(rsData.Fields(rsFields!FieldDescription)) Then WordDoc.FormFields(rsFields!BookMarkName).Result = "" Else WordDoc.FormFields(rsFields!BookMarkName).Result = rsData.Fields(rsFields!FieldDescription) End If End Select rsFields.MoveNext Loop Return
Working on code you sent, and will ask one question at a time, trying not to waste your time. Stepping through code slowly.Forget the first part where I described the complex solution that allows users to define new fields. You don't need that. Read the second part where I described the simple solution.
Depending on whether you are printing one document at a time or multiple copies of the same document, you would open a recordset created from a query that either selects ONE record or many records. Then you would loop through the recordset. For each record, you would use GoSub to fill the field. The following code may be more complicated than you need. In my behind the scenes field definition table (which is used as the RowSource for the combo in the form I pasted above), it has a type code that says what type of data you have. I used a combination of bookmarks, formfields, and checkboxes and each is filled differently. The formfield and checkbox controls are the "old" style so I'm not sure the code works with the "new" style. I never checked it. You can try it and let me know. Anyway. It loops through the fields collection of the recordset so make sure you only include fields you want to use.
I think you can format dates and phone numbers in the query and not need to do it in the case below so you can get rid of most of the case.
I extracted this code from a very complicated procedure. I think this is all you need.
Code:Dim db As DAO.Database Dim rsFields As DAO.Recordset Dim qdData As DAO.QueryDef Set db = CurrentDb() Set qdFields = db.QueryDefs!qFetchFieldNames qdFields.Parameters![EnterDataSourceName] = "tblMembers" qdFields.Parameters![EnterDocID] = DocumentID Set rsFields = qdFields.OpenRecordset(dbOpenDynaset, dbSeeChanges) ''''' You may need a loop here if your query selects more than one record for printing. GoSub FillFields ExitSub: Exit Sub FillFields: Do While rsFields.EOF = False Select Case rsFields!FieldType Case "Memo" If IsNull(rsData.Fields(rsFields!FieldDescription)) Then WordDoc.FormFields(rsFields!BookMarkName).Result = "" Else WordDoc.Bookmarks(rsFields!BookMarkName).Range.Fields(1).Result.Text = rsData.Fields(rsFields!FieldDescription) End If Case "Ckbox" If IsNull(rsData.Fields(rsFields!FieldDescription)) Then Else WordDoc.FormFields(rsFields!BookMarkName).CheckBox.Value = rsData.Fields(rsFields!FieldDescription) End If Case "Date" If IsDate(rsData.Fields(rsFields!FieldDescription)) Then WordDoc.FormFields(rsFields!BookMarkName).Result = Format(rsData.Fields(rsFields!FieldDescription), "mmmm dd, yyyy") Else WordDoc.FormFields(rsFields!BookMarkName).Result = "" End If Case "Phone" If IsNumeric(rsData.Fields(rsFields!FieldDescription)) Then WordDoc.FormFields(rsFields!BookMarkName).Result = Format(rsData.Fields(rsFields!FieldDescription), "\(###\) 000\-0000") Else WordDoc.FormFields(rsFields!BookMarkName).Result = "" End If Case Else If IsNull(rsData.Fields(rsFields!FieldDescription)) Then WordDoc.FormFields(rsFields!BookMarkName).Result = "" Else WordDoc.FormFields(rsFields!BookMarkName).Result = rsData.Fields(rsFields!FieldDescription) End If End Select rsFields.MoveNext Loop Return
Set db = CurrentDb()
Set qdFields = db.QueryDefs!qFetchFieldNames
qdFields.Parameters![QryGetDLData] = "tblMembers"
qdFields.Parameters![Templete1.docx] = DocumentID
Set rsFields = qdFields.OpenRecordset(dbOpenDynaset, dbSeeChanges)
I am trying to figure out how to create a query that only returns the column names. I think that is what I need. I understand all the code except the setup.do you have a query named qFetchFieldNames?
Set db = CurrentDb()
Set qdFields = db.QueryDefs!qFetchFieldNames
qdFields.Parameters![Suspects] = "tblMembers"
qdFields.Parameters![TemplateTest] = DocumentID
Set rsFields = qdFields.OpenRecordset(dbOpenDynaset, dbSeeChanges)
''''' You may need a loop here if your query selects more than one record for printing.
GoSub FillFields
Pat, let me play with this on my own for a while. I've taken too much of your time, and I really appreciate it. I have learned a lot, or, at least something. Let me see if I can get something going on my own, but this last post cleared up things a lot.One step at a time. Make the procedure work for a query that selects a single record. Once that works, add the loop. This would have gone a lot easier if you had just used the sample and hardcoded the fields to make your document work since you are a long way from understanding the process. I do not have a working sample I can post so you are getting this piecemeal and essentially I am working privately for you to make this work which was not my intention. I was trying to show you how OLE automation works and tell you how to proceed from there if you had more than the most basic of requirements.
The query here is the one that pulls the list of bookmarks for a specific document. The two tables in the query are the list of all the defined fields and the bookmarks mapped to a specific document. The Parameters provide the criteria. [EnterDocID] is the parameter that specifies which DocID we want from the bookmarks table and [EnterDataSource] is the parameter that specifies which fields we want from the fields table. The two tables are joined on FieldID. this is a partially complete sample so it isn't quite how the next sample database will work but for now it will be OK. The "tblMembers" is hardcoded in the sample code. Replace this with the name of the table or query that will pull the data you want. I think it actually isn't used. I think the recordsource is also created from a hardcoded query. You should see that later in the FillFields GoSub.
You can take a short cut for now and add the FieldName, FieldDescription, and FieldType to tblBookmarks. This will duplicate the data but it simplifies the process until I can build the whole thing in a coherent sample. There is a case statement in some code I posted that shows what the FieldType is used for (formatting the field for Excel). You may even be able to avoid that if you format the field in your query. The problem arises with fields like phone numbers. They are ( or should be) stored as 10 digit strings in the table. but you want them displayed as (xxx) xxx-xxxxx and you do that on forms/reports with a mask. That doesn't apply here so you actually need to format the field to export it to Word or Excel.
As you follow the code in the FillFields you'll see that this is the query that controls the filling because it has the field names.
View attachment 104808
Let me try and work things out, based on this last post. I've taken too much of your time already. Thanks again for all your help, I really appreciate it.One step at a time. Make the procedure work for a query that selects a single record. Once that works, add the loop. This would have gone a lot easier if you had just used the sample and hardcoded the fields to make your document work since you are a long way from understanding the process. I do not have a working sample I can post so you are getting this piecemeal and essentially I am working privately for you to make this work which was not my intention. I was trying to show you how OLE automation works and tell you how to proceed from there if you had more than the most basic of requirements.
The query here is the one that pulls the list of bookmarks for a specific document. The two tables in the query are the list of all the defined fields and the bookmarks mapped to a specific document. The Parameters provide the criteria. [EnterDocID] is the parameter that specifies which DocID we want from the bookmarks table and [EnterDataSource] is the parameter that specifies which fields we want from the fields table. The two tables are joined on FieldID. this is a partially complete sample so it isn't quite how the next sample database will work but for now it will be OK. The "tblMembers" is hardcoded in the sample code. Replace this with the name of the table or query that will pull the data you want. I think it actually isn't used. I think the recordsource is also created from a hardcoded query. You should see that later in the FillFields GoSub.
You can take a short cut for now and add the FieldName, FieldDescription, and FieldType to tblBookmarks. This will duplicate the data but it simplifies the process until I can build the whole thing in a coherent sample. There is a case statement in some code I posted that shows what the FieldType is used for (formatting the field for Excel). You may even be able to avoid that if you format the field in your query. The problem arises with fields like phone numbers. They are ( or should be) stored as 10 digit strings in the table. but you want them displayed as (xxx) xxx-xxxxx and you do that on forms/reports with a mask. That doesn't apply here so you actually need to format the field to export it to Word or Excel.
As you follow the code in the FillFields you'll see that this is the query that controls the filling because it has the field names.
View attachment 104808
Got it. Thanks.The rsData references are to the query that pulls the data. The rsData query controls the outer loop which at the moment, isn't a loop. It just reads one record.