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

vb case help

For simplicity sake, here are the basics of the query I am stumped
on...
Type: Select query
Name: ColorCells

Below is a partial list of the "Cases" for the code (I did not think
there was a need to list all 21)
If Identifier = "A" then color Green
If Identifier = "B" then color Blue
If Identifier = "C" then color Red
The final "case" would be a case else. In other words If Identifier
=
to anything besides the previous 20 items the color = clear
Can anyone please assist me with formalizing this code?
Your help is greatly appreciated. I am enrolled in a Visual Basic
class in 4 weeks, but need to complete this before then.

Mar 28 '07 #1
6 2489

Could you do this with another table? Said table would contain the
letter id and the colour name. I *think* a left join would match the
ones it can figure out and return nulls for the others. In which case
you could say in a query...IIF(IsNull([FieldSomething]),"Clear",
[ColourField]).
Maybe?

Mar 28 '07 #2
"storrboy" <st******@sympatico.cawrote
Could you do this with another table? Said table
would contain the letter id and the colour name. I
*think* a left join would match the ones it can
figure out and return nulls for the others. In which
case you could say in a query...IIF(IsNull([FieldSomething]),"Clear",
[ColourField]).
Maybe?
I agree that a separate Table, and a join on the "Identifier", would be
best. But, JIC you don't understand "left join", you create a Join by
clicking and dragging the Field to the corresponding Field in the Table to
be joined. If the Table is tblA and the color code/names are in tblB, then
you will click the Join line to highlight it, right-click and select Join
Properties from the dialog, then choose the option "All records from tblA
and only those that match from tblB."

But, if for some reason you feel compelled to NOT use a separate table, in a
standard module in your application, create a public

Function ColorName(ID As String) As String
Select Case ID
Case "A"
ColorName = "Green"
Case "B"
ColorName = "Blue"
Case "C"
ColorName = "Red"
Case Else
ColorName = "Clear"
End Select
End Function

Then in the Query, you have a Calculated Field. If I understand your post,
you want that Field to be called "ColorCells" so type in ColorCells: into
the Field, and follow with ColorName([ put in the name of the Field that
contains the Idenfier ]). As you didn't say, I've assumed you want to
return a text string with the color name; but, if you want to return a color
value, there are built-in constants for some, but not, I suspect for all 20,
so you'll have to determine the value for other than vbRed, vbBlue, vbGreen,
vbYellow, and the others with built-in constants. If I remember correctly,
color values are held in a LongInteger, so there would be some minor change
to the Function.

Larry Linson
Microsoft Access MVP
Mar 29 '07 #3
On Mar 28, 11:15 pm, "Larry Linson" <boun...@localhost.notwrote:
"storrboy" <storr...@sympatico.cawrote
Could you do this with another table? Said table
would contain the letter id and the colour name. I
*think* a left join would match the ones it can
figure out and return nulls for the others. In which
case you could say in a query...IIF(IsNull([FieldSomething]),"Clear",
[ColourField]).
Maybe?

I agree that a separate Table, and a join on the "Identifier", would be
best. But, JIC you don't understand "left join", you create a Join by
clicking and dragging the Field to the corresponding Field in the Table to
be joined. If the Table is tblA and the color code/names are in tblB, then
you will click the Join line to highlight it, right-click and select Join
Properties from the dialog, then choose the option "All records from tblA
and only those that match from tblB."

But, if for some reason you feel compelled to NOT use a separate table, in a
standard module in your application, create a public

Function ColorName(ID As String) As String
Select Case ID
Case "A"
ColorName = "Green"
Case "B"
ColorName = "Blue"
Case "C"
ColorName = "Red"
Case Else
ColorName = "Clear"
End Select
End Function

Then in the Query, you have a Calculated Field. If I understand your post,
you want that Field to be called "ColorCells" so type in ColorCells: into
the Field, and follow with ColorName([ put in the name of the Field that
contains the Idenfier ]). As you didn't say, I've assumed you want to
return a text string with the color name; but, if you want to return a color
value, there are built-in constants for some, but not, I suspect for all 20,
so you'll have to determine the value for other than vbRed, vbBlue, vbGreen,
vbYellow, and the others with built-in constants. If I remember correctly,
color values are held in a LongInteger, so there would be some minor change
to the Function.

Larry Linson
Microsoft Access MVP

Agreed that the separate table idea is best. However, rather than
create one's own function to do this you could use the VBA Switch()
function instead. Similar to Larry's example you might have a
calculated field in your query that looks like the following:

