• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 599
  • Last Modified:

How to search for records in a subform within a form?

Hello,

I would like to add a contact log to the database I am creating.

I have figured out how to create a functional search combo box to browse through records and edit the information on those records.

I am using the following Macro on the search combo box:
Apply Filter: [YthID]=[Forms]![frmIntake]![ComboYth]

Now, I am moving into relational tables ("many" contact logs to "one" person). I have been able to add a subform (a contact log) to this form to enter data only. And it works.

I would also like to add a table showing the summary of these contact logs either on this same form (as a subform) or on a popup form.

How can I link the search combo box (located on the Form Header) to the primary key (YthID) on a subform? What should I put on the Macro builder?

Thank you in advance!
sos.JPG
Contact-Log-Subform-embedded-int.JPG
0
terraks
Asked:
terraks
  • 9
  • 8
  • 3
1 Solution
 
LucasCommented:
In the "On Click" event of your combo box you can have something like this:

Me.NameofYourSubForm.Form.Filter = "YthId=" & Me.ythIdCombo & ""
Me.NameofYourSubForm.Form.FilterOn = True
0
 
terraksAuthor Commented:
This is what I typed in:

Me.sbfrmLogTable.Form.Filter = "YthID=" & Me.ComboLogYth & ""
Me.sbfrmLogTable.Form.FilterOn = True

When I try it out, it tells me that it cannot find the Macro "Me".

0
 
LucasCommented:
don't put it in a macro.  This should be in your VBA code.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
terraksAuthor Commented:
Is there a way to do it without VBA? I don't know how to use VBA.

I typed:

Me.sbfrmLogTable.Form.Filter = "YthID=" & Me.ComboLogYth & ""
Me.sbfrmLogTable.Form.FilterOn = True

on the right of the "On Click" field under the "Event" tab in the "Properties" window for the combo box.

Thanks, lucas911.
0
 
SheilsCommented:
OK,

My first advise is that you stop using Macro and start writting VB codes for these action. I looks and sound difficult when you first start but believe me, it is so much more powerful than macro that you won't look back. You can ask every expert on this panel and I can bet that non of them use macro anymore.

Back to your question

To create a subform do the following:

1) create the subform as a normal form
2) open the main form in design view.
3) select the subform icon and use it to create the subform control on your main form. Then select the form that you created in step one as the source object of the subform control. During this process you will also be asked to select the child and master link. Select the YthID on the main form (the combo) and YthID on the subform as the child master link.

That's your form/subform done.

Now all you have to do is refresh the subform every time you change the value of the combobox. To do this follow the following steps:

1)Open the main form in design view.
2) right click on the combo box and select property.
3) In the property dialog box go to the event tab. Then go to On Change and type event procedure next to it. Then double click on the box on the right hand side of this textbox.
4) The code editor window will open and your cursor will be between the following

Private Sub ComboYth_Change()


End Sub

Type the following code before end sub

Me.SubformName.Form.Refresh

------------------

NB: the subform name is called child1,child2 .... by default. It is always good to rename them to something meaningful to help with your coding. To find the name of a child right click on it in design view. Go to the other tab and the name will be in the name textbox


 


0
 
SheilsCommented:
<Is there a way to do it without VBA? I don't know how to use VBA.>

That was me 5 years a go. I soon found out that there was no way to achieve the stuffs I wanted to achieve without vba. You are about figure that out too. It  will be a steep learning curve but achievable. I am self thought and I believe everybody can do it if they put there mind to it.
0
 
LucasCommented:
sb9 don't be too hard on them ;-)

Here is a macro way on your ComboBox:


Action: ApplyFilter
Where Condition: YthID= ComboLogYth
Control Name: sbfrmLogTable
0
 
SheilsCommented:
OK Lucas, I am sure that terrak will find this in his own time like we all did.

 Coool
0
 
terraksAuthor Commented:
Thanks, sb9.

I was able to insert the form as a subform, but it didn't ask me for the child/master link. Instead, I was able to right click on the subform and go to the Properties dialog box and click on the Data tab where I can set the Child and Master links. However, when I click on the "..." it tells me: "Can't build a link between unbound forms." So I went ahead and typed "YthID" on both child and master link fields.

Subform Properties/Data (frmLogTable)
Source Object: frmLogTable
Link Child Fields: YthID
Link Master Fields: YthID

I typed this on VBA for the combo box under On Change:
Private Sub ComboLogYth_Change()
Me.frmLogTable.Form.Refresh
End Sub

Then I go to "Form View" and it asks me to enter a parameter value for YthID. I click OK without inputting anything and nothing happens--when I select the different YthID's on the search combo box, it does not change the YthID on the subform (shown as a table).


SIDENOTE: When I go on the dashboard and click on tables, I see the master table, I can see the Contact Log table as a subtable. I think this is so because I linked the YthID's through relational tables. Not sure if this affects the child/master links on the subform.
0
 
