473,626 Members | 3,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA array basics - how to test for value?

What I'd like to do is create an array of values and test for existence of
those values.
Here's the non-working code I'm having trouble with:

Dim wcSearch(4 To 7) As Integer

If Me.Value = wcSearch Then
Do Something
Else
Do Something else
End If

Compile Error: Type mismatch <<=== error when try to compile

Anyone know how to do this with a VBA array?

Thanks in advance!
Nov 12 '05 #1
15 51666
On Tue, 30 Sep 2003 23:18:00 GMT, "deko" <dj****@hotmail .com> wrote:
What I'd like to do is create an array of values and test for existence of
those values.
Here's the non-working code I'm having trouble with:

Dim wcSearch(4 To 7) As Integer

If Me.Value = wcSearch Then
Do Something
Else
Do Something else
End If

Compile Error: Type mismatch <<=== error when try to compile

Anyone know how to do this with a VBA array?

Thanks in advance!


Dim wcSearch(4 To 7) As Integer
Dim i As Integer

For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.Value = wcSearch(i) Then
Do Something
Exit For
Else
Do Something else
End If
Next i
Wayne Gillespie
Gosford NSW Australia
Nov 12 '05 #2
Thanks for the reply. That code seems to work okay, but I've just
discovered I need change the contents of the array to something like:

Dim wcSearch(1, 2, 3, 8, 9) As Integer

But the line:

For i = LBound(wcSearch ) To UBound(wcSearch )

does not appear to work now.

How to remedy?

Thanks again!!

"Wayne Gillespie" <be*****@NObest fitsoftwareSPAM .com.au> wrote in message
news:b8******** *************** *********@4ax.c om...
On Tue, 30 Sep 2003 23:18:00 GMT, "deko" <dj****@hotmail .com> wrote:
What I'd like to do is create an array of values and test for existence ofthose values.
Here's the non-working code I'm having trouble with:

Dim wcSearch(4 To 7) As Integer

If Me.Value = wcSearch Then
Do Something
Else
Do Something else
End If

Compile Error: Type mismatch <<=== error when try to compile

Anyone know how to do this with a VBA array?

Thanks in advance!


Dim wcSearch(4 To 7) As Integer
Dim i As Integer

For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.Value = wcSearch(i) Then
Do Something
Exit For
Else
Do Something else
End If
Next i
Wayne Gillespie
Gosford NSW Australia

Nov 12 '05 #3
On Tue, 30 Sep 2003 23:28:55 GMT in comp.databases. ms-access, Wayne
Gillespie <be*****@NObest fitsoftwareSPAM .com.au> wrote:
On Tue, 30 Sep 2003 23:18:00 GMT, "deko" <dj****@hotmail .com> wrote:
What I'd like to do is create an array of values and test for existence of
those values.
Here's the non-working code I'm having trouble with:

Dim wcSearch(4 To 7) As Integer

If Me.Value = wcSearch Then
Do Something
Else
Do Something else
End If

Compile Error: Type mismatch <<=== error when try to compile

Anyone know how to do this with a VBA array?

Thanks in advance!


Dim wcSearch(4 To 7) As Integer
Dim i As Integer

For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.Value = wcSearch(i) Then
Do Something
Exit For
Else
Do Something else
End If
Next i


Not sure you'd want to "Do something else" at that point, perhaps...

For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.Value = wcSearch(i) Then
Do Something
Exit For
End If
Next i
If i > UBound(wcSearch ) Then
' exited For loop naturally
' so didn't find one
Do Something Else
End If

--
A)bort, R)etry, I)nfluence with large hammer.
Nov 12 '05 #4
On Tue, 30 Sep 2003 23:45:02 GMT, "deko" <dj****@hotmail .com> wrote:
Try this

Dim wcSearch As Variant
Dim i As Integer

wcSearch = Array(1, 2, 3, 8, 9)
For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.ControlName. Value = wcSearch(i) Then
'Do Something
Exit For
Next i

'Trevor's suggestion
If i > UBound(wcSearch ) Then
' exited For loop naturally
' so didn't find one
Do Something Else
End If

Thanks for the reply. That code seems to work okay, but I've just
discovered I need change the contents of the array to something like:

Dim wcSearch(1, 2, 3, 8, 9) As Integer

But the line:

For i = LBound(wcSearch ) To UBound(wcSearch )

does not appear to work now.

How to remedy?

Thanks again!!