ColorCells:Switch([ColorID]="A","Green",[ColorID]="B","Blue",
[ColorID]="C","Red",IsNull([ColorID],Null,True,"Clear")

One advantage is that there is no need to create a separate function.
In addition, if the value of your ColorID field can ever be Null, the
Switch() function will return a Null whereas the ColorName function
above would crash. If you wanted to return "Clear" when ColorID is
Null, simply remove the ",IsNull([ColorID],Null" clause from your
calculated field.

Bruce

Mar 29 '07 #4
"Bruce" <de***************@gmail.comwrote
Agreed that the separate table idea is best. However,
rather than create one's own function to do this you
could use the VBA Switch() function instead. Similar
to Larry's example you might have a calculated field in
your query that looks like the following:

ColorCells:Switch([ColorID]="A","Green",[ColorID]="B","Blue",
[ColorID]="C","Red",IsNull([ColorID],Null,True,"Clear")
One advantage is that there is no need to create a separate function.

Frankly, I like breaking out the calculation, rather than having 20 items
plus the ELSE in a calculation in the Query. It's easier, at least for me,
to follow in indented code if you have to debug. But, that's a matter of
preference. And, if it were accessing a server DB, performance might
improve if you put it in a stored procedure.
In addition, if the value of your ColorID field
can ever be Null, the Switch() function will
return a Null whereas the ColorName function
above would crash.
Yep, and there's no error handling, either -- it is, after all, merely a
suggestion in a newsgroup, not intended to be a fully-developed application
or a library procedure for immediate insertion into your general library.
Null input can be handled, but, as I said, you are correct, I didn't handle
it in the newsgroup post.

Larry Linson
Microsoft Access MVP
Mar 30 '07 #5
On Mar 29, 7:26 pm, "Larry Linson" <boun...@localhost.notwrote:
"Bruce" <deluxeinformat...@gmail.comwrote
Agreed that the separate table idea is best. However,
rather than create one's own function to do this you
could use the VBA Switch() function instead. Similar
to Larry's example you might have a calculated field in
your query that looks like the following:
>
ColorCells:Switch([ColorID]="A","Green",[ColorID]="B","Blue",
[ColorID]="C","Red",IsNull([ColorID],Null,True,"Clear")
One advantage is that there is no need to create a separate function.

Frankly, I like breaking out the calculation, rather than having 20 items
plus the ELSE in a calculation in the Query. It's easier, at least for me,
to follow in indented code if you have to debug. But, that's a matter of
preference. And, if it were accessing a server DB, performance might
improve if you put it in a stored procedure.
In addition, if the value of your ColorID field
can ever be Null, the Switch() function will
return a Null whereas the ColorName function
above would crash.

Yep, and there's no error handling, either -- it is, after all, merely a
suggestion in a newsgroup, not intended to be a fully-developed application
or a library procedure for immediate insertion into your general library.
Null input can be handled, but, as I said, you are correct, I didn't handle
it in the newsgroup post.

Larry Linson
Microsoft Access MVP
The more I think about it, the more I think that for most situations
the idea of a separate function is the better one. It is more code
but it is probably easier to maintain in the long run. If the case
selection were any more complicated than these four or five cases
trying to maintain the Switch() function would probably be more
arduous than necessary.

Bruce

Mar 30 '07 #6
"Bruce" <de***************@gmail.comwrote
If the case selection were any more complicated
than these four or five cases trying to maintain
the Switch() function would probably be more
arduous than necessary.
If memory serves, somewhere in this thread, the original poster said twenty
colors plus the ELSE case... mine was just a few of those to illustrate. An
IIF with twenty options and a fail-safe ELSE would make for a "busy" Query
Field calculation.

Larry Linson
Microsoft Access MVP
Mar 31 '07 #7

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

Similar topics

20
by: Joey Martin | last post by:
I am scanning an HTML file. I need to gather certain data from areas that start with <SMALL> text. Let me show the code, then explain more. ---- Set fso =...
5
by: Ryan | last post by:
I'm struggling with a Case statement. The problem I has is with doing >= I can use any value in there, but need to check if it's greater or equal to 1. I'm sure I'm missing something but can't...
2
by: Tim Graichen | last post by:
Could somebody please help me out with this easy one? I have a five button toggle set up. Case 1-4 work fine, so I have ommitted the code from this post. I need help with Case 5. Case five...
4
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
1
by: esil | last post by:
Can anybody help me with this query? All other fields are correct except UsageStock field... select Sum(Case when ='7' then (++) else 0 end) AS ProjUsageClear, Sum(Case when ='7' then else 0...
5
by: bob | last post by:
Now this ought to be a simple matter. But nothing's simple in the Net world, I'm finding. In vb6 you could use "!" to force text to upper case in the format function. I've searched the vb.net...
5
by: Frederick Dean | last post by:
Hi,guys! I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I meet a weird case: bool Postorder::next() { switch (pc) case START: while (true) if (!child()) { pc = LEAF; return...
7
by: Adam | last post by:
Hello. I do most of my web dev work from a Mac (whose file system is case-insensitive.) I upload my web pages and other files onto a Unix-based host, which is case sensitive. When I look at the...
3
by: emalcolm_FLA | last post by:
Hello and Thanks in advance for any help. I have been tasked with rewriting a christmas assistance database using Access 2003. The old system used pre-assigned case numbers to identify...
2
by: Desitech | last post by:
I have a table entitled "Parts". In that table I have a field entitled "PartDoc" and four fields entitled "Product1", Product2", "Product3", and Product4". The datatype for these fields is Number...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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...

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.