473,397 Members | 1,985 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,397 software developers and data experts.

query from Form to Subform

Hello,
Almost a newbie at VB and First time question in this forum!

I have a form "MAIN" with:
- 2 unbound textboxes ("startdate" and "endate")
- 1 optiongroup with 2 options (value "1" & "2")
- 1 unbound multiselect listbox "accountlist" shown if optiongroup value is "1" taking its (grouped) values from transactions table.
and a Subform "SUBFORM" with:-
account, transactiondate, amount fields taken from transactions table
I would like to get the result of a query in subform with the following variables:
- "transactiondate">="startdate" AND <="Endate" values taken from textboxes
- if optiongroup = 1, limit the result to those accounts selected in "accountlist" multiselect listbox
- if optiongroup = 2, select all accounts
Thank you very much for your precious help!
Dec 12 '06 #1
9 1536
NeoPa
32,556 Expert Mod 16PB
What SQL do you use to get all the info you want?
What are the names of the individual Options in the Option Group?
Dec 12 '06 #2
What SQL do you use to get all the info you want?
What are the names of the individual Options in the Option Group?
I don't get your first question, do you mean how do i get the info on the main form?
If your question is related to the SQL needed to update the subform, it is definitely part of my request!

The individual options names in the option group are:
-option1account
-option2allaccounts

thanks a lot !
Dec 12 '06 #3
NeoPa
32,556 Expert Mod 16PB
Thank you for the option names.
My first question meant :
What is the Record Source of the forms?
For subforms - what are the linked fields?
Please provide MetaData for any tables.
Here is an example of how to post table MetaData :
Posting Table/Dataset MetaData
Expand|Select|Wrap|Line Numbers
  1. Table Name=tblStudent
  2. StudentID; Autonumber; PK
  3. Family; String; FK
  4. Name; String
  5. University; String; FK
  6. MaxMark; Numeric
  7. MinMark; Numeric
Dec 12 '06 #4
MMcCarthy
14,534 Expert Mod 8TB
Thank you for the option names.
My first question meant :
What is the Record Source of the forms?
For subforms - what are the linked fields?
Please provide MetaData for any tables.
Ade

I think the main form is unbound and he wants to use the controls to set the record source for the subform.

Mary
Dec 13 '06 #5
MMcCarthy
14,534 Expert Mod 8TB
Firstly, you will need to add a command button to generate the records in the subform.

Create the subform to show all accounts of all dates i.e. the transactions table. Don't have any relationship i.e. master/child between the subform and the main form.

Then in the code behind the command button you will need the following:

Expand|Select|Wrap|Line Numbers
  1. Private Sub commandXX_Click()
  2. Dim strSQL As String
  3. Dim itemVal As Variant
  4. Dim accList() As Variant
  5. Dim count As Integer
  6.  
  7.    count = 0
  8.  
  9.    SELECT CASE optGroupName
  10.    CASE 1
  11.       For Each itemVal In Me.accountlist.ItemsSelected
  12.          accList(count) = Me.accountlist.ItemData(itemVal)
  13.          count = count + 1
  14.       Next itemVal
  15.  
  16.       strSQL = "SELECT account, transactiondate, amount fields " & _
  17.             "FROM transactions WHERE (transactiondate " & _
  18.             "BETWEEN #" & Me.startdate & "# AND #" & Me.Endate & "#) " & _
  19.             "AND account IN (" & accList & ");"
  20.    CASE 2
  21.       strSQL = "SELECT account, transactiondate, amount fields " & _
  22.             "FROM transactions WHERE (transactiondate " & _
  23.             "BETWEEN #" & Me.startdate & "# AND #" & Me.Endate & "#);"
  24.    END SELECT
  25.  
  26.    Me.SubformName.RecordSouce = strSQL
  27.    Me.SubformName.Requery
  28.  
  29. End Sub
  30.  
  31.  
