I now you have been ask’t this a thousand times. But please take in to account I am a complete novice and a bit stupid. I have a simple database using Access 97. I have 1 form 1 report. I just want to know how I can put a command button on the form that will print one report instead off all the reports. It has to be simple I don’t understand code or macro.
First you have to write down the name of the field in the form that is unique or that you want to filter your report by.
It should be something like [BatchNumber] or [CustomerID].
Now you should also have a corresponding field name on your report (they have the same data value). Write it down as well and the name of your report. Make sure to get the spelling right. So with this information written down in front of you, open the form in design view.
Now we are going to add a button, I am assuming that you know how to do this.
Don’t let the wizard do anything you just want a button that dose nothing for the moment.
Right now for the fun part, right click the new button and you should get a list of three options. Select Code Builder.
Now the Visual Basic window will open, don’t freak out, almost there.
Its should say something like …
Private Sub Command47_Click()
End Sub
The Command(what ever number)_Click is your button and we are going to tell it what to do when the user clicks it.
Add these lines of code between the Command_Click line and the End Sub line, just cut and past.
Code:
Dim stDocName As String
On Error GoTo Err_Command47_Click
DoCmd.RunCommand acCmdSaveRecord
stDocName = "YourReport"
DoCmd.OpenReport stDocName, acViewPreview, , "[KeyField] = " & Me.KeyField
Exit_Command47_Click:
Exit Sub
Err_Command47_Click:
MsgBox Err.Description
Resume Exit_Command47_Click
Ahhhh! So much code! What dose it all mean!
Stay cool, almost done.
Now where the line says “YourReport” change this with the name of YOUR REPORT. You must have the name inside the “ ”. Next where is says [KeyField] this has to be changed to the field from your report that you wrote down, again make sure the spelling is exactly the same.
Last, at the end of the line of code you will see Me.KeyField
Change this to Me.UniqueFieldFromYourForm
The Me. Part is code so it looks at the form that you are in, the UniqueFieldFromYourForm is the field you should have written down at the start.
Last, go through and make sure you have changed all the Command(YourNumber)_Click to the correct number for your button.
And that’s it!
Save and Close and your button should work fine.
Your code will look something like this…
Code:
Private Sub Command47_Click()
Dim stDocName As String
On Error GoTo Err_Command47_Click
DoCmd.RunCommand acCmdSaveRecord
stDocName = "YourReport"
DoCmd.OpenReport stDocName, acViewPreview, , "[KeyField] = " & Me.KeyField
Exit_Command47_Click:
Exit Sub
Err_Command47_Click:
MsgBox Err.Description
Resume Exit_Command47_Click
End Sub
Well I hope you can follow this and it’s some help.
Speaking of help, next time you think you have a simple question try the search button on these forums, there is a lot of information here, not all of it for the “high end developers” with a little time you will find what you are looking for.
Good luck.