Gregory1979
Technical User
Hi!
I'm in serious need of help, I've searched everywhere and asked on every forum I can find... Please Help!!!!
I have an access database with a few tables and a main form. What happens is: I fill in the main form with the customers details (applications for liquor licenses), when I save it, the data is put into the various tables, works great. There is a subform that can have multiple entries (for directors belonging to the company that is making the application). This also works perfectly.
Then, there is a button on the form which exports all the data related to that specific application into a word document. This also works perfectly.
The problem that I have is that I want to have the data related to the subform outputted to a table in word. I need to write a function that can loop through the subform's table and export only the entries related to that specific license application.
I've put the files on google drive if that will help you understand the problem and see the database in action. It's pretty cool.
Link
Someone suggested I try this code snip in my function, but I can't get it to work:
Here is the full code that I am using now, which works perfectly, but does not do the subform loop...
I'm in serious need of help, I've searched everywhere and asked on every forum I can find... Please Help!!!!
I have an access database with a few tables and a main form. What happens is: I fill in the main form with the customers details (applications for liquor licenses), when I save it, the data is put into the various tables, works great. There is a subform that can have multiple entries (for directors belonging to the company that is making the application). This also works perfectly.
Then, there is a button on the form which exports all the data related to that specific application into a word document. This also works perfectly.
The problem that I have is that I want to have the data related to the subform outputted to a table in word. I need to write a function that can loop through the subform's table and export only the entries related to that specific license application.
I've put the files on google drive if that will help you understand the problem and see the database in action. It's pretty cool.
Link
Someone suggested I try this code snip in my function, but I can't get it to work:
Code:
'Loop data
Set rs = CurrentDb().OpenRecordset("SELECT * FROM TableName WHERE [Criteria]", dbOpenSnapshot)
With rs
If Not .EOF Then
.MoveLast
.MoveFirst
End If
End With
Dim idx As Integer
For idx = 1 To rs.RecordCount
With oDoc.Tables(1)
.Cell(idx, 1).Range.Text = rs![FieldName_1]
.Cell(idx, 2).Range.Text = rs![FieldName_2]
.Cell(idx, 3).Range.Text = rs![FieldName_1]
'add extra rows if required
If rs.AbsolutePosition <> rs.RecordCount - 1 Then .Columns(1).Cells.Add
End With
rs.MoveNext
Next idx
Here is the full code that I am using now, which works perfectly, but does not do the subform loop...
Code:
Public Sub ExportToWord_Click()
On Error GoTo ErrorTrap
Const TemplatePath As String = "C:\forms\templates\Form 3 - Sec 36(1).docx"
'SaveAs
Dim name_ As String
name_ = "C:\forms\generated\" & Me![ACNumber] & "_Form 3 - Sec 36(1).docx"
'Word
Dim oWord As Word.Application
Set oWord = New Word.Application
oWord.Visible = True
Dim oDoc As Word.Document
Set oDoc = oWord.Documents.Add(TemplatePath)
With oDoc
.Bookmarks("wAppTradingNames").Range.Text = Nz(Me![AppTradingName], "")
.Bookmarks("wAppTradingName").Range.Text = Nz(Me![AppTradingName], "")
.Bookmarks("wCompanyName").Range.Text = Nz(Me![CompanyName], "")
.Bookmarks("wCompanyNumber").Range.Text = Nz(Me![CompanyNumber], "")
.Bookmarks("wRAddress1").Range.Text = Nz(Me![RAddress1], "")
.Bookmarks("wPostalCode").Range.Text = Nz(Me![PostalCode], "")
.Bookmarks("wRPostalAddress1").Range.Text = Nz(Me![RPostalAddress1], "")
.Bookmarks("wRPostalCode").Range.Text = Nz(Me![RPostalCode], "")
.Bookmarks("wDomicilium1").Range.Text = Nz(Me![Domicilium1], "")
.Bookmarks("wDomiciliumCode").Range.Text = Nz(Me![DomiciliumCode], "")
.Bookmarks("wDomAfter1").Range.Text = Nz(Me![DomAfter1], "")
.Bookmarks("wDomAfterCode").Range.Text = Nz(Me![DomAfterCode], "")
.Bookmarks("wTelOffice").Range.Text = Nz(Me![TelOffice], "")
.Bookmarks("wTelCell").Range.Text = Nz(Me![TelCell], "")
.Bookmarks("wTelHome").Range.Text = Nz(Me![TelHome], "")
.Bookmarks("wFaxNumber").Range.Text = Nz(Me![FaxNumber], "")
.Bookmarks("wEmail").Range.Text = Nz(Me![Email], "")
.Bookmarks("wFIP").Range.Text = Nz(Me![FIP], "")
.Bookmarks("wAppLicCat").Range.Text = Nz(Me![AppLicCat], "")
.Bookmarks("wLPAddress").Range.Text = Nz(Me![LPAddress], "")
.Bookmarks("wErfNumber").Range.Text = Nz(Me![ErfNumber], "")
.Bookmarks("wLPPostalCode").Range.Text = Nz(Me![LPPostalCode], "")
.Bookmarks("wLPOwnership").Range.Text = Nz(Me![LPOwnership], "")
.Bookmarks("wLPOwnersName").Range.Text = Nz(Me![LpOwnersName], "")
.Bookmarks("wLpOwnerAddress").Range.Text = Nz(Me![LpOwnerAddress], "")
.Bookmarks("wLpRightOccupation").Range.Text = Nz(Me![LpRightOccupation], "")
.Bookmarks("wLPOccDuration").Range.Text = Nz(Me![LPOccDuration], "")
.Bookmarks("wLpPremNotErected").Range.Text = Nz(Me![LpPremNotErected], "")
.Bookmarks("wLpPremAlterReq").Range.Text = Nz(Me![LpPremAlterReq], "")
.Bookmarks("wLpPremAllGood").Range.Text = Nz(Me![LpPremAllGood], "")
.Bookmarks("wLpBuildCommence").Range.Text = Nz(Me![LpBuildCommence], "")
.Bookmarks("wLpBuildDuration").Range.Text = Nz(Me![LpBuildDuration], "")
.Bookmarks("wLpTradingHours").Range.Text = Nz(Me![LpTradingHours], "")
.Bookmarks("wLpRenewal").Range.Text = Nz(Me![LpRenewal], "")
.Bookmarks("wLpJobsa").Range.Text = Nz(Me![LpJobsa], "")
.Bookmarks("wLpJobsB").Range.Text = Nz(Me![LpJobsB], "")
.Bookmarks("wLpJobsC").Range.Text = Nz(Me![LpJobsC], "")
.Bookmarks("wNNPRegName").Range.Text = Nz(Me![NNPRegName], "")
.Bookmarks("wNNPRegNumber").Range.Text = Nz(Me![NNPRegNumber], "")
.Bookmarks("wNNPRegDate").Range.Text = Nz(Me![NNPRegDate], "")
End With
'Save
With oDoc
.SaveAs FileName:=name_, FileFormat:=Word.WdSaveFormat.wdFormatXMLDocument
.Close SaveChanges:=wdDoNotSaveChanges
End With
Leave:
On Error Resume Next
rs.Close
Set rs = Nothing
oWord.Quit
Set oWord = Nothing
On Error GoTo 0
Exit Sub
ErrorTrap:
MsgBox Err.Description, vbCritical, "ExportToWord()"
Resume Leave
End Sub