Mary
Dec 13 '06 #6
NeoPa
32,556 Expert Mod 16PB
Something like this should at least start you off :
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtStartDate_AfterUpdate()
  2.     Call SetSQL()
  3. End Sub
  4.  
  5. Private Sub txtEndDate_AfterUpdate()
  6.     Call SetSQL()
  7. End Sub
  8.  
  9. 'Add similar procedures for listbox & option group
  10.  
  11. Private Sub SetSQL()
  12.     Dim strSQL As String, strWork As String
  13.  
  14.     strSQL = "SELECT *" & VbCrLf & _
  15.              "FROM [Transactions]" & VbCrLf & _
  16.              "WHERE ([TransactionDate] Between " & _
  17.              Format(Me.StartDate,"\#m/d/yyyy\#") & _
  18.              " And " & _
  19.              Format(Me.EndDate,"\#m/d/yyyy\#") & ")"
  20.     If option1account Then
  21.         'In here put code to add items to a string (strWork)
  22.         'formatted as a list separated by commas.
  23.         'Single-quotes should be used to surround each list item
  24.         strSQL = strSQL & " AND ([Account] In(" & strWork & "))"
  25.     End If
  26.     Forms!MainForm.[YourSubFormControl].RecordSource = strSQL
  27. End Sub
Dec 13 '06 #7
Hello guys,
thank you for your posts, were really helpful.

Here is the code I could get to work:

Expand|Select|Wrap|Line Numbers
  1. Private Sub SetSQL()
  2.     Dim strSQL As String
  3.     Dim strWhere As String
  4.     Dim itemVal As Variant
  5.     Dim accList() As Variant
  6.     Dim count As Integer
  7.     Dim lngLen As Long
  8.     Dim messqge As String
  9.  
  10.     strSQL = ""
  11.  
  12. strSQL = "SELECT *" & vbCrLf & _
  13.             "FROM [Transactions_table]" & vbCrLf & _
  14.              "WHERE ([Trade date] Between " & _
  15.              Format(Me.StartDate, "\#mm/dd/yyyy\#") & _
  16.              " And " & _
  17.              Format(Me.End_Date, "\#mm/dd/yyyy\#") & ")"
  18.  
  19.  
  20.     If Forms![Transactions].[Accountselect] = 1 Then
  21.  
  22.         With Me.accountlist
  23.         For Each itemVal In Me.accountlist.ItemsSelected
  24.         If Not IsNull(itemVal) Then
  25.         strWhere = strWhere & """" & .ItemData(itemVal) & """, "
  26.         End If
  27.         Next
  28.         End With
  29.         lngLen = Len(strWhere) - 2 'Without trailing comma and space.
  30.         If lngLen > 0 Then
  31.         strWhere = "[account] IN (" & Left$(strWhere, lngLen) & ")"
  32.         strSQL = strSQL & " AND " & strWhere
  33.         End If               
  34.  
  35.     End If
  36.     strSQL = (strSQL & vbCrLf & "ORDER BY [Trade date];")
  37.  
  38.     Me.Transactions_Query_subform.Form.RecordSource = strSQL
  39.  
  40. End Sub
Dec 15 '06 #8
MMcCarthy
14,534 Expert Mod 8TB
That's great.

I'm glad you got it working.

Mary
Dec 16 '06 #9
NeoPa
32,556 Expert Mod 16PB
Yes, I'm pleased for you.
Particularly as you've picked up from the suggestions and taken it further by yourself :).
Dec 16 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John | last post by:
Hi - I am trying to perform a simple append query, with no luck. I have a table (MktPrices) that has the following fields: BondID, PriceDate, Price. The objective is to allow the user to input a...
3
by: cassandra.flowers | last post by:
Hi, I was wondering if it is possible (Using access) to have a query parameter as a drop down box rather than a text box? e.g. typing as criteria for a query produces a box with a text box...
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...
1
by: Joe.Mobley | last post by:
All, When you sell a product we require access to remove that information from the stock table, so the product is sold througha comobo box named "Combo4" under the prodcuts sold form which is a...
2
by: Smartin | last post by:
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...
1
by: Stephen D Cook | last post by:
I have a form with txtSN text box, btnShow button, and a subform. The form is bound to a Table. The button is bound to a lookup query. The query is bound to the text box. I want to be able to...
0
by: Stephen D Cook | last post by:
I have a form linked to a table. In the form I have a textbox, a command button, and a query subform. I dragged the query onto the form to create the query subform. The query has a field named...
1
by: Macbane | last post by:
Hi There, I have a main form that with a subform which is based on a parameter query. I have 2 controls on the main form & which are used as parameters in the query. It all works fine and...
11
by: kabradley | last post by:
Hello Everyone, So, thanks to nico's help I was finally able to 'finish' our companies access database. For the past week or so though,I have been designing forms that contain a subform and an...
1
by: jcf378 | last post by:
Is it possible to set a main-form with an embedded subform to "Filter by Form", subsequently enter the desired variables in fields in BOTH the main-form and subform, and then save the resulting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.