Solved

Query results based on option group in Access on form

Posted on 2011-09-27
7
491 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
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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