VBA to only show records that relate to an ID (1 Viewer)

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
Hi,

I have a check box which on click I want to filter a drop down called METER to only show the records that relate to the field called METERID.

So say my METERID is 1, I want my dropdown METER to only show the records that have a METERID of 1 as well.

Anyone?
Thanks
 

Beetle

Duly Registered Boozer
Local time
Today, 09:15
Joined
Apr 30, 2011
Messages
1,808
So the Meter combo box (drop down) initially shows all Meter records, but when a check box is checked true you want the combo box to only display Meter records related to the MeterID of the current record on the main form?

You can reset the Row Source of the combo box in the After Update event of the check box. Example (air code);

Code:
Private Sub chkYourCheckBox_AfterUpdate()

If chkYourCheckBox Then
    Me.cboYourComboBox.RowSource = "Select Meter From tblMeters " _
                                  & "Where MeterID = " & Me.MeterID
Else
   Me.cboYourComboBox.RowSource = "Select Meter From tblMeters"
End If

End Sub

You may also want to repeat this code in the Current event of your Form.
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
Yes.

I have 1 form, with a Contract tab. With ContractID as PK.

Then I open a METER form and when I begin entering details the link between the Contract and Meter is made. So a FK ContractID is stored in the Meter table.

On the Meter form, I select from a list of Meter Refs, currently just show all meter refs.

What I need it to show is only Meter Refs that relate to that ContractID.

Does that make sense?
 

Beetle

Duly Registered Boozer
Local time
Today, 09:15
Joined
Apr 30, 2011
Messages
1,808
Did you try implementing my suggestion?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:15
Joined
Feb 19, 2002
Messages
43,376
YNWA, this question has been answered hundreds of times and there are at least two valid solutions. Search for cascading combos.
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
YNWA, this question has been answered hundreds of times and there are at least two valid solutions. Search for cascading combos.

But I only have 1 combo?

When I check the box, the join is made and the Price table stores the ContractID from the Contract table.

So when checked, I need the Combo to display the lists that match that ContractID.
 

Beetle

Duly Registered Boozer
Local time
Today, 09:15
Joined
Apr 30, 2011
Messages
1,808
Yes, but I dont get the list of Meter Refs that match that Contract. I dont get any.

Can you post an example of the code you attempted?

Then I open a METER form and when I begin entering details the link between the Contract and Meter is made. So a FK ContractID is stored in the Meter table.

Are we talking about a form with a sub form here? Two separate forms?

When I check the box, the join is made and the Price table stores the ContractID from the Contract table.

What do you mean by the join is made when you check the box? What join?

Also, in your first post you were talking about a Meter table, now you're talking about a Price table. Did something change? It's hard to give you advice if we can't follow what you're doing.
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
Can you post an example of the code you attempted?



Are we talking about a form with a sub form here? Two separate forms?



What do you mean by the join is made when you check the box? What join?

Also, in your first post you were talking about a Meter table, now you're talking about a Price table. Did something change? It's hard to give you advice if we can't follow what you're doing.

I have a form CLIENTS with 3 subforms. One of which is called CONTRACT on the contract tab (subform) I have a button to open up a new form to enter PRICES that relate to that CONTRACT.

The code for the button to open the PRICES form is:
Code:
Private Sub cmd_OpenCosts_Click()
On Error GoTo Err_cmd_OpenCosts_Click
    
    DoCmd.OpenForm "frm_CostsPrices", , , "[contractID]=" & Me.Contract_ID, , , Me.Contract_ID

Exit_cmd_OpenCosts_Click:
    Exit Sub

Err_cmd_OpenCosts_Click:
    MsgBox Err.Description
    Resume Exit_cmd_OpenCosts_Click
    
    
End Sub

Then on the PRICES form I have this code:
Code:
Private Sub Form_BeforeInsert(Cancel As Integer)
On Error GoTo Err_Handler

    If Me.OpenArgs & "" <> "" Then
        ContractID = Me.OpenArgs
        DoCmd.RunCommand acCmdSave
    End If

Exit_Err_Handler:
    Exit Sub

Err_Handler:
    MsgBox Err.Description
    Resume Exit_Err_Handler

End Sub

