473,406 Members | 2,220 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,406 software developers and data experts.

Command button to open a form meeting two WHERE conditions by Expression Builder

79 64KB
I want to use a command button in 1st form to open a 2nd form that meets two “WHERE” conditions.

One of the conditions is the customer number (ordh_cust_no) that resides in both forms. Then I want user to enter second condition, Item Code, to meet the condition in a text box (txt_ordd_stock_no) from the 2nd form.

When I use the command button wizard, the wizard created the following where statement in the Expression Builder.

Expand|Select|Wrap|Line Numbers
  1.  ="[ordh_cust_no]=" & "'" & [txt_ordh_cust_no] & "'" 
How do I make Access to ask for the manually entered 2nd condition so it display only records that meet both conditions?
Oct 24 '11 #1

✓ answered by NeoPa

Thank you Joe. That makes the situation clear :-)

It seems to me you have two main options :
  1. (Recommended) Add a new control on your first form to allow the operator to specify the value they want for [OrdD_Stock_No].
  2. Allow Access to prompt the operator every time.
A is recommended because the value persists after it's been used in the filter. Also, it looks so much more professional. Data prompts are generally considered to reflect a poorly implemented database.
  1. Assuming we have a control on your first form called [txtItemCode] and the Item Code is numeric, we would use code like :
    Expand|Select|Wrap|Line Numbers
    1. Dim strFilter As String
    2.  
    3. strFilter = "([OrdH_Cust_No]='" & Me.txt_OrdH_Cust_No & "') AND " & _
    4.             "([OrdD_Stock_No]=" & Me.txtItemCode & ")"
    5. Call DoCmd.OpenForm(FormName:="Your2ndForm", WhereCondition:=strFilter)
  2. Otherwise it would be more like :
    Expand|Select|Wrap|Line Numbers
    1. Dim strFilter As String
    2.  
    3. strFilter = "([OrdH_Cust_No]='" & Me.txt_OrdH_Cust_No & "') AND " & _
    4.             "([OrdD_Stock_No]=[Please enter Item Code])"
    5. Call DoCmd.OpenForm(FormName:="Your2ndForm", WhereCondition:=strFilter)
    Access will prompt for a value whenever it finds a reference (within []) that it can't resolve.

7 5055
NeoPa
32,556 Expert Mod 16PB
Within the code of the Command Button you will need a line to open the new form (Call DoCmd.OpenForm()). One of the parameters will be your filter string. For now we'll call that strFilter :
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.OpenForm(FormName:="Your2ndForm", _
  2.                     WhereCondition:=strFilter)
Before this is run you will need to prepare the value of strFilter correctly :
Expand|Select|Wrap|Line Numbers
  1. Dim strFilter As String
  2.  
  3. strFilter = "([OrdH_Cust_No]='" & Me.txt_OrdH_Cust_No & "') AND " & _
  4.             "(???)"
Unfortunately that's where your question let's us down as it doesn't make proper sense. You can't compare it against a TextBox control you haven't opened yet. We don't know what to compare with what here. You mention Item Code, but fail to explain if it's a field (If it is then it's in a different format from the other field mentioned) or what it is. Is it [Item Code], [ItemCode], [Item_Code] or none of the above? This sort of information should be included clearly in the question if you don't want to waste any of our, or your, time.

I've answered what I can, and that may be enough to get you going on this occasion, but I expect there'll be more questions coming (and you're welcome to post more) and it would be far better if you get those right first time, as it'll save lot's of time.
Oct 24 '11 #2
Joe Y
79 64KB
Sorry I did not make it clear in my question.

So far, I can make the command button to open the 2nd form to list all records matching current customer number on the 1st form. However, I need to filter further by specifying an item code.

The item code exists only in the table “DBA_ordd” (record source of the 2nd form). The data field for Item Code is “ordd_stock_no” that is displayed in the text box “txt_ordd_stock_no” in the 2nd form. The 1st form has no object that is tied to item code, so I cannot add the rest of Where condition after AND (or perhaps, I just don’t know how).

What I am imaging of the design is to have Access pop up a window asking user to enter Item Code as filter criteria then execute the VBA code in the open form Command as you described. How to do this in the VBA or in the Expression Builder?

Thanks for the helps.
Oct 25 '11 #3
NeoPa
32,556 Expert Mod 16PB
Thank you Joe. That makes the situation clear :-)

It seems to me you have two main options :
  1. (Recommended) Add a new control on your first form to allow the operator to specify the value they want for [OrdD_Stock_No].
  2. Allow Access to prompt the operator every time.
A is recommended because the value persists after it's been used in the filter. Also, it looks so much more professional. Data prompts are generally considered to reflect a poorly implemented database.
  1. Assuming we have a control on your first form called [txtItemCode] and the Item Code is numeric, we would use code like :
    Expand|Select|Wrap|Line Numbers
    1. Dim strFilter As String
    2.  
    3. strFilter = "([OrdH_Cust_No]='" & Me.txt_OrdH_Cust_No & "') AND " & _
    4.             "([OrdD_Stock_No]=" & Me.txtItemCode & ")"
    5. Call DoCmd.OpenForm(FormName:="Your2ndForm", WhereCondition:=strFilter)
  2. Otherwise it would be more like :
    Expand|Select|Wrap|Line Numbers
    1. Dim strFilter As String
    2.  
    3. strFilter = "([OrdH_Cust_No]='" & Me.txt_OrdH_Cust_No & "') AND " & _
    4.             "([OrdD_Stock_No]=[Please enter Item Code])"
    5. Call DoCmd.OpenForm(FormName:="Your2ndForm", WhereCondition:=strFilter)
    Access will prompt for a value whenever it finds a reference (within []) that it can't resolve.
