Access 2010
Windows
Goal: Output of various letter types (each containing multiple people) as individual .pdf files, to a variable network folder and subfolder location
Based on our prior posts and working on this project, I now have the following code in place:
Code:
Private Sub ctlP_LetterA_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = CurrentProject.Connection
Set rs = New ADODB.Recordset
Dim NTWK_FOLDER As String
Dim NTWK_SUBFOLDER As String
Dim LAST_NAME As String
Dim FIRST_NAME As String
Dim DISPLAY_NAME As String
Dim YR As String
Dim strFolder As String
'This line indicates which data values you will be using from your record source.
rs.Open "SELECT EmpID, LAST_NAME, FIRST_NAME, DISPLAY_NAME, NTWK_FOLDER, NTWK_SUBFOLDER, YR FROM qry_Letter_A;", cn, adOpenStatic, adLockPessimistic
While Not rs.EOF
'The next line determines the path for the output of the selected documents.
strFolder = "z:\2012_testing\" & rs!NTWK_FOLDER & "\" & rs!NTWK_SUBFOLDER & "\"
DoCmd.OpenReport "rpt_Letter_A_SupportStaff", acViewPreview, , "EmpID='" & rs!EMPID & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, strFolder & rs!LAST_NAME & "," & rs!FIRST_NAME & " - " & rs!YR & " Letter" & ".pdf", False
DoCmd.Close acReport, "rpt_Letter_A", acSaveNo
rs.MoveNext
Wend
MsgBox "Export of Letter A is complete.", vbOKOnly, "Export Complete"
End Sub
_________________________________________________________________
Private Sub ctlP_LetterB_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = CurrentProject.Connection
Set rs = New ADODB.Recordset
Dim NTWK_FOLDER As String
Dim NTWK_SUBFOLDER As String
Dim LAST_NAME As String
Dim FIRST_NAME As String
Dim DISPLAY_NAME As String
Dim YR As String
Dim strFolder As String
'This line indicates which data values you will be using from your record source.
rs.Open "SELECT EmpID, LAST_NAME, FIRST_NAME, DISPLAY_NAME, NTWK_FOLDER, NTWK_SUBFOLDER, YR FROM qry_Letter_B;", cn, adOpenStatic, adLockPessimistic
While Not rs.EOF
'The next line determines the path for the output of the selected documents.
strFolder = "z:\2012_testing\" & rs!NTWK_FOLDER & "\" & rs!NTWK_SUBFOLDER & "\"
DoCmd.OpenReport "rpt_Letter_B", acViewPreview, , "EmpID='" & rs!EMPID & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, strFolder & rs!LAST_NAME & "," & rs!FIRST_NAME & " - " & rs!YR & " Letter" & ".pdf", False
DoCmd.Close acReport, "rpt_Letter_B", acSaveNo
rs.MoveNext
Wend
MsgBox "Export of Letter B is complete.", vbOKOnly, "Export Complete"
End Sub
As noted above, I repeat the code from Letter A for each of the letter types (I have included B above). I do this for Letter Types B-R. (We have to do them separately instead of all at one time.) The Letter A group works fantastic. When I get to the other letter types, I am receiving errors. The errors are:Letter B:
Run-time error '-2147217900 (80040e14)':
Syntax error in FROM clause.
Letter C:
Run-time error '-2147467259 (80004005)':
Method 'Open' of object '_Recordset' failed
Can someone assist with helping me understand where I am making my mistake(s)? Do I need to repeat all of the Dim and Set statements each time? This is all still relatively new and I appreciate both your patience if I failed to state or post anything correctly as well as your expertise in resolving.
Thank you.
sren