473,414 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Bind a Subform to a Query

Using Access 97

I am trying to create a form that allows for user entry of search terms
and displays a table of results. I thought I could accomplish this using
a subform but it isn't quite doing what I want it to do.

What I have so far:

Fields to enter search terms
Subform bound to a query
Command button fires VBA code that...
....builds SQL SELECT statement using the search terms
....modifies the query object (to which the subform is bound) to take on
the new SELECT statement
....does subform.Requery

What happens is the subform displays data when the form is launched,
based on whatever the SELECT query happened to be at the time. However,
when the command button is clicked, the subform is not updated to
display the new record set, even though I have verified the query object
is successfully updated.

I would like to update the subform with the revised query when the
button is clicked.

I'm probably going at this all wrong... any suggestions are welcome.
Nov 13 '05 #1
2 5755
Ideally, a subform displays data in datasheet view. You have your
parameter fields in the main form along with your command button. For
the query which underlies your subform you can submit parameters using
custom functions.

Say your query (Query1) has 5 fields: FirstName, LastName, Phone,
Address, Zip

So your subform (frmSub), which is based on Query1 will also have 5
fields and will be in datasheet view (select "Datasheet" in the Default
View property of the subform's property sheet in design view - must
close main form first to access design view of subform).

In the query you will use a custom function which you will call in the
criteria field for one of the 5 fields. Lets use the FirstName field.
I will explain how you call the custom function after we create it.

We create a simple custom function in a standard code module (code
module section of Access). First, lets create a public var like this -
at the top of the code module:

Public fName As String

Then we create a simple function like this:

Function GetFname() As String
GetFname = fName
End Function

Save the code module. Drag the subform into the main form (Form1). Add
a text box to Form1 and a button. Text1 is where you enter the
FirstName value of a firstname you want to look up. Say your data table
contains 10 rows will firstName "Bill", 7 rows with firstName "Sue", and
so on. If you type Bill in Text1 the subform will display only rows
that have the FirstName of Bill. In your command button on the main
form (Form1) add this code:

Private Sub Command1_Click()
str1 = Text1
Me.Requery
Me.Refresh
End Sub

When you type Bill and click on the button you get all rows with
FirstName of Bill. If you type Sue in Text1, you get all rows with
FirstName of Sue...

To add the custom function to Query1 you simply type this in the
criteria field for the Firstname field of Query1:

= GetFname()

Save the query. Now open up form1 and type Bill in Text1 and click on
the button. The subform will display the desired records.

HTH

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #2
Rich P wrote:
Ideally, a subform displays data in datasheet view. You have your
parameter fields in the main form along with your command button. For
the query which underlies your subform you can submit parameters using
custom functions.

Say your query (Query1) has 5 fields: FirstName, LastName, Phone,
Address, Zip

So your subform (frmSub), which is based on Query1 will also have 5
fields and will be in datasheet view (select "Datasheet" in the Default
View property of the subform's property sheet in design view - must
close main form first to access design view of subform).

In the query you will use a custom function which you will call in the
criteria field for one of the 5 fields. Lets use the FirstName field.
I will explain how you call the custom function after we create it.

We create a simple custom function in a standard code module (code
module section of Access). First, lets create a public var like this -
at the top of the code module:

Public fName As String

Then we create a simple function like this:

Function GetFname() As String
GetFname = fName
End Function

Save the code module. Drag the subform into the main form (Form1). Add
a text box to Form1 and a button. Text1 is where you enter the
FirstName value of a firstname you want to look up. Say your data table
contains 10 rows will firstName "Bill", 7 rows with firstName "Sue", and
so on. If you type Bill in Text1 the subform will display only rows
that have the FirstName of Bill. In your command button on the main
form (Form1) add this code:

Private Sub Command1_Click()
str1 = Text1
Me.Requery
Me.Refresh
End Sub

When you type Bill and click on the button you get all rows with
FirstName of Bill. If you type Sue in Text1, you get all rows with
FirstName of Sue...

To add the custom function to Query1 you simply type this in the
criteria field for the Firstname field of Query1:

= GetFname()

Save the query. Now open up form1 and type Bill in Text1 and click on
the button. The subform will display the desired records.

HTH

Rich

*** Sent via Developersdex http://www.developersdex.com ***


Thanks for the tip. It didn't help me with this application but I'm
going to save your message for future reference.

For the record I was only one short step away from the solution to my
problem. Rather than having the subform bound to a query object I set
the RecordSource property of the subform at runtime to be the SELECT
statement built up from the text fields. This way I can use multiple
fields to create the query.

Again, thanks for your assistance.

Regards,
--
Smartin
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
0
by: Jason | last post by:
I have a primary form which is used to enter/edit data in a table named Test_Results. On this primary form there is a subform which displays site addresses. This subform is linked to the primary...
4
by: Dave Boyd | last post by:
Hi, I have two very similar forms each with a subform. The main form gets a few fields from the user and passes this back to a query that the subform is bound to. The requery is done when the...
9
by: PC Datasheet | last post by:
I'm stuck on something that seems should be easy and I need some help. My main form has an option group with five options. My subform chooses from different lists depending on which option is...
1
by: bcreighton | last post by:
I have created a bound subform on an unbound masterform linked together with a common field (A store's identification number) using an unbound combobox on the masterform and an invisible field on...
14
by: Kurt | last post by:
I have an unbound main form with an unbound subform. frmProjects fsubProjectList Using combo boxes, the user can select several search criteria on frmProjects and then click a command button....
1
by: Eric | last post by:
I wrote a query in Form_Open method but i didnt bind it with the textbox. How to i bind this whole method with the text box, as right now the textbox shows the last value of the recordset. The...
1
by: Stinky Pete | last post by:
Hi everyone, I have been updating a file that uses a main form that contains a subform (as a datasheet). The main form really does not do anything on opening except maximizes to the users...
1
by: adolph | last post by:
I wrote an access2000 database for a POS system. In it is a sales form with a a subform showing the items being purchased. The main form is unbound. It uses a class to hold the main form data...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.