I don't really know anything about VBA. I need to generate a pdf for each groupid named with the groupid. I found some code and repurposed it. Someone was doing the same thing as I but it is not working. The groupids do not match the groupid and that groupid's associated data in the pdf. So for example groupid 555.pdf file does not contain groupid 555 associated data. Each generated pdf contains the same groupid data while the name of the pdf is created as I desire it to. There are not any error messages.
Form1 contains three fields. The groupid, and two associated data fields. Query3 pulls the data from form1 for whatever row the form is open to. Query3 data goes to a report that displays one row at a time. I run the code in a module.
This is the code for the query3.
Expand|Select|Wrap|Line Numbers
- SELECT [groupidquery].groupid, [groupidquery].Expr1, [groupidquery].Expr2
- FROM [groupidquery]
- WHERE ((([groupidquery].groupid)=[Forms]![Form1]![groupid]));
Expand|Select|Wrap|Line Numbers
- Option Compare Database
- Private Sub FFS()
- Dim db As DAO.Database
- Dim rs As DAO.Recordset
- Dim MyFileName As String
- Dim mypath As String
- Dim temp As String
- mypath = "C:\Users\myfolder\folder\"
- Set db = CurrentDb()
- Set rs = db.OpenRecordset("SELECT DISTINCT [groupid] FROM [groupidquerymain]", dbOpenSnapshot)
- Do While Not rs.EOF
- Forms![Form1].groupid.Value = 0
- temp = rs("groupid")
- MyFileName = rs("groupid") & ".pdf"
- Forms![Form1].groupid.Value = rs!groupid
- DoCmd.OpenForm "Form1"
- DoCmd.GoToRecord , acDataForm, acGoTo, "temp"
- DoCmd.OpenQuery "query3"
- DoCmd.Close acQuery, "query3", acSaveYes
- DoCmd.OpenReport "Report1", acViewPreview
- DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
- DoCmd.Close acReport, "groupidquery", acSaveYes