Log in

View Full Version : Help with Code



steveo0707
06-10-2024, 06:14 AM
Hello,

I am using Access 365, and we use our database to track nonconforming parts.

I have a Form that I have the inspectors save and print. Currently I have the default printer set to Print as PDF so they can save to our network location. Once they save, they click CTRL P to select the printer they want to print to.

My inspectors are older and not very computer savvy. I want them to be able to click the save button to save to file location, then hit a Print button to print to the proper Printer. We only use one Printer to print in our lab.

The following code is what I have for the save button currently.



Private Sub Save_Click()On Error GoTo Err_Print_Record_Click
DoCmd.RunCommand acCmdSelectRecord
DoCmd.PrintOut acSelection
Exit_Print_Record_Click:
Exit Sub
Err_Print_Record_Click:
MsgBox Err.Description
Resume Exit_Print_Record_Click
End Sub


How would I modify this code to select the Quality Printer to print, I would use a Print Button for this. or what code do I need to save to a drive location.

I know it is easier to do all this in a Report, however, when doing this through a report it does not print or save correctly, I have tried several different ways to fix, and the Form is the only thing that works. I did not set up the DB and really don't have time to rebuild a new one

June7
06-10-2024, 09:55 PM
What does "not print or save correctly" mean. What is not correct?

Instead of depending on default printer setting to save as PDF, use DoCmd.OutputTo method.

Leave the printer as default and DoCmd.PrintOut form or report.

Both commands can be behind one button.

steveo0707
06-11-2024, 05:16 AM
Everything is printing and saving correctly. I just wanted to eliminate them having to hit CTRL P to print. They sometimes have to print and save multiple records and they don't understand they have to reset the default. I will try the DoCmd.OutoutTo method.

Thank you for your time.

steveo0707
06-11-2024, 06:51 AM
I am trying to get this code to work and can't figure it out. I want it to print to pdf to this file location.



Private Sub SaveBtn_Click()

DoCmd.OutputTo acCmdSelectRecord, acFormatPDF, "O:\NCMR\InProcess\"

End Sub

June7
06-11-2024, 07:22 AM
Need to include filename in the destination path. What do you want each report to be named? If you don't assign a unique name to each report, it will overwrite existing. Does your current code overwrite? If it does, what is purpose of saving to PDF?

Where do you get acCmdSelectRecord from? AFAIK, that is not an option with OutputTo.

Apply filter to form or report if you don't want all records to output.

Review https://learn.microsoft.com/en-us/office/vba/api/access.docmd.outputto

You said does not work correctly when you use a report - why? Really should use report.

Gasman
06-11-2024, 10:27 AM
Create a report.
Open report with WHERE criteria for the record(s) required.
Output to pdf is required.



Private Sub cmdShip_Click()
On Error GoTo Err_cmdShip_Click
Dim stRptName As String, stParam As String, stLinkCriteria As String, stDBpath As String, stFTPpath As String, _
Dim iPreview As Integer, iDialog As Integer, blnPrintIt As Boolean
stDBpath = CurrentProject.Path & "\"
stFTPpath = stDBpath & "Gazette\"
iPreview = acViewPreview
If Me.ChkPreview Then
' iPreview = 2
iDialog = acWindowNormal
Else
iDialog = acHidden
End If
stRptName = "Main_by_Ship"
stParam = Replace(LCase(Me.cboShip.Value), " ", "_")
stLinkCriteria = "[Ship] = '" & Me.cboShip.Value & "'"
'DoCmd.CopyObject , stParam, acReport, stRptName
If Me.ChkPreview Then
DoCmd.OpenReport stRptName, iPreview, , stLinkCriteria, iDialog
Else
DoCmd.OpenReport stRptName, iPreview, , stLinkCriteria, iDialog
DoCmd.OutputTo acOutputReport, stRptName, acFormatPDF, stFTPpath & stParam & ".pdf", False
DoCmd.Close acReport, stRptName
End If
Exit_cmdShip_Click:
Exit Sub
Err_cmdShip_Click:
MsgBox Err.Description
Resume Exit_cmdShip_Click
End Sub