"Wayne Gillespie" <be*****@NObest fitsoftwareSPAM .com.au> wrote in message
news:b8******* *************** **********@4ax. com...
On Tue, 30 Sep 2003 23:18:00 GMT, "deko" <dj****@hotmail .com> wrote:
>What I'd like to do is create an array of values and test for existenceof >those values.
>Here's the non-working code I'm having trouble with:
>
>Dim wcSearch(4 To 7) As Integer
>
>If Me.Value = wcSearch Then
> Do Something
>Else
> Do Something else
>End If
>
>Compile Error: Type mismatch <<=== error when try to compile
>
>Anyone know how to do this with a VBA array?
>
>Thanks in advance!
>


Dim wcSearch(4 To 7) As Integer
Dim i As Integer

For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.Value = wcSearch(i) Then
Do Something
Exit For
Else
Do Something else
End If
Next i
Wayne Gillespie
Gosford NSW Australia


Wayne Gillespie
Gosford NSW Australia
Nov 12 '05 #5
works great! But do I need the "Exit For" statement?

This seems to work okay:

Dim wcSearch As Variant
Dim i As Integer

wcSearch = Array(1, 2, 3, 8, 9)
For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.ControlName. Value = wcSearch(i) Then
DoSomething
Else
DoSomethingElse
Next i

Why do I need to Exit the For loop?
am I missing something?

Thanks Wayne and Trevor! I appreciate the help very much!

"Wayne Gillespie" <be*****@NObest fitsoftwareSPAM .com.au> wrote in message
news:gl******** *************** *********@4ax.c om...
On Tue, 30 Sep 2003 23:45:02 GMT, "deko" <dj****@hotmail .com> wrote:
Try this

Dim wcSearch As Variant
Dim i As Integer

wcSearch = Array(1, 2, 3, 8, 9)
For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.ControlName. Value = wcSearch(i) Then
'Do Something
Exit For
Next i

'Trevor's suggestion
If i > UBound(wcSearch ) Then
' exited For loop naturally
' so didn't find one
Do Something Else
End If

Thanks for the reply. That code seems to work okay, but I've just
discovered I need change the contents of the array to something like:

Dim wcSearch(1, 2, 3, 8, 9) As Integer

But the line:

For i = LBound(wcSearch ) To UBound(wcSearch )

does not appear to work now.

How to remedy?

Thanks again!!

"Wayne Gillespie" <be*****@NObest fitsoftwareSPAM .com.au> wrote in message
news:b8******* *************** **********@4ax. com...
On Tue, 30 Sep 2003 23:18:00 GMT, "deko" <dj****@hotmail .com> wrote:

>What I'd like to do is create an array of values and test for
existenceof
>those values.
>Here's the non-working code I'm having trouble with:
>
>Dim wcSearch(4 To 7) As Integer
>
>If Me.Value = wcSearch Then
> Do Something
>Else
> Do Something else
>End If
>
>Compile Error: Type mismatch <<=== error when try to compile
>
>Anyone know how to do this with a VBA array?
>
>Thanks in advance!
>

Dim wcSearch(4 To 7) As Integer
Dim i As Integer

For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.Value = wcSearch(i) Then
Do Something
Exit For
Else
Do Something else
End If
Next i
Wayne Gillespie
Gosford NSW Australia


Wayne Gillespie
Gosford NSW Australia

Nov 12 '05 #6
On Wed, 01 Oct 2003 00:29:30 GMT, "deko" <dj****@hotmail .com> wrote:
works great! But do I need the "Exit For" statement?

This seems to work okay:

Dim wcSearch As Variant
Dim i As Integer

wcSearch = Array(1, 2, 3, 8, 9)
For i = LBound(wcSearch ) To UBound(wcSearch )
If Me.ControlName. Value = wcSearch(i) Then
DoSomething
Else
DoSomethingElse
Next i

Why do I need to Exit the For loop?
am I missing something?


The code you have above will execute code for every element in the array.
If the array element = MeControlName.V alue DoSomething will execute.
If the array element <> MeControlName.V alue DoSomethingElse will execute.
If this is how you want the code to execute then the Exit For should not be used.

If however you want DoSomethingElse to ONLY execute if MeControlName.V alue is not found in the array at all, then there is no need to continue looping
the array after a match is found (unless there could be additional matches within the array). The Exit For stops the loop when a match is found. The
value of the variable i will be <=UBound(wcSear ch) so the check Trevor added will not be executed. If you do not use Exit For the loop will continue
to the end and the value of i will be UBound(wcSearch )+1. Therefore Trevor's check will ALWAYS execute whether you want it to or not.

Your design will determine whether to use Exit For or not.
Wayne Gillespie
Gosford NSW Australia
Nov 12 '05 #7
"deko" <dj****@hotmail .com> wrote in
news:Ia******** ************@ne wssvr21.news.pr odigy.com:
What I'd like to do is create an array of values and test for
existence of those values.
Here's the non-working code I'm having trouble with:

