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.

Using VBA to define variable for query criteria

Hi

Basicaly i have a combo box with two options "ug" and "dh". this is the VBA code i have so far:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cbosample_AfterUpdate()
  2.  
  3. Dim SampleType As String
  4.  
  5. If Me.cbosample = "dh" Then
  6.     SampleType = "Like dh"
  7. Else
  8.     SampleType = "Not Like dh"
  9. End If
  10.  
  11. End Sub

So if "dh" is selected in the combo box then the query criteria will be equivlaent to Like "dh" and if "ug is selected in the combo box then the query criteria will be equivalent to Not Like "dh"

Below is the code is used to reference the variable in the query. In the query criteria i put GetSampleType()


Expand|Select|Wrap|Line Numbers
  1. Public Sub SetSampleType(Value As String)
  2.  
  3. SampleType = Value
  4.  
  5. End Sub
  6.  
  7. Public Function GetSampleType()
  8.  
  9. GetSampleType = SampleType
  10.  
  11. End Function

Thanks
Apr 30 '13 #1

✓ answered by Rabbit

You could use an iif or switch to change the comparison value so that each is grouped under the correct category.

7 2411
Seth Schrock
2,965 Expert 2GB
I'm not sure what your question is. Can you let us know what you need help with as well as what isn't working and the results (ie. error message along with the EXACT wording and number, or invalid values).

Also, please use the [CODE/] button when posting code.
Apr 30 '13 #2
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code.

A query does not see "Like 'dh'" as SQL. It sees it as a literal string. You will have to edit the query definition itself if you want to do it that way.

But I don't understand the need for any code at all. You can just do this directly in the query:
Expand|Select|Wrap|Line Numbers
  1. fieldName = Forms!formName!controlName
Apr 30 '13 #3
Ok, i am not getting any error messages, but the query criteria does not seem to be enforced as the query table does not return any values.

Let me know if this is not clear and I'll try again.
Apr 30 '13 #4
Ok, thanks.

The need for the code is because the "ug" value in the combo box has to encompass several different values which would be easiest described as all values except for "dh". ie in my table i have values "dh", "fc", "b", "st" and i want to group all values except "dh" under the "ug" combo box value.

I have been editing the query box manually for quite a while to get what i needed, but i am new to VBA and just trying to speed things up a bit using combo boxes and VBA code.

let me know if any of this doesn't make sense

Thanks.
Apr 30 '13 #5
Rabbit
12,516 Expert Mod 8TB
You could use an iif or switch to change the comparison value so that each is grouped under the correct category.
Apr 30 '13 #6
Thanks Rabbit, got it sorted, just calculated a new query field
Expand|Select|Wrap|Line Numbers
  1. SampleType: IIf([collar]![type]="dh","dh","ug")
then used the value from the combo box as criteria.
Expand|Select|Wrap|Line Numbers
  1. [forms]![f_create_surpac_str]![cbosample].[value]
Apr 30 '13 #7
Rabbit
12,516 Expert Mod 8TB
Glad you got it working!
May 1 '13 #8

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

Similar topics

3
by: Mike Conmackie | last post by:
Greetings, I am trying to create a node in the output tree using a variable. Here are some fragments that I hope will explain the problem better. <xsl:stylesheet...
9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
6
by: Access Newbie | last post by:
I'm using Access 2000 and I'm trying to create a pass-through query to append all the data from a local table to a remote table. I'm using the SQL query editor in MS Access to create the query (I...
6
by: msigwald | last post by:
The following line of code works, however, since my professor is a real purist of c, I would like to know if this code is valid (is it good code or a piece of crap?): #define DMP_FILE argv ...
8
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it...
1
by: Epson Barnett | last post by:
Hi, I'm new to C# and I'd like to be able to reference a field of an object using a variable instead of literal text. In the PHP scripting language, you can create a variable: $var = "name";...
2
by: davidmarks | last post by:
I am trying to use a combo box to set a variable, and then refresh the form using that variable when the combo box is changed. I've used the following method before, but must be forgetting something...
7
by: John Smith | last post by:
Hello, I have a simple question, I have a vb.net form with several buttons. If I store the name of a button in a variable.. Dim TheName as string TheName = ...
4
by: Polaris431 | last post by:
If I have a method like this: private void DoSomething() { try { return true; } catch{} finally
8
by: jj | last post by:
Trying to understand the manual regarding variables to solve the following. I'm working with Drupal CMS and have a loop to retrieve values that are normally fetched by using this variable:...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.