Solved

Query results based on option group in Access on form

Posted on 2011-09-27
7
553 Views
Last Modified: 2012-05-12
Hello to all.  I have this issue here that was solved yesterday but it still is puzzling me a little bit.

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_27343036.html

The guys were great so here is what I am wanting that's not quite happened yet.

I have an option group box called "ogFind".  In the 3 options in the Option Group, 0=All records, 1=find by ID, and 2=Find by name.

I have a regular command button to run Query1.  I do not have any parameters in the criteria field in the query due to it being in the SQL.  In other words, it's not attached to the form in any way.  I do not have the Like "*" & [Forms]![Form1]![Text1] & "*" in any of the criteria fields.  This is one of the things I'm not sure about.

I have an unbound textbox called "text1".    In the "after update" command code I have the attached code.

It still seems it doesn't work.  Even if there is text in the textbox and I have "All Records" chosen, shouldn't it still bring up all records?  When I choose Option 1 or Option 2, it doesn't seem to be attaching itself to the combo box, in other words, no matter what I choose in the Option group, it still gives me the same results.

I am trying to get the user to type in something in the textbox and have the query find it then display the results based on the option they have chosen.

Any more help would be awesome!

Private Sub Text1_AfterUpdate()
 Dim vSQL
    Select Case ogFind
    Case Is = 0
        vSQL = "SELECT * FROM table1;"
    Case Is = 1
        vSQL = "SELECT * FROM table1 where ID = " & text1 & ";"
    Case Is = 2
        vSQL = "SELECT * FROM table1 WHERE Name1 LIKE " & Chr(34) & "*" & Me.text1 & "*" & Chr(34)
    End Select
    Me.RecordSource = vSQL
    
End Sub

Open in new window

0
Comment
Question by:palmtreeinfotech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36716022
The After Update event of your textbox doesn't seem like the correct place for this code.  I would place it in the After Update event of your option group, or behind a seperate command button.

Secondly, double check the properties of your option group to ensure that you are using the correct name and values in your code.

- Ensure that the name of your option group is ogFind (this is the Name property of the frame surrounding your radio buttons)

- Ensure that your option values are indeed  0,1,2.  These are the Option Value properties under the Data tab for each of the radio buttons (or whatever option controls you are using).  When using the default, these Option Values start at 1.

If these ideas don't help, post a sample of your database including just the relevant parts.  Mask or remove any sensitive data before uploading your sample.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36716034
Also, to ensure that all of your variables are defined, add the following line to the top of your code module, directly under Option Compare Database:

Option Explicit

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36716046
<< it doesn't seem to be attaching itself to the combo box  >>

Finally, what do you mean by 'attaching itself to the combo box'?  Are you trying to limit records in your form, or are you trying to limit rows seen in a combobox?

If you are trying to limit rows in a combo box, the following line needs to be changed:

>>>     Me.RecordSource = vSQL

That defines the records seen in the current form.

THIS defines rows seen in a combo box:

    Me.MyComboBox.Rowsource = vSQL
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:palmtreeinfotech
ID: 36717186
Hi mbizup thanks for your comments.  I have attached the database Access 2010 if you can open it.  What I mean by not attaching itself to the combo box is that nothing seems to be working as planned when using the combobox.  My default value is "0" and "1" and "2".

The way it is setup now, it should run the query and attach itself no matter what I option I choose on the combobox.  It doesn't seem like the combobox and textbox are working together.

There was an old "orders" database that I saw a long time ago but couldn't find it on the net. Database4.accdb
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 36718007
Okay - a few issues.  We didn't have the full picture in your previous question.

1.  The code we gave you is designed to filter records on the same form as the option group (not a seperate query).
2.  We didn't know about the combo box.  Your code is currently referring to the textbox when filtering on the name.
3.  The "run query" button is not related in any way to the optiongroup or your new code.

To fix this don't need to change the code much at all - just the layout of the form:

1.  Move the controls to the form header section
2.  Add a couple of textboxes bound to ID and Name in the detail section of your form, and set the default view to "Continuous forms"
3.  Change the code for "Name" option to this, so that it refers to the combo box where the user selects a name (this is the only line of code that needs to be changed):

        vSQL = "SELECT * FROM table1 WHERE Name1 LIKE " & Chr(34) & "*" & Me.Combo12.Column(1) & "*" & Chr(34)


Give this upload a try, using the "Run Filter" button  - there is a ton more you can do with it... but this meets your basic requirements.
Database4.accdb
0
 

Author Closing Comment

by:palmtreeinfotech
ID: 36890552
hi mbizup and sorry for the late reply.  I will check it out.  yeah I knew there were problems and I was not explaining it too well.  I appreciate you sticking with me and answering my question.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36890576
Glad to help out!  Enjoy your weekend.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question