When I enter data into the first field. The codes generates the ID from the Contract form and stores it in the PRICES form (table also). So each PRICE now links to a CONTRACT.

What I need to do is filter my dropdown to only show METER refs (each METER Ref relates to a contractID and are stored in a table called METERREFS).

But I cant filter it, as I have no Contract ID to say "hey, pull all the meter refs that match this ContractID".

So my idea was to have a field I enter into first (eg. a checkbox), then when you check the box, that ID is passed from CONTRACT to PRICES form, so the Combo can say "hey, bring me all meter refs that match that ContractID".

In the Meter table I have MeterID (PK), ContractID (FK) MeterRef

In the Contract table I have ContractID (PK), ClientID (FK), Contract Name etc..

In the Prices table I have PriceID (PK), ContractID (FK), Price etc..
 

Beetle

Duly Registered Boozer
Local time
Today, 09:15
Joined
Apr 30, 2011
Messages
1,808
I'm not sure, but I think from the above that the Meter combo box is on your Prices form. You're already passing the Contract_ID to the Prices form via OpenArgs, so just use that value to set the Row Source of your combo box in the Open event of the Prices form;

Code:
Private Sub Form_Open(Cancel As Integer)

    If Nz(Me.OpenArgs, vbNullString) <> vbNullString Then
        Me.cboMeters.RowSource = "Select MeterID, ContractID, MeterRef" _
                               & " From tblMeters Where ContractID=" & Me.OpenArgs _
                               & " Order By MeterRef;"
    End If

End Sub

You'll need to adjust the above example to match your actual object names, and depending on the data type of ContractID, you may need to add delimiters.
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
Cheers I will give that a go now.
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
I'm not sure, but I think from the above that the Meter combo box is on your Prices form. You're already passing the Contract_ID to the Prices form via OpenArgs, so just use that value to set the Row Source of your combo box in the Open event of the Prices form;

Code:
Private Sub Form_Open(Cancel As Integer)

    If Nz(Me.OpenArgs, vbNullString) <> vbNullString Then
        Me.cboMeters.RowSource = "Select MeterID, ContractID, MeterRef" _
                               & " From tblMeters Where ContractID=" & Me.OpenArgs _
                               & " Order By MeterRef;"
    End If

End Sub

You'll need to adjust the above example to match your actual object names, and depending on the data type of ContractID, you may need to add delimiters.

My ContractID is a number field. What do you mean delimiters?

Tried that code and edited it to suit my table that stores MeterRefs. But no joy.

When using the form, it appears the ContractID is not passed to the Prices form/table until you enter the first bit of data, so would this be why if using Open Form?

I have the ContractID field on my Prices form, and it doesn't populate until I enter some data.
 

Beetle

Duly Registered Boozer
Local time
Today, 09:15
Joined
Apr 30, 2011
Messages
1,808
My ContractID is a number field. What do you mean delimiters?

If your ContractID field is a number, then you shouldn't need the delimiters. Just to clarify what I mean, if the ContractID was text, then this line;

Code:
& " From tblMeters Where ContractID=" & Me.OpenArgs

would need string delimiters added like the following;

