Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2003, 09:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

First, what's all this talk about putting the option group's code in the BEFORE UPDATE event? You should be putting this in the AFTER UPDATE event. You want to change the combobox's rowsource immediately after an option button is chosen. If you put it in BEFORE UPDATE, you have not chosen an option yet so how will it know which one you want? Your code will look something like this:

Code:
Private Sub Frm_InvUpd_Category_AfterUpdate()

   Dim strCategory as String

   Select Case Me.Frm_InvUpd_Category.Value
      Case 1
         strCategory = "Long Range"
      Case 2
         strCategory = "Mid Range"
      Case 3
         strCategory = "Putt & Approach"
      Case 4
         strCategory = "Specialty"
      Case 5
         strCategory = "Recreational"
   End Select

   Me.Cmb_InvUpd_Prod.RowSource = "SELECT Product.Product_Name " & _
      "FROM Product WHERE (((Product.Product_Category) = '" & _
      strCategory & "')) ORDER BY Product.Product_Name;"

End Sub

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old November 14th, 2003, 09:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Oops... I forgot two closing parentheses before the "ORDER BY" in the rowsource statement. I edited them in.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old November 14th, 2003, 02:14 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That code doesn't cause an error but it doesn't populate the combo box either...

I wish I had a better understanding of VBA
 
Old November 14th, 2003, 02:36 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What Braxis meant is that
"SELECT Product.Product_Name" & _

should be
"SELECT Product.Product_Name " & _

Basically, add an extra space at the end or the line before the " (Except for the last line).

Have you tried to put the SQL String on a query to see what it returns?
By the way, use " double quotes for string literals.



Sal
 
Old November 14th, 2003, 03:03 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yeah, the queries work great. I can write SQL all day. Just don't know VBA very well
 
Old November 14th, 2003, 06:05 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is my latest attempt at failure...



Private Sub Frm_InvUpd_Category_AfterUpdate()

Select Case Frm_InvUpd_Category.Value

Case 1
     Me.Frm_InvUpd_Category.Value = 1
     Me.Cmb_InvUpd_Prod.RowSource = "SELECT Product.Product_Name " & _
                                     " FROM Product " & _
                                     " WHERE (((Product.Product_Category) = 'Long Range')) " & _
                                     " ORDER BY Product.Product_Name;"

Case 2
    Me.Frm_InvUpd_Category.Value = 2
    Me.Cmb_InvUpd_Prod.RowSource = "SELECT Product.Product_Name " & _
                                 "FROM Product " & _
                                 "WHERE (((Product.Product_Category) = " 'Mid Range'")) " & _
                                 "ORDER BY Product.Product_Name;"

Case 3
Case 4
Case 5

End Select
End Sub

I've also tried it this way to no avail...

Private Sub Frm_InvUpd_Category_AfterUpdate()
Dim strCategory As String
Select Case Me.Frm_InvUpd_Category.Value
     Case 1
         strCategory = "Long Range"
     Case 2
         strCategory = "Mid Range"
     Case 3
         strCategory = "Putt & Approach"
     Case 4
         strCategory = "Specialty"
     Case 5
         strCategory = "Recreational"
End Select
Me.Cmb_InvUpd_Prod.RowSource = "SELECT Product.Product_Name " & _
      "FROM Product WHERE (((Product.Product_Category) = '" & _
      strCategory & "')) ORDER BY Product.Product_Name;"

End Sub
 
Old November 14th, 2003, 07:11 PM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have you tried stepping through your code?
Do this, put this line anywhere

MsgBox "MESSAGE"

Does it display when you run the sub?

Do you have data in the table?

when you open the form in design view and check the properties, under Events, do you have an [Event Procedure] by the elipsys next to the After update event for the frame control?

I keep getting mixed up with this name Frm_InvUpd_Category for a frame (it is not a frm it is a cbo). the name should be something close to cboSomething.




Sal
 
Old November 17th, 2003, 02:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Quote:
quote:Originally posted by Anubis
 That code doesn't cause an error but it doesn't populate the combo box either...
 If there is no error and the combobox won't populate then are you sure that the table you're referencing, namely Product, has any data that meets the criteria? Perhaps it's giving you no items because there are no items that meet your criteria. Check your criteria, field names, and table closely to see if there is data in there that matches what you want.

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old November 17th, 2003, 03:24 PM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just recreated all my tables and dropped all my queries and now the form works great!

You guys rock!!!
 
Old November 18th, 2003, 11:23 AM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh No! Can you believe I'm adding even MORE to this topic? :)

Here's a couple of pointers that I would recommend to keep in
mind when you're trying to do what you mentioned in the first panel.

 - Build your SQL in the AfterUpdate() for your option group if you're
   using option buttons - that way you KNOW that the refresh will
   take place at the moment that the button is selected.
 - Don't forget to do a requery on your combo box as soon as you
   rebuild the SQL and set the rowsource. Something like this:

Select Case Frm_InvUpd_Category.Value
  Case 1
    Me.Frm_InvUpd_Category.Value = 1
    Me.Cmb_InvUpd_Prod.RowSource = "SELECT Product.Product_Name " & _
                                     " FROM Product " & _
                                     " WHERE (((Product.Product_Category) = 'Long Range')) " & _
                                     " ORDER BY Product.Product_Name;"

  Case 2
    Me.Frm_InvUpd_Category.Value = 2
    Me.Cmb_InvUpd_Prod.RowSource = "SELECT Product.Product_Name " & _
                                   "FROM Product " & _
                                   "WHERE (((Product.Product_Category) = " 'Mid Range'")) " & _
                                   "ORDER BY Product.Product_Name;"

 Case 3
 Case 4
 Case 5

Me.Cmb_InvUpd_Prod.Requery

End Select


If you need to have the combo box's rowsource updated for an existing
record is displayed with a selected value for an option button, you can put the SQL build statements in a shared function within the form and call it from the AfterUpdate on the buttons and from the Form_Current(). Just be sure that the Form_Current() will only call the function if Me.NewRecord = False.

I'll be happy to clarify if any of this seems a little fuzzy or confusing.

Best Wishes to All,

Warren
:D





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Check Boxes/Option buttons/Option Group hewstone999 Access VBA 1 March 14th, 2008 07:25 AM
Error is using option buttons return value Subuana Beginning VB 6 2 May 1st, 2006 03:39 PM
updating text fields with option buttons redtechcoms Access VBA 7 February 2nd, 2006 03:14 PM
Using Option buttons NovieProgrammer Access VBA 1 April 2nd, 2005 09:16 PM
Is one (1) a 'length' for option buttons? John K. King Javascript How-To 2 November 7th, 2004 06:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.