Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...your web site's great! I've been using this system for almost a year now and find it really, really helpful. The people have been helpful in answering just about any question you post in the forums..."

Geography

Where in the world do Tek-Tips members come from?

Multivalue field in subform as criteria in query

BrianWen (Programmer)
30 Apr 12 5:52
Hi all!

First of all, I know of the implications of using multivalue fields. The data I store in the field is on the fly data (what the user has selected for filtering).

So, let me first explain the setup:

1. Table (filter) that contains a few fields that for each user contains what the user has selected to filter in a form (query). One of those fields is a multivalue field, so that they can select 5,7 and 8 for instance.

2. Table with main data to be displayed via a form.

3. The form that displays the main data. The form's data source is a query, that has criteria, so that what the user has in the filter query is what is displayed from the main data table.

4. Filter form bound to the filter table. This a put on the main data form as a subform in the form header.

So, the subform with the bound multivalue field is displayed through the main data form. I change select some values in the multi value field and some code requeries the main data, which is based on a query, that looks in the subform control. I don't get the popup asking a value but I get no records, even though there are of course matching records.

My query has this criteria on the appropriate field: Forms![Dataform]![Filter_subform].Form![multivaluefield]

Got any good ideas on what could be wrong?

Please ask if I missed some important info.
MajP (TechnicalUser)
30 Apr 12 6:54
I have never tried this so I am not certain.  But I think you may have to use the value property of the multi valued field in your criteria.  See link

http://office.microsoft.com/en-us/access-help/using-multivalued-fields-in-queries-HA010149297.aspx
BrianWen (Programmer)
30 Apr 12 8:27
Problem is I'm not using the multivalue field as criteria in the query, but instead a control bound to the field.

It doesn't complain of this as criteria either, but it doesn't return any records:

Forms![Dataform]![Filter_subform].Form![multivaluefield].Value
MajP (TechnicalUser)
30 Apr 12 10:31
I have no idea what the value of a control bound to a multivalued field. It is a non null variant, but that is about it as far as I can tell. You can test this in code by trying to return the value of a control bound to a multivalued field. I could not find any info on this.  Maybe someone else knows.

So I doubt that the value of the control can be used as a criteria in another query.  

The value of the multi valued field is a recordset.  But as far as I can tell the value of the control is not a recordset.  If this was me, and I had to do this I would build the sql string in code instead of trying to use the control as a criteria.  You can return the recordset of the field, loop the records, and build the criteria.
MajP (TechnicalUser)
30 Apr 12 12:02
I cannot figure out how to use the value of an MV control as a criteria for a query.  But I can build a function that turns the value of the MV field into a criteria to use in a query.

CODE

Public Function getFilterString(mvField As Variant, Optional andOr As String = "AND")
  Dim aStr() As String
  Dim str As String
  Dim i As Integer
  aStr = Split(mvField, ",")
  andOr = " " & andOr & " "
  For i = 0 To UBound(aStr)
    aStr(i) = "'" & Trim(aStr(i)) & "'"
    If str = "" Then
      str = aStr(i)
    Else
      str = str & andOr & aStr(i)
    End If
  Next i
  getFilterString = str
End Function

I am using the products table from Northwind.  So if I can select products I can build a string like

'Aniseed Syrup' AND 'Boston Crab Meat' AND 'Camembert Pierrot' AND 'Carnarvon Tigers' AND 'Chai'

That can be used to query the multivalue field or any field.

Note: Need to add error checking for passing a Null value of the muli value field.
BrianWen (Programmer)
1 May 12 2:10
As far as I know it's not neccesary to construct a long SQL manually by looping over the multivalue recordset. I believe can be 'In(controlname)'.
MajP (TechnicalUser)
1 May 12 6:48
I doubt it.  As I have proven the control value is not a string.  You might be able to do it the field value which is a comma seperated string.  However you cannot not use a field value in a query without a custom function to return the field value.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close