Dim wcSearch(4 To 7) As Integer

If Me.Value = wcSearch Then
Do Something
Else
Do Something else
End If

Compile Error: Type mismatch <<=== error when try to compile

Anyone know how to do this with a VBA array?

Thanks in advance!


Might be barking up the wrong tree, but whenever I see "test for existence of those values" I think
Dictionary, not array.

Dim wcSearch as Dictionary
wcSearch.add 1,"z"
wcSearch.add 3,"z"
wcSearch.add 5,"z"
wcSearch.add 7,"z"

if wcSearch.Exists (Me.Value) Then
do something
else
do something else
end if
--
Ross Presser -- rpresser AT imtek DOT com
"... VB is essentially the modern equivalent of vulgar Latin in 13th Centurary Europe. Understand it, and
you can travel to places you never heard of and still understand some people." -- Alex K. Angelopoulos
Nov 12 '05 #8
rp******@NOSPAM .imtek.com.invalid (Ross Presser) wrote in
<Xn************ **********@129. 250.170.100>:
Might be barking up the wrong tree, but whenever I see "test for
existence of those values" I think Dictionary, not array.

Dim wcSearch as Dictionary
wcSearch.add 1,"z"
wcSearch.add 3,"z"
wcSearch.add 5,"z"
wcSearch.add 7,"z"

if wcSearch.Exists (Me.Value) Then
do something
else
do something else
end if


I don't see any "Dictionary " data or object type in Access97 or
Access2K.

Is that VB only?

Is it a custom type that can be implemented in some other way?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #9
rkc

"David W. Fenton" <dX********@bwa y.net.invalid> wrote in message
news:94******** *************** ****@24.168.128 .78...
I don't see any "Dictionary " data or object type in Access97 or
Access2K.

Is that VB only?

Is it a custom type that can be implemented in some other way?


It's part of the Windows Scripting Runtime library so you would
have no use for it.
Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1734
by: Emmett Power | last post by:
Hi, I have a form with a table with two fields SelectionID and Experience. I am posting the data to a database using an array function. I have set out the code below. The problem I am having is where the Experience field is blank I get an Microsoft OLE DB Provider for ODBC Drivers error '80040e21'. It makes sense that a null value would cause the function problems.
2
7106
by: deko | last post by:
Can I return the index number of an array if all I have is the element? For example, if I want to index the alphabet, I can put the letters in the array: Dim varLtr As Variant varLtr = Array("A", "B", "C", "D", "E", "F", "G", _ "H", "I", "J", "K", "L", "M", "N", _ "O", "P", "Q", "R", "S", "T", "U", _ "V", "W", "X", "Y", "Z")
11
39452
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not rst.EOF i = i + 1 If rst!Something = Then astrMyArray(i) = rst!Something
2
4669
by: Rohit Malaviya | last post by:
Hi, I was asked this question at a recent interview... it goes like this: Suppose I want to manipulate an array inside a function (say, sort)... but I do not want the changes to be reflected in the calling function. How do I do it? Now folks, do not tell me that I should copy the array to temp array... using a 'for' loop... thats not the solution.
1
2185
by: Katit | last post by:
Is it possible to find object in array with specific property value? Just like dataset filter? Thanks!
1
1999
by: starffly | last post by:
In my program, the calculated value is supposed to be no more than the constants named MAXINT,otherwise, the overflow error will be reported, however, I cannot test if the value exceeds MAXINT within the integer scope smaller than MAXINT, so I want to seek a solution to test the excess without comparing the value with MAXINT. Please let me know if you have a good idea. THX.
0
1963
by: melb | last post by:
I haven't done much work with XSL translations before and i'm stuggling to apply some code to a list of items. The when code works if it is not a list but I think I need to replace the ITEMCOST with something else but i'm not sure what. can someone help. I am using this code as the <xsl:value-of select="format-number does not work with teh translator I'm using. I am basically trying to set the resulting value as two decimal places, for...
2
5053
by: Steve Richter | last post by:
Is it possible to write a generic method which returns true or false, depending on if the array contains a value? Is there already a framework method that provides this functionality? the following code does not compile. Error is: Operator == cannot be applied to operands of type T and T. public static bool Contains<T>(T InValues, T InPattern) {
4
2369
by: akshay01 | last post by:
Hi All, I am using the following code in which i am creating some textboxes and and as the for loop continues the name of the textboxes will also be unique for all the textboxes. now i want to store the textbox values into some array and want to pass the array as an value of hidden variable <% int i =0; String arr_TextValues = new String; String arr_ChkValues = new String;
0
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8641
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8510
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.