Code:
& " From tblMeters Where ContractID=""" & Me.OpenArgs & """"

similarly, if you were passing a date value you would need date delimiters (#).


Tried that code and edited it to suit my table that stores MeterRefs. But no joy.

What do you mean by "no joy"? You get no records? You get all records? You get an error? We need specifics. What does the code look like now and where did you put it?

When using the form, it appears the ContractID is not passed to the Prices form/table until you enter the first bit of data, so would this be why if using Open Form?

I have the ContractID field on my Prices form, and it doesn't populate until I enter some data.

That's because you're writing the value in the Before Insert event, which doesn't fire until the user enters the first character in a new record. However, that would not prevent you from being able to use the OpenArgs value to set up your combo box. If you have a sanitized copy of your app that you can post, that might be helpful so we can duplicate the problem.
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
If your ContractID field is a number, then you shouldn't need the delimiters. Just to clarify what I mean, if the ContractID was text, then this line;

Code:
& " From tblMeters Where ContractID=" & Me.OpenArgs

would need string delimiters added like the following;

Code:
& " From tblMeters Where ContractID=""" & Me.OpenArgs & """"

similarly, if you were passing a date value you would need date delimiters (#).




What do you mean by "no joy"? You get no records? You get all records? You get an error? We need specifics. What does the code look like now and where did you put it? - I get no records. I will copy code tonight when I am home. I put the code in the Prices form the one with the MeterRef combo on it.



That's because you're writing the value in the Before Insert event, which doesn't fire until the user enters the first character in a new record. However, that would not prevent you from being able to use the OpenArgs value to set up your combo box. If you have a sanitized copy of your app that you can post, that might be helpful so we can duplicate the problem. - I will attach it tonight

I currently have a table in the Record Source of the combo, even if I remove that source and leave the code you supplied I get nothing.

I have commented out the Previous Open Arg code also and get nothing.

thanks again for your help.
 

Beetle

Duly Registered Boozer
Local time
Today, 09:15
Joined
Apr 30, 2011
Messages
1,808
Try this. In the Open event of your Prices form put the following code (again, adjust for correct object names). Comment out any existing code;

Code:
Dim strSQL AS String

strSQL = "Select * From tblMeters Where tblMeters.ContractID=" & Me.OpenArgs

MsgBox strSQL
Then open your Prices form from the command button on your Contracts form (so the ContractID gets passed via OpenArgs). Post back with what the message box says when the Prices form opens.
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
Try this. In the Open event of your Prices form put the following code (again, adjust for correct object names). Comment out any existing code;

Code:
Dim strSQL AS String

strSQL = "Select * From tblMeters Where tblMeters.ContractID=" & Me.OpenArgs

MsgBox strSQL
Then open your Prices form from the command button on your Contracts form (so the ContractID gets passed via OpenArgs). Post back with what the message box says when the Prices form opens.

Thanks.

I get

Select * from STORE_Meter_Refs Where STORE_Meter_Refs.ContractID=6
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
See attached.

Open Clients Form then look in Contract tab. button at top to open Prices form.

thanks
 

Attachments

  • Test.zip
    1,001.4 KB · Views: 72

Beetle

Duly Registered Boozer
Local time
Today, 09:15
Joined
Apr 30, 2011
Messages
1,808
The problem is that you've got the columns in your combo box out of whack. You have 13 columns defined in the combo box, with all widths set to 0" except for the 9th column. I'm a bit confused as to why it would have been set up this way because the Meter_Refs table only has 7 columns total. Plus if you use a SQL statement to set the Row Source then you may likely end up with even fewer columns. So the data is there but you can't see it because it's in a column for which you have the width set to 0".
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
The problem is that you've got the columns in your combo box out of whack. You have 13 columns defined in the combo box, with all widths set to 0" except for the 9th column. I'm a bit confused as to why it would have been set up this way because the Meter_Refs table only has 7 columns total. Plus if you use a SQL statement to set the Row Source then you may likely end up with even fewer columns. So the data is there but you can't see it because it's in a column for which you have the width set to 0".

Cheers.

I needed to do that as the client wants to pull through different fields such as Site Name, Client Name, Contract Ref, Start/End Date etc...

So an Afterupdate event on the combo box populates a host of fields on the form when they select a meter ref.

So what do you suggest to change, do I just need to figure out which column my Meter Refs are in a set width to 2cm to view it?

Sort of lost now as to what code I should and should not go with?

Cheers again.
 

YNWA

Registered User.
Local time
Today, 16:15
Joined
Jun 2, 2009
Messages
905
The problem is that you've got the columns in your combo box out of whack. You have 13 columns defined in the combo box, with all widths set to 0" except for the 9th column. I'm a bit confused as to why it would have been set up this way because the Meter_Refs table only has 7 columns total. Plus if you use a SQL statement to set the Row Source then you may likely end up with even fewer columns. So the data is there but you can't see it because it's in a column for which you have the width set to 0".

Also the 9th column of the combo is the meter ref column of the query.

I used a query as I need to pull fields in from different tables in order to get the data pulled through that the clinet wanted in the afterupdate event.

The STORE_Meter_Refs table stores Meter Ref info from a subform on the Contract tab. So the join between MeterRef and Contract is in there also.
 

Users who are viewing this thread

Top Bottom