SheilsCommented:
Good start!

<Then I go to "Form View" and it asks me to enter a parameter value for YthID>

This is telling me that a field by this name does not exist in either the form or the subform. Check the subform and make your that it has a field with datasource YthID. Or maybe the related datasource has another name. If so change the Link Child Fields to that field name.

<when I select the different YthID's on the search combo box>

As I said before the default name of a subform control is child#. It is a common rockie error (got me confused and frustrated before I figured it out) to confuse between the subform and the subform source object. In this case your source object is frmLogTable. Your subform has another name. Open the property dialog box of the subform (the one which contains child/master links) and check the name in the other tab. I suggest that you change it to chdLogTable.

Then your code is:

Private Sub ComboLogYth_Change()
Me.chdLogTable.Form.Refresh
End Sub
0
 
SheilsCommented:
If refresh don't work try

Private Sub ComboLogYth_Change()
Me.chdLogTable.Form.Requery
End Sub
0
 
SheilsCommented:
<SIDENOTE: When I go on the dashboard and click on tables, I see the master table, I can see the Contact Log table as a subtable. I think this is so because I linked the YthID's through relational tables. Not sure if this affects the child/master links on the subform.>

That will cause a subform to be automatically generated if you create an auto form from the main table. But otherwise it should not affect anything.

When assigning the child master link use the dropbox in the dialog and it should give you a list of available fields from the form and subform.
0
 
terraksAuthor Commented:
The field YthID exists within the subform. And it is being referred to by the combo search box. I made sure to create a 8 records and I added some contact logs, just to give the database a try.

So, I'm following your lead sb9, but it's not quite working for me.

The subform's name was frmLogTable (as it says on the Database/Dashboard) now is chdLogTable. After going to the form containing the subform, I went to the properties of the subform and changed the source object to chdLogTable.

I went to VBA and this is what is says:

Private Sub ComboLogYth_Change()
Me.chdLogTable.Form.Requery
End Sub

When I go to Form View, it asks me to enter the parameter value for YthID. I click OK, then I can view the form. When I search with the combo search box, nothing changes on the table below it. Except that I get the error message on the image below.


error.JPG
0
 
terraksAuthor Commented:
lucas911,

I'm giving Macro a try as well. And this is what's going on. After I add:

Action: ApplyFilter
Where Condition: YthID= ComboLogYth
Control Name: sbfrmLogTable

to the combo search dialog box and I view it on Form View, I can't click on any of the names on the list.
0
 
terraksAuthor Commented:
sb9,

When I try to see the available fields to add to the child/master links, it tells me
Can't build a link between unbound forms.
This is why I typed in YthID on both. But it keeps asking me for a parameter value.. even though YthID does exist as a field on the table where the subform is coming from.
0
 
SheilsCommented:
Ok Check out this page on form subform reference:

http://www.mvps.org/access/forms/frm0031.htm

<When I go to Form View, it asks me to enter the parameter value for YthID> IT should not be asking you for this unless the combo box is unbounded. Actually this must be what is happening. Which means that there is no control with YthID as its datasource in the main form.

Try inserting a text box in the main form header and use YthID as its data source.

Then change the onchange event of the combobox to:

Private Sub ComboLogYth_Change()

DoCmd.FindRecord Me.ComboLogYth

End Sub

This will cause the main form to go to the record that you have selected in the combobox. This should be seen in the new textbox (you can make it invisible once its all working) and change the subform.
0
 
SheilsCommented:
<even though YthID does exist as a field on the table where the subform is coming from>

That doesn't matter. It must exist as the data source of a control on the main form
0
 
terraksAuthor Commented:
Thanks, sb.

I ended up doing something different.

I embedded the subform through the subform wizard. I added it to the main working form where the combo search box has been working well. The child/master link recognizes YthID now and when I go to Form View, it allows me to filter and see the contact logs of specific people (YthID).

Thanks for helping. Since I tried the different approaches but none of them worked for me, how should I go about closing this question? I'm new in E-E so I just want to make sure that I don't do anything unfair, but at the same time I don't want to select any one answer as the solution because it didn't work for me.. Should I just rate the comments?

Thanks again for your help.
0
 
SheilsCommented:
If you have solve the problem yourself you select your comment as solution and no one gets the point.

However, what you said you have done is exactly what I told you to do in my first post.

You said: "I embedded the subform through the subform wizard."

I told you to: "3) select the subform icon and use it to create the subform control on your main form. Then select the form that you created in step one as the source object of the subform control. During this process you will also be asked to select the child and master link. Select the YthID on the main form (the combo) and YthID on the subform as the child master link. "

Isn't that the step for "embeding the subform through the subform wizard."

I am not sure what you did the first time but following my instruction step by step would have embedded the subform. A child/master link would have been suggested and you would have the option to choose alternative links.
0
 
terraksAuthor Commented:
You're right, and that's true. Your way worked though I interpreted as something else. Thank you!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 9
  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now