Command Button to Open AND Filter Different Form

maytime

Registered User.
Local time
Today, 00:28
Joined
Apr 1, 2008
Messages
29
I have tried my hand at the VBA code to fix this, but I admit I am a novice in VBA. Hopefully one of you can help me out. Here is what I want to do:

I have a form that only has command buttons that represent pieces of equipment in a laboratory floor plan layout (i.e. one button represents one piece of equipment in that laboratory). I want to be able to click on a certain equipment's command button and launch a data entry form AND have the data entry form filtered to the exact piece of equipment.

I figured the easiest way would be to add an "ApplyFilter" line of code for each command button in my VBA console, but I am not sure how to tell VBA to apply the filter to the form the command button is supposed to open. Right now I believe VBA is applying the filter to the form with all the command buttons on it, so it isn't helping me out.

Here is the code from one command button (for equipment # IN-1021), I want that particular command button (IN_1021_Datasheet) to open Form "Instruments TRM" and filter that opened form for "IN-1021" in the "Unique Tag" control field:

Code:
Private Sub IN_1021_Datasheet_Click()
On Error GoTo Err_IN_1021_Datasheet_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Instruments TRM"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    DoCmd.ApplyFilter "Forms!Instruments TRM![Unique Tag]" = "IN-1021"
   
Exit_IN_1021_Datasheet_Click:
    Exit Sub

Err_IN_1021_Datasheet_Click:
    MsgBox Err.Description
    Resume Exit_IN_1021_Datasheet_Click
    
End Sub
 
No, you pass the filter to your form:

Code:
    stLinkCriteria = "Forms!Instruments TRM![Unique Tag]" = "IN-1021"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
 
So do I put that suggested line of code you gave me into the command button's code section or do I have to put it in the VBA console for the form that the command button opens?

I was hoping to keep all the filtering code in the form with all the command buttons.
 
You just replace these two lines
Code:
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    DoCmd.ApplyFilter "Forms!Instruments TRM![Unique Tag]" = "IN-1021"

with these two lines:
Code:
    stLinkCriteria = "Forms!Instruments TRM![Unique Tag]" = "IN-1021"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
 
with these two lines:
Code:
    stLinkCriteria = "Forms!Instruments TRM![Unique Tag]" = "IN-1021"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

This code will not work tho... will it??

I would think it would have to be something like:
stLinkCriteria = "Forms!Instruments TRM![Unique Tag] = IN-1021"

You put the code to filter the form in the same button you use to open it... You basicaly are telling it to open with this filter active.
 
Yeah, I screwed up - again. I have been missing little details like that recently. I think I need a long vacation.
 
This code will not work tho... will it??

I would think it would have to be something like:
stLinkCriteria = "Forms!Instruments TRM![Unique Tag] = IN-1021"

You put the code to filter the form in the same button you use to open it... You basicaly are telling it to open with this filter active.

When I paste the line you suggested and click the command button I get a syntax error for a "missing operator". Do I need quotation marks around IN-1021 in the code statement?

It says the missing operator is in this statement:

Forms!Instruments TRM![Unique Tag] = IN-1021
 
Last edited:
I tried adding some "[" and "]" brackets around the "Forms" and "Instruments TRM" parts, but I still get the same message. It seems that it almost is working but some little operator is either wrong or not present. Any ideas? Thanks.

EDIT: I put in apostrophe marks around IN-1021 in the stLinkCriteria statement and it finally worked how I wanted it to. Thank you both for helping me.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom