Search Button and Save Button

steveo0707

New member
Local time
Today, 18:31
Joined
Aug 2, 2024
Messages
1
Hello,

I am using Microsoft 365.

I have a db that tracks nonconformance parts. I am very new to VBA and don't quite understand how all the DoCmd.'s work for Access.

I am trying to accomplish two things:

I have a search button next to NCMR Number. There was a code in there for the operator to click the button and it would go to a specific record so we could update as needed. somewhere over the past couple of weeks it got deleted.

So, I need code to allow this to happen.

The code listed below was previously there from another user. It is the current code to print our NCMR Form. The default Printer is set to Print to PDF so we can save. then the operator clicks control P to print. However, this changes the default printer, and a couple of my operators are not so computer savvy and can't figure out to change the default printer back to print to PDF. I want to create a save button that will save it to a specific drive location. I know this would all be easier if it was a report, however this was already in place, and I plan on transforming this into a report when time allows.

I am not sure how to modify the code below so I can save the document.

Using the export to PDF will not work, since it tries to save all 6500 records that are in the form. Is there a way to filter this out? If so, how is this accomplished?


Code:
Private Sub PrintButton_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
 
Welcome aboard. Lots to unpack here. First thing of course is to please do not post the same question in multiple forums. At least give people a chance to answer your question before going elsewhere and then tell people and include a link. Don't make folks waste their time trying to help you when someone else may have already solved the problem.

Here is some air code to get you going. Add an option group on the form so that the user can specify where he wants the output and make a report. Just let the wizard build it for you.
1722631020899.png


Then in your "run" button
1. fill the three variables. Case 3 is the only one that uses the file name so it is only built there. The air code references a form control to get the path. I don't like to hard code paths but if it is easier for you, then replace the "Me.txtPath" with "C:\somedirectory". You will have to substitute your own field name for the criteria. This is what will filter the report so it only prints the report for the item the user selected.
2. When I export files, I always suffix them with a date.

Give it a try and let us know how it works. If you have questions about the code, please post YOUR version of the code and keep in mind that what I posted is "air" code so it is probably correct logically but may have syntax errors.

Code:
    Dim strFileName As String
    Dim strReport As String
    Dim strCriteria As String

    strReport = "your report name"
    strCriteria = "YourIDFieldName = " & Me.YourIDFieldName

        Select Case Me.fraOutputTo
            Case 2      'Print
                DoCmd.OpenReport strReport, acViewNormal, , strCriteria
            Case 3      'Export to PDF
                strFileName = Me.txtPath & "\" & strReport & "_" & Format(Date, "yyyymmdd") & ".pdf"
                DoCmd.OpenReport strReport, acViewPreview, , strCriteria
                DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, strFileName
            Case Else      'Preview
                DoCmd.OpenReport strReport, acViewPreview, , strCriteria
        End Select

The OutputTo method does not have a WHERE clause the way the OpenReport method does so to filter the .pdf, in this case, you can open the report in preview first. Then output it. There are better solutions if you have to output a bunch of .pdf's at once but one at a time, this method is OK.
 
Last edited:
Hi. Welcome to AWF!
I know this would all be easier if it was a report, however this was already in place, and I plan on transforming this into a report when time allows.

It sounds like you are trying to print a Form, not a Report. Is that correct?
 

Users who are viewing this thread

Back
Top Bottom