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

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 51641
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*****@NObestfitsoftwareSPAM.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 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*****@NObestfitsoftwareSPAM.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*****@NObestfitsoftwareSPAM.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*****@NObestfitsoftwareSPAM.com.au> wrote in message
news:gl********************************@4ax.com...
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*****@NObestfitsoftwareSPAM.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.Value DoSomething will execute.
If the array element <> MeControlName.Value 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.Value 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(wcSearch) 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********************@newssvr21.news.prodigy .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********@bway.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
rk*@yabba.dabba.do.rochester.rr.mom (rkc) wrote in
<IK***************@twister.nyroc.rr.com>:
"David W. Fenton" <dX********@bway.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.


Ah.

Nor should anyone else, then.

BTW, can it be used with late binding successfully?

And do people who depend on it check that it's installed before
using it?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Nov 12 '05 #11
rkc

"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:94***************************@24.168.128.78.. .
rk*@yabba.dabba.do.rochester.rr.mom (rkc) wrote in
<IK***************@twister.nyroc.rr.com>:
"David W. Fenton" <dX********@bway.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.
It's part of the Windows Scripting Runtime library so you would
have no use for it.
Ah.

Nor should anyone else, then.

BTW, can it be used with late binding successfully?


You do not need to set a reference to the library.
CreateObject ("Scripting.Dictionary") works just fine.
And do people who depend on it check that it's installed before
using it?


Can't answer that one for obvious reasons..
Dictionary advantages over a Collection:

Speed increase.
..Key method is read/write
..Keys method returns an array of all keys.
..Exists method determines if a key exists.
..Items method returns an array of all items.
..Item method is read/write.


Nov 12 '05 #12
dX********@bway.net.invalid (David W. Fenton) wrote in
news:94***************************@24.168.128.78:
It's part of the Windows Scripting Runtime library so you would
have no use for it.
Ah.


Your decisions are your own.
Nor should anyone else, then.
Your opinions are your own, and I wish they'd remain your own.
BTW, can it be used with late binding successfully?
Of course.
And do people who depend on it check that it's installed before
using it?


Since I do not publish applications but instead use them in my own
corporate environment, I have control over that environment.
Nov 12 '05 #13
rk*@yabba.dabba.do.rochester.rr.mom (rkc) wrote in
<CG****************@twister.nyroc.rr.com>:

"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:94***************************@24.168.128.78. ..
rk*@yabba.dabba.do.rochester.rr.mom (rkc) wrote in
<IK***************@twister.nyroc.rr.com>:
>"David W. Fenton" <dX********@bway.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. >It's part of the Windows Scripting Runtime library so you would
>have no use for it.
Ah.

Nor should anyone else, then.

BTW, can it be used with late binding successfully?


You do not need to set a reference to the library.
CreateObject ("Scripting.Dictionary") works just fine.
And do people who depend on it check that it's installed before
using it?


Can't answer that one for obvious reasons..


Well, then you don't use the library, I take it?

If not, do you get paid for this kind of neglect of your duties?
Dictionary advantages over a Collection:

Speed increase.
.Key method is read/write
.Keys method returns an array of all keys.
.Exists method determines if a key exists.
.Items method returns an array of all items.
.Item method is read/write.


Why are you comparing to a collection instead of an array?

And isn't there a performance hit for using an outside library for
this kind of data structure?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #14
rp******@NOSPAM.imtek.com.invalid (Ross Presser) wrote in
<Xn**********************@129.250.170.91>:
dX********@bway.net.invalid (David W. Fenton) wrote in
news:94***************************@24.168.128.7 8:

And do people who depend on it check that it's installed before
using it?


Since I do not publish applications but instead use them in my own
corporate environment, I have control over that environment.


Every desktop?

And you assume that every desktop is running properly?

I guess you probably have no error checking in any of your code,
since you've already accounted for everything that could go wrong,
right?

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

"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:94***************************@24.168.128.90.. .
If not, do you get paid for this kind of neglect of your duties?

Sorry. Not interested.
Nov 12 '05 #16

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

Similar topics

3
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...
2
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 =...
11
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...
2
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...
1
by: Katit | last post by:
Is it possible to find object in array with specific property value? Just like dataset filter? Thanks!
1
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...
0
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...
2
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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.