Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vba for a print button on a form

Status
Not open for further replies.

lr999

Technical User
May 16, 2011
30
US
Hello
How to code the print button on a form, for a single sript record, currently it's printing all records, i need to print only one. Thanks
 
Well.... that would be easier to answer if we knew more about what you are doing. For instance: Are you printing a report or a form? What is the RecordSource and what field do you want to use to identify which record to print?

In the broadest terms, what you want to do is limit the RecordSource by using the command line that launches the report, or by using code to change the Filter on the Form, prior to printing.

Give us some more specific information and we can give you more specific instructions.
 
It's from a form, I need sample code to change the filter on the form, so it will print only one record from the form. thanks
 

In the code for the cmdPrint_Click event (or whatever the event is that launches the print function) before the print statement put:
Code:
Me.Filter = "whatever your criteria is for restricting the records"
Me.FilterOn = True
For example, if your Record has a field called ID you could use:
Code:
Me.Filter = "ID = " & ID
Me.FilterOn = True
You have to keep the quotes exactly like they are.

If you are using a non-numeric value (e.g. Name) You would use:
Code:
Me.Filter = "Name = '" & Name & "'"
Me.FilterOn = True
Note the use of the double quotes and single quotes... this is because the value is a string and not a number.

If your criteria is entered in a testbox or combo box, use something like:
Code:
Me.Filter = "Name = '" & txtMyTextBox & "'"

Technically you do not the the 'Me' in any of those statements, but using it makes things clearer and also causes the 'autocomplete' function to work, saving keystrokes.

A keystroke saved is an error avoided.
 
If your criteria is entered in a testbox or combo box, use something like

Of course, that should be textbox.

Perfect example of a 'keystroke = error' equation.
 

I would suggest to stay away from naming fields/variables with reserved words, like Name (use FName or LName or Empl_Name), Date (use EndDate or StartDate, or My_Date), Time (use Start_Time or Old_Time), etc.

Other than that - good examples Gammachaser :)

Just my $0.02

Have fun.

---- Andy
 
Those were for illustrative purposes only.

Naturally, you never use keywords in naming variables.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top