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

Access 2007 - VB If, then, else query issue

The front end form has the following fields across the page - all are combo boxes:

Cat# Category Description Cat Part#


The first field is sourcing a seperate table from the other two. The second and third fields are sourcing the same table, just different columns. Similar to a cascading combobox, I wish to limit the list of descriptions and parts selectable.

To make this task simpler, I created 7 seperate queries (there are 7 categories). The idea is to call the query required based on the category selected. This would then display the parts required for that particular category.

I have written the following VB code to call the required query:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Cat___A_Click()
  2.  If TN_IDU_Ref.[Cat#] = 1 Then
  3.     DoCmd.OpenQuery ([Cat 1 Query])
  4. ElseIf TN_IDU_Ref.[Cat#] = 2 Then
  5.     DoCmd.OpenQuery ([Cat 2 Query])
  6. ElseIf TN_IDU_Ref.[Cat#] = 3 Then
  7.     DoCmd.OpenQuery ([Cat 3 Query])
  8. ElseIf TN_IDU_Ref.[Cat#] = 4 Then
  9.     DoCmd.OpenQuery ([Cat 4 Query])
  10. ElseIf TN_IDU_Ref.[Cat#] = 5 Then
  11.     DoCmd.OpenQuery ([Cat 5 Query])
  12. ElseIf TN_IDU_Ref.[Cat#] = 6 Then
  13.     DoCmd.OpenQuery ([Cat 6 Query])
  14. ElseIf TN_IDU_Ref.[Cat#] = 7 Then
  15.     DoCmd.OpenQuery ([Cat 7 Query])
  16. End If
  17. End Sub
When I run the code a box pops up asking for a macro name. Any idea why? I would appreciate code suggestions as this is a a work related project that is quite urgent. Thanking you in anticipation, Patricia
Mar 28 '11 #1
9 3173
TheSmileyCoder
2,322 Expert Mod 2GB
You do realise that Docmd.Openquery will open/run the query as if you had double clicked it in the object explorer window?

If this is a matter of limiting selections for the user in regards to the next combobox in the line of selections, you should modify the recordsource of that combobox.

If opening the query is truly what you want you should enclose the query name in double quotes. Example:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenQuery ("[Cat 7 Query]")
Mar 28 '11 #2
Thank you for the feedback. I have emended the code as suggested, now I am receiving another error message:

"Run-time error '424':
Object required

I added the following before the first "if" statement:

Me.Cat___A = TN_IDU_Ref.[Cat#]

but it makes no difference. The first if statement is highlighed and the error message pops up. What am I missing? I understand that the error on the Else condition is that TN_IDU_Ref.[Cat#] does not = 1, so the first condition is skipped. I'm just not sure how to get around it.

The problem with modifying the recordsource of the combobox is that the secondary source has repeated category numbers, eg:

1 description
1 description
1 description
1 description
etc..

whereas the first combo box lists

1
2
3
4
5
6
7

I want the related query to be called based on the number selected so the user doesn't see multiple numbers, only the second column with the related descriptions. Any suggestions on how to achieve this?
Mar 28 '11 #3
Ok, I have decided to take another approach. I turned the TN_IDU_Ref table into a pivot table and created a LEFT JOIN from the Cat # table. Then wrote the following code:

Private Sub Cat_Part_A_AfterUpdate()
Dim Category_Description As String

sCategoryDescriptionSource = "SELECT [TN_IDU_Ref].[Cat#]," & _
" [TN_IDU_Ref].[Category_Title]," & _
" [TN_IDU_Ref].[Category_Description] " & _
" [TN_IDU_Ref].[Part_No#] " & _
" [TN_IDU_Ref].[Quantity] " & _
"FROM TN_IDU_Ref " & _
"WHERE [Cat#] = " & Cat#.Cat#
Me.Cat_Part_A.RowSource = sCategoryDescriptionSource
Me.Cat_Part_A.Requery

End Sub

Once a gain, when I run the code a box pops up asking for a macro name. I tried placing brackets, as per the previous post, but it makes no difference.
Mar 29 '11 #4
TheSmileyCoder
2,322 Expert Mod 2GB
I want the related query to be called based on the number selected so the user doesn't see multiple numbers, only the second column with the related descriptions. Any suggestions on how to achieve this?
If you have a combobox, with 2 columns where the first is a Key column and the second is the column you want to display look at the Column properties of the combobo:
Expand|Select|Wrap|Line Numbers
  1. Column Count=2
  2. Column Width=0;3cm
  3. List Width  =3cm

Regarding your code (please use code tags [code]your code here[/code]):
Expand|Select|Wrap|Line Numbers
  1. Private Sub Cat_Part_A_AfterUpdate()
  2. Dim Category_Description As String
  3.  
  4. sCategoryDescriptionSource = "SELECT [TN_IDU_Ref].[Cat#]," & _
  5. " [TN_IDU_Ref].[Category_Title]," & _
  6. " [TN_IDU_Ref].[Category_Description] " & _
  7. " [TN_IDU_Ref].[Part_No#] " & _
  8. " [TN_IDU_Ref].[Quantity] " & _
  9. "FROM TN_IDU_Ref " & _
  10. "WHERE [Cat#] = " & Cat#.Cat#
  11. Me.Cat_Part_A.RowSource = sCategoryDescriptionSource
  12. Me.Cat_Part_A.Requery
  13.  
  14. End Sub
Line 6 and 7, your missing a ",".

What is Cat#.Cat#?

Asumming you want to reference the combobox which you just updated, it would look like:
Expand|Select|Wrap|Line Numbers
  1. WHERE [Cat#] = " & Me.Cat_Part_A
Mar 29 '11 #5
Thanks for the update....still no joy. The macro pop up appears when I run it (just like before). Tried adding brackets, but Access just highlighted the code in red.

All I want to do is create several caascading combo boxes, the first being Cat # (select 1-7), this would display the selected category listing (eg: Cat 1 materials if Cat#1 selected), then you would select one of the materials, which then populates the table with the associated parts and required quantities. Some category materials have only one peice of material, others have up to 10, so all materials and associated quantities need to be listed when the user selects the category and the material.

Perhaps there is another way to achieve this? That's why I decided to take another approach. I turned the TN_IDU_Ref table into a pivot table and created a LEFT JOIN from the Cat # table.....but still no success. Not sure what to do now. What would you suggest as the best approach?
Mar 30 '11 #6
Had another thought......what about using Case instead? I'm not sure about the syntax, something similar to:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Cat___A_AfterUpdate()
  2. Dim sqltext As String
  3. Dim rst As DAO.Recordset
  4. Dim db As DAO.Database
  5.  
  6. Select Case Me.Cat#
  7.  
  8.    Case "1"
  9.       Set db = BOMUpdate
  10.       [Cat#] = 1
  11.       DoCmd.OpenQuery [Cat 1 Query]
  12.       DoCmd.RunSQL sqltext = "INSERT INTO BOMUpdate.CatPartA"
  13.    Case "2"
  14.       [Cat#] = 2
  15.       DoCmd.OpenQuery ["Cat 2 Query"]
  16.       DoCmd.RunSQL sqltext = "INSERT INTO BOMUpdate.CatPartA"
  17.    Case "3"
  18.       [Cat#] = 3
  19.       DoCmd.OpenQuery ["Cat 3 Query"]
  20.       DoCmd.RunSQL sqltext = "INSERT INTO BOMUpdate.CatPartA"
  21.    Case "3"
  22.       [Cat#] = 4
  23.       DoCmd.OpenQuery ["Cat 4 Query"]
  24.       DoCmd.RunSQL sqltext = "INSERT INTO BOMUpdate.CatPartA"
  25.    Case "5"
  26.       [Cat#] = 5
  27.       DoCmd.OpenQuery ["Cat 5 Query"]
  28.       DoCmd.RunSQL sqltext = "INSERT INTO BOMUpdate.CatPartA"
  29.    Case "6"
  30.       [Cat#] = 6
  31.       DoCmd.OpenQuery ["Cat 6 Query"]
  32.       DoCmd.RunSQL sqltext = "INSERT INTO BOMUpdate.CatPartA"
  33.    Case "7"
  34.       [Cat#] = 7
  35.       DoCmd.OpenQuery ["Cat 7 Query"]
  36.       DoCmd.RunSQL sqltext = "INSERT INTO BOMUpdate.CatPartA"
  37. End Select
  38.  
  39.   End Sub   
Maybe that would work better?
Mar 30 '11 #7
By the way I used your suggestion of
Expand|Select|Wrap|Line Numbers
  1. your code here
, hope I did it correctly.
Mar 30 '11 #8
TheSmileyCoder
2,322 Expert Mod 2GB
To be clear, you have 3 comboboxes. After making a selection in combobox 1, you want to update combobox 2, and after making a selection in combobox 2, you want to update combobox 3?

Your latest piece of code seems to be complete gibberish to be honest.

What type of query are [Cat 1 Query]? Is it an action query (Update/append/delete) or a selection query?

Assuming it is an action query you want to run for some reason, it should be:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenQuery "Cat 7 Query"
This is because the Docmd.Openquery expects a string parameter detailing the name of the query to run.

You use
Expand|Select|Wrap|Line Numbers
  1. Set db = BOMUpdate
but only for case 1
Is that intentional? what is BOMUPDATE?

Finally your syntax for
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL sqltext = "INSERT INTO BOMUpdate.CatPartA"
is completely off. When that code is run, access will first evaluate the sqltext = "INSERT INTO BOMUpdate.CatPartA" and since that is not the case, the comparison will return false. Then access will proceed to try to run the code:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL FALSE
which makes no sense.
Correct syntax would be:
Expand|Select|Wrap|Line Numbers
  1. sqlText="INSERT INTO BOMUpdate.CatPartA"
  2. DoCmd.RunSQL sqltext 
That said, your SQL syntax itself gives no meaning. Insert WHAT into BOMUpdate.CatPartA?
Mar 30 '11 #9
To answer your questions:

Yes, I have 3 comboboxes. After making a selection in combobox 1, it updates combobox 2, and after making a selection in combobox 2, it updates combobox 3.

Cat 1 Query is a selection query (select Cat 1 displays descriptions associated with that category, including all the associated parts and quantity required for each part).

BOMUPDATE is the name of the database and the table that the selections are inserted into. Set db = BOMUpdate is for all 7 categories, I didn't realise that I had set it for the first case only.

Insert WHAT into BOMUpdate.CatPartA? Insert the part number associated with the selected Category into column CatPartA in the BOMUpdate table.

The difficulty lies in that some category descriptions have only one part associated with it, while others can have up to 10 parts for that one description. A category can have up to 100 descriptions, so it makes it easier for the user to first select a category, then choose the required description and they get to see all the parts required. This then populates a table which will later transfer the data into a pivot table which sources data from multiple tables.
Mar 30 '11 #10

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

Similar topics

4
by: Scott | last post by:
I insert a number into textbox1 between the number of 1 and 37 and i need to manipulate what the user puts in. For instance if they put between 1 and 5 they get a certain price and between 6...
5
by: ARC | last post by:
Hello, A user posted a reply to a post of mine regarding the fact that Access 2007 would be free, or maybe I misunderstood? All I could find on Microsoft's site was information regarding the...
12
by: Reg (Lincolnshire) | last post by:
Converted an A2003 system to A2007, on testing founf out that right- click to bring up a shortcut menu on a combo box didn't work if the combo box was in a subform. Found out that this was a...
3
by: IntelliOfficer | last post by:
The data I am using was imported from Excel. The tables were then merged into one large table (3 million + records) and so cannot be re-exported into excel for modification. I am trying to map the...
1
by: AccessGuy | last post by:
Happy New Year All! I have 2 queries, a qry_HISTData and qry_RAWData. qry_RAWData is automatically triggered from a subform using the "asofdate" of the mainform as a date parameter. What I'm...
1
by: david.triplett | last post by:
I am using MS Access 2007 and have an issue with writing queries against ODBC connected tables. After providing the appropriate ODBC connection string in the properties page, the Show Tables...
2
by: Wayne | last post by:
I'm trying to overcome the problem that some users including myself have noticed with combo boxes in A2003 databases that are run in A2007. When the combo is tabbed out of the backstyle changes to...
2
by: brat33 | last post by:
I am trying to modify some code to create a mail merged label document within Word 2007, using a Access 2007 Parameter Field. My issue comes about when I cannot see the Parameter Query within the...
1
by: Lori Jones | last post by:
Access 2007, have seen several posts on this, but no answer. Trying to load from Query in the Advanced Filter, but no filters/queries show up. I can create a filter and choose to save it as a...
3
by: Edward Gorman | last post by:
Hello Everyone, I'm having abit of difficulty with an aspect of Access. I wish to create a query which changes a persons wage depending upon their age. This is what I have done so far: SELECT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.