Oct 25 '11 #4
Joe Y
79 64KB
Thanks NeoPa. The #2 option works. I added View:=acFormDS in order to open 2nd form as datasheet.

Now I am testing the #1 option. Instead of a command button and a text box, I am planning to place the option #1 codes in the After Update event of a Combo Box for convenient reason. User would click and choose desired item code. After update, the 2nd form opened with two filter criteria.

However, I am stuck with an error message 3464, data type mismatch in criteria expression.

The item code is a text field in the table. Does that require a different VBA code? Your message says assuming item code is a numeric, which make me think the code may need to be different.

Also, is there a instruction somewhere to guide how and the purposes to place quotation mark (“ or ‘) and bracket (() or [])? I am very confused with this. VBA is brand new to me.

Below is the code I have and the debug stopped at the line starting with Call.

Thanks.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmb_Price_Hist_AfterUpdate()
  3.  
  4.  
  5. Dim strFilter As String
  6.  
  7. strFilter = "([ordH_cust_no]='" & Me.txt_ordh_cust_no & "') AND " & _
  8.              "([ordd_stock_no]=" & Me.cmb_Price_Hist & ")"
  9.  
  10. Call DoCmd.OpenForm(FormName:="F_Ord_History", View:=acFormDS, WhereCondition:=strFilter)
  11.  
  12. End Sub
  13.  
Oct 25 '11 #5
Joe Y
79 64KB
After a few trial and error, the following codes worked.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmb_Price_Hist_AfterUpdate()
  2.  
  3.  Dim strFilter As String
  4.  
  5.  strFilter = "([ordh_cust_no]='" & Me.txt_ordh_cust_no & "') AND " & _
  6.  "([ordd_stock_no]='" & Me.cmb_Price_Hist & "')"
  7.  
  8. Call DoCmd.OpenForm(FormName:="F_Ord_History", View:=acFormDS, WhereCondition:=strFilter)
  9.  
  10. End Sub
  11.  
I was confused with quote and brackets signs and still is.

Thanks.
Oct 25 '11 #6
NeoPa
32,556 Expert Mod 16PB
Joe Y:
Also, is there a instruction somewhere to guide how and the purposes to place quotation mark (" or ') and bracket (() or [])?
Try Quotes (') and Double-Quotes (") - Where and When to use them for the first. Brackets ([]) identify an element of a reference. Parentheses (()) are used to ensure a section of code is interpreted before those othe sections around it.

Brackets are only required when without them the reference is ambiguous or wrong (Such as a control name with an embedded space in it, or one that uses a reserved word - [Name] is often used for fields and controls). SQL references often require brackets around the elements in more situations than those in VBA code. A qualified reference should only have brackets around the elements. Never around the full reference. EG. [Forms.frmMenu] is totally wrong and will always cause a failure. It could be [Forms].[frmMenu] though.

PS. Your working code matches the article I linked to above. That's exactly how I would have suggested to do it. Nice work.
PPS. ComboBoxes usually have the prefix cbo rather than cmb. It's up to you of course but I changed all my work a while back to reflect this (I started out using cmb too). The idea of using the ComboBox AfterUpdate event in place of a Command Button is another good one.
Oct 25 '11 #7
Joe Y
79 64KB
Thanks for clarification!
Oct 26 '11 #8

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

Similar topics

1
by: Grant Hammond | last post by:
I assume I'm not alone in my frustration that the expression builder that comes (in part) with Access XP that dosnt wrap text when you open it on an exisitng expression in a query or form. I's...
9
by: Melissa | last post by:
What is the code to delete a command button from a form? Can the code be run from the click event of the button to be deleted? Thanks! Melissa
2
by: Mike Turco | last post by:
I like using the expression builder for a lot of different things but it isn't always available when I want to use it, for example in the code window, or in all of the control properties. I am...
3
by: Nigel C | last post by:
My problem is best explained by way of tables and examples... I am using Access 2000 (in case this is relevant) and I have a table with the following fields... Field names ------------ Code ...
1
by: richardkreidl | last post by:
Two questions, first I'm using CR 10 and connecting to a Sybase database. I don't see a 'Add Command' button in the 'Database Expert' dialog. Second is how would I correctly code the syntax for...
3
emandel
by: emandel | last post by:
I am an armature access programmer trying my best to design my own database so your patience and detailed instructions is much appreciated. Here is my question: How do I get a command button (on...
2
by: buddyr | last post by:
Hello, I have a cmd button on form to search for serial number. I notice when I search for record-that if I don't start at the beginning of records I might not find the record. example: if I am...
4
by: Hiramicus | last post by:
Hi - I'm using Access 2003. I have an input form that I'm trying to link to multiple tables. I have a command button that updates all the tables with the ~40 textboxes which the user inputs the...
7
by: Bre035 | last post by:
Created a database that I want to be maintained and updated by the Department designated Admins. I have an user table that set specific individuals access to Admin and created admin form and...
0
by: Paul Wilson | last post by:
Access 2003 Sp2 – Pretty new to Access so please bear with me! I have created a simple database which has 4 tables with matching column names but info from different systems. I have merged some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.