Solved

Query results based on option group in Access on form

Posted on 2011-09-27
7
485 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
  • 5
  • 2
7 Comments
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
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
Comment Utility
<< 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:palmtreeinfotech
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Glad to help out!  Enjoy your weekend.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now