two ComboBox questions

hello - I have some code attached to several of my ComboBoxes that works 
fine, but I'm not if it is 'best practice' coding.  I'd really like any 
suggestions for improvement:

a) If I have to walk through the items in a ComboBox, i do this...

     n = 0
     Do While Not IsNull(Me!cboGroup.ItemData(n))
           ... code here ....
           n = n + 1
     Loop

Is this the best way to walk through these items? Is there a better way 
involving 'Do While Not rs.EOF'?

b)  Say I have three ComboBoxes and I only want to proceed
     if they all show values...

        If Len(cboA) * Len(cboB) * Len(cboC) > 0 Then .....

Again, is this the accepted way to do this sort of thing? I want to make 
sure i don't run into any unexpected outcomes.

regards, Terrance




0
Utf
3/28/2010 12:44:01 AM
access.formscoding 7493 articles. 0 followers. Follow

7 Replies
813 Views

Similar Articles

[PageSpeed] 1

The question boils down to this: What is your objective?  Or... Why are you 
interested in "walking" through a combo box?  Once we know your objective, we 
can better assist you with an answer for "best practice."

"terry w" wrote:

> hello - I have some code attached to several of my ComboBoxes that works 
> fine, but I'm not if it is 'best practice' coding.  I'd really like any 
> suggestions for improvement:
> 
> a) If I have to walk through the items in a ComboBox, i do this...
> 
>      n = 0
>      Do While Not IsNull(Me!cboGroup.ItemData(n))
>            ... code here ....
>            n = n + 1
>      Loop
> 
> Is this the best way to walk through these items? Is there a better way 
> involving 'Do While Not rs.EOF'?
> 
> b)  Say I have three ComboBoxes and I only want to proceed
>      if they all show values...
> 
>         If Len(cboA) * Len(cboB) * Len(cboC) > 0 Then .....
> 
> Again, is this the accepted way to do this sort of thing? I want to make 
> sure i don't run into any unexpected outcomes.
> 
> regards, Terrance
> 
> 
> 
> 
0
Utf
3/28/2010 1:31:01 AM
hello Devlin - with respect, I don't think that is the question.  I'm just 
working through various scenarios to learn better coding.  One of the things 
I would like to know how to do is to examine each item in a combo box.  For 
example, there may be reports I want to print, one for each item in a 
particular combo box.  -  Terrance

"DevlinM" wrote:

> The question boils down to this: What is your objective?  Or... Why are you 
> interested in "walking" through a combo box?  Once we know your objective, we 
> can better assist you with an answer for "best practice."
> 
> "terry w" wrote:
> 
> > hello - I have some code attached to several of my ComboBoxes that works 
> > fine, but I'm not if it is 'best practice' coding.  I'd really like any 
> > suggestions for improvement:
> > 
> > a) If I have to walk through the items in a ComboBox, i do this...
> > 
> >      n = 0
> >      Do While Not IsNull(Me!cboGroup.ItemData(n))
> >            ... code here ....
> >            n = n + 1
> >      Loop
> > 
> > Is this the best way to walk through these items? Is there a better way 
> > involving 'Do While Not rs.EOF'?
> > 
> > b)  Say I have three ComboBoxes and I only want to proceed
> >      if they all show values...
> > 
> >         If Len(cboA) * Len(cboB) * Len(cboC) > 0 Then .....
> > 
> > Again, is this the accepted way to do this sort of thing? I want to make 
> > sure i don't run into any unexpected outcomes.
> > 
> > regards, Terrance
> > 
> > 
> > 
> > 
0
Utf
3/28/2010 1:43:01 AM
Answering your second question.  Your test is fine.  Just be sure that if 
using the function Len, each of your controls returns a string.

"terry w" wrote:

> hello - I have some code attached to several of my ComboBoxes that works 
> fine, but I'm not if it is 'best practice' coding.  I'd really like any 
> suggestions for improvement:
> 
> a) If I have to walk through the items in a ComboBox, i do this...
> 
>      n = 0
>      Do While Not IsNull(Me!cboGroup.ItemData(n))
>            ... code here ....
>            n = n + 1
>      Loop
> 
> Is this the best way to walk through these items? Is there a better way 
> involving 'Do While Not rs.EOF'?
> 
> b)  Say I have three ComboBoxes and I only want to proceed
>      if they all show values...
> 
>         If Len(cboA) * Len(cboB) * Len(cboC) > 0 Then .....
> 
> Again, is this the accepted way to do this sort of thing? I want to make 
> sure i don't run into any unexpected outcomes.
> 
> regards, Terrance
> 
> 
> 
> 
0
Utf
3/28/2010 1:46:01 AM
OK, well there are a number of ways to determine the contents of a combo or 
list box.  ItemData is used to return the bound column of either control.  
Looking at your scenario here is what you'd probably want to do.

Dim i as integer

For i = 0 to Me.Cbo_A.ListCount - 1
    Debug.Print Me.Cbo_A.ItemData(i)
Next i

"terry w" wrote:

> hello Devlin - with respect, I don't think that is the question.  I'm just 
> working through various scenarios to learn better coding.  One of the things 
> I would like to know how to do is to examine each item in a combo box.  For 
> example, there may be reports I want to print, one for each item in a 
> particular combo box.  -  Terrance
> 
> "DevlinM" wrote:
> 
> > The question boils down to this: What is your objective?  Or... Why are you 
> > interested in "walking" through a combo box?  Once we know your objective, we 
> > can better assist you with an answer for "best practice."
> > 
> > "terry w" wrote:
> > 
> > > hello - I have some code attached to several of my ComboBoxes that works 
> > > fine, but I'm not if it is 'best practice' coding.  I'd really like any 
> > > suggestions for improvement:
> > > 
> > > a) If I have to walk through the items in a ComboBox, i do this...
> > > 
> > >      n = 0
> > >      Do While Not IsNull(Me!cboGroup.ItemData(n))
> > >            ... code here ....
> > >            n = n + 1
> > >      Loop
> > > 
> > > Is this the best way to walk through these items? Is there a better way 
> > > involving 'Do While Not rs.EOF'?
> > > 
> > > b)  Say I have three ComboBoxes and I only want to proceed
> > >      if they all show values...
> > > 
> > >         If Len(cboA) * Len(cboB) * Len(cboC) > 0 Then .....
> > > 
> > > Again, is this the accepted way to do this sort of thing? I want to make 
> > > sure i don't run into any unexpected outcomes.
> > > 
> > > regards, Terrance
> > > 
> > > 
> > > 
> > > 
0
Utf
3/28/2010 1:58:01 AM
Iterating through the combo itself is just one way of looking at the contents 
of the control.  You can also look at the control's columns.  For instance

Dim i as integer

For i = 0 to Me.Cbo_A.ListCount - 1
    Debug.print Me.Cbo_A.Column(0) & " " & Me.Cbo_A.Column(1)
Next i

You can also look at the controls row source.  You are most likely going to 
be using a query, therefore, you can open the query to look at items.  If you 
are concerned about nulls in your list, you might do something with them 
before you reach the point of displaying them in your control.  For instance, 
you might set criteria in query to WHERE Table_Name.Attribute_Name Is Not 
Null.  This will limit your list non-null values.  Or you might use a query 
or other method to elimate nulls from your recordset.

Once again, knowing your objective is an important as to how to answer your 
question.  If you just want to know how to iterate through a combo box, then 
your answer is here.

Cheers!
"terry w" wrote:

> hello Devlin - with respect, I don't think that is the question.  I'm just 
> working through various scenarios to learn better coding.  One of the things 
> I would like to know how to do is to examine each item in a combo box.  For 
> example, there may be reports I want to print, one for each item in a 
> particular combo box.  -  Terrance
> 
> "DevlinM" wrote:
> 
> > The question boils down to this: What is your objective?  Or... Why are you 
> > interested in "walking" through a combo box?  Once we know your objective, we 
> > can better assist you with an answer for "best practice."
> > 
> > "terry w" wrote:
> > 
> > > hello - I have some code attached to several of my ComboBoxes that works 
> > > fine, but I'm not if it is 'best practice' coding.  I'd really like any 
> > > suggestions for improvement:
> > > 
> > > a) If I have to walk through the items in a ComboBox, i do this...
> > > 
> > >      n = 0
> > >      Do While Not IsNull(Me!cboGroup.ItemData(n))
> > >            ... code here ....
> > >            n = n + 1
> > >      Loop
> > > 
> > > Is this the best way to walk through these items? Is there a better way 
> > > involving 'Do While Not rs.EOF'?
> > > 
> > > b)  Say I have three ComboBoxes and I only want to proceed
> > >      if they all show values...
> > > 
> > >         If Len(cboA) * Len(cboB) * Len(cboC) > 0 Then .....
> > > 
> > > Again, is this the accepted way to do this sort of thing? I want to make 
> > > sure i don't run into any unexpected outcomes.
> > > 
> > > regards, Terrance
> > > 
> > > 
> > > 
> > > 
0
Utf
3/28/2010 2:09:01 AM
hey Devlin - thanks for you help!  -  Terrance

"DevlinM" wrote:

> Answering your second question.  Your test is fine.  Just be sure that if 
> using the function Len, each of your controls returns a string.
> 
> "terry w" wrote:
> 
> > hello - I have some code attached to several of my ComboBoxes that works 
> > fine, but I'm not if it is 'best practice' coding.  I'd really like any 
> > suggestions for improvement:
> > 
> > a) If I have to walk through the items in a ComboBox, i do this...
> > 
> >      n = 0
> >      Do While Not IsNull(Me!cboGroup.ItemData(n))
> >            ... code here ....
> >            n = n + 1
> >      Loop
> > 
> > Is this the best way to walk through these items? Is there a better way 
> > involving 'Do While Not rs.EOF'?
> > 
> > b)  Say I have three ComboBoxes and I only want to proceed
> >      if they all show values...
> > 
> >         If Len(cboA) * Len(cboB) * Len(cboC) > 0 Then .....
> > 
> > Again, is this the accepted way to do this sort of thing? I want to make 
> > sure i don't run into any unexpected outcomes.
> > 
> > regards, Terrance
> > 
> > 
> > 
> > 
0
Utf
3/28/2010 3:06:01 AM
terry w wrote:
>hello - I have some code attached to several of my ComboBoxes that works 
>fine, but I'm not if it is 'best practice' coding.  I'd really like any 
>suggestions for improvement:
>
>a) If I have to walk through the items in a ComboBox, i do this...
>
>     n = 0
>     Do While Not IsNull(Me!cboGroup.ItemData(n))
>           ... code here ....
>           n = n + 1
>     Loop
>
>Is this the best way to walk through these items? Is there a better way 
>involving 'Do While Not rs.EOF'?
>
>b)  Say I have three ComboBoxes and I only want to proceed
>     if they all show values...
>
>        If Len(cboA) * Len(cboB) * Len(cboC) > 0 Then .....
>
>Again, is this the accepted way to do this sort of thing? I want to make 
>sure i don't run into any unexpected outcomes.

Another way for a) in A2002+

With Me.cboGroup.Recordset
	Do Until .EOF
		If Not IsNull(!somefield) Then
			...
		End If
		.MoveNext
	Loop
End With

b)  Won't work if any of those field's contain Null.  In
general, it is unusual to have a valid reason to set a
field's AllowZeroLength property to Yes so the fields should
contain Null instead of a ZLS where your code would be
acceptable.

Depending on how obscure you want to make your code, you
could use:
	If Len((cboA + cboB + cboC) & vbNullString) > 0 Then

But we're talking about best practices where clarity
sometimes wins out over brevity.  I would at least think
about using:
	If Len(Nz(cboA,"")) > 0 _
		And Len(Nz(cboB,"")) > 0 _
		And Len(Nz(cboC,"")) > 0 Then

-- 
Marsh
MVP [MS Access]
0
Marshall
3/28/2010 2:09:31 PM
Reply:

Similar Artilces:

QUESTION ABOUT DISABLING A BUTTON ON A FORM
I have 2 checkbox on a form and a button. The button is disabled when the form is loaded and enabled when either of the checkboxes is checked. What I want to do is set things up such that when the checkboxes are both unchecked, the button is disabled. I tried the On Current event of the form, see below, but that didn't work. Any ideas? Thanks Private Sub Form_Current() If Me.chkFilterBy.Value = False And Me.chkTimeRange.Value = False Then Me.cmdFilter.Enabled = True Else Me.cmdFilter.Enabled = False End If End Sub Private Sub Form_Load() M...

Excel : Saving and Mailing Question
Hi, A few questions I hope you can help me with (please) : 1. Is it possible to save a particular worksheet under a set name t one destination and the rest of the workbook to another, through macro/click of a button? 2. Is it possible to do the same but through email? Thanks Andre -- Message posted from http://www.ExcelForum.com Ron de Bruin has lots of code examples at: http://www.rondebruin.nl/sendmail.htm He also has an addin that may do almost what you want. (on that same web page) "ajw150 <" wrote: > > Hi, > > A few questions I hope you can help me wi...

synchiing two computers?
Is there a way to synch Outlook on two computers? I end up with some email on my laptop and some on my desktop - it would be nice if the two could be synched so that I could have all of my email on both computers. Take a look here, it may help: http://www.slipstick.com/outlook/sync.htm -- Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. All unsolicited mail sent to my personal account will be deleted without reading. After furious head scratching, Bill Andersen asked: | Is there a way to synch Outlook on two computers? | I end up with some...

EN_UPDATE usability question
What would a reasonable upper limit in time spent responding to the EN_UPDATE notification? If its too long the UI would be lagged and that would be an annoyance to the end user. Is this in any official MS style guide? Personally I would assume maybe .25 seconds however this is for an official document so I need to be able to back up a value with some officialness such as MS recommendation. River, You might want to do a Google search, this is a well-researched topic, and I imagine you'll find quite a few studies. For web usability, there has been lots of work, at least, and as...

modification questions
Does anyone know for sure if setting up modifications in the isv.config and web.config files before migrating data from siebel into crm will cause any problems? Every time I talk to someone at Microsoft they ask me if I've done any modifications....I need to make some modifications when I go live before I run the migration scripts. I just don't want it to cause any problems. Thanks, Jen Hi. I can't think of any reason why modifying these files would impact on data migration but logically I would have thought you would want to migrate the data first and then make the cha...

Compare two cells in two different files and return answer
I am attempting to compare two text cells and if they both exist, I want to bring in a "yes" answer. If they don't, bring in a "no" answer. Here's an example of text. File 1 Col A Col B BRATTA ZPB_COMMON AHERNC ZPB_COMMON GENCABH ZPB_COMMON File 2 Col A Col B BRATTA ZPB_COMMON AHERNC ZPB_COMMON GENCABH So, what I want to do is say in File 1 take cells A1 and B1 compare them to File 2 and search Col A and Col B and if there is an exact mat...

Exmerge Question #5
Two scenarios: 1. Seems like this should work no problem. If a user mailbox was deleted using Exchange Tasks (but not the user) and I was able to restore the mail store to a RSG, shouldn't I use Exmerge to pull the data out of the RSG and put it all back into the original account (of course assuming that I've created an empty mailbox for the user)? It seems for whatever reason Exmerge isn't able to see the database after the mailbox has been removed and a new one created on the account. 2. Should the above situation work if it's a whole new user created where the o...

Pivot table for two different data variables
I have the following table Name type Cost overhead amy C1 23 12 bob C2 129 17 cat C2 36 45 doo C2 100 32 I owuld like to create a pivot table that looks similar to: the standard pivot table. (Please ignore type for now.) name Data Total amy Sum of overhead 200 Sum of cost 12 bob Sum of overhead 130 Sum of cost 23 cat Sum of overhead 90 Sum of cost 19 doo Sum of overhead 87 Sum of cost 34 Total Sum of overhead 507 Total Sum of cost 88 But with overall total (overhead + cost) for ...

Mouse event question
Does anybody know if is there an event triggered in the CWnd class when a mouse "enters" that class, I mean, for example, when the mouse moves out from a view, or returns to a mainframe from the desktop? So far, the only workaround I've found is to use the OnSetCursor event to signal the position of the mouse, but this is not the optimal, since is triggered each time the mouse moves, making the application slow, and, as I said, I only need a signal that tells me when the mouse leaves one window and enter to another. Thanks a lot for your attention. OnMouseMove is what you nee...

Filtering a Number of Comboboxes Before Their Forms are Opened(?)
I'm trying to set up a "switch" in my db such that another office can get an updated front-end from me, and the first time they open the mdb, they can select their office, causing several cbos on various forms to change their rowsource (or change a criteria in their SQL) so that they don't have to weed thru so many choices. I've got a startup form with a cbo for selecting their office, and I've made it sticky, like it should be. But now, how to change what they see in the cbos on the other forms? -- Thanks, croy croy wrote: >I'm trying to set up a "s...

if- then question
Is it possible to compare two rows and then based on the value, assign a character to occupy and adjacent column in the same row. For example, I want to compare all of the numbers in column B, with th numbers in column D, if B is greater than D, I want to put the lette "W" in the adjacent column (in the same row). If B is less than D, want to put the letter "L" in the adjacent column. Is this possible -- Message posted from http://www.ExcelForum.com Hi enter the following in the adjacent row (lets say in column E, cell E1) =IF(B1>D1,"W",IF(B1<D1,"...

Few questions about VB
1. How can I do number for example 812 from 812,033217 ? I try to do like this but it did not work Dim first as double Dim second as intiger firs = 812,033217 second = first first = second but still firs = 812,033217 :confused: 2. How can I open not .xls file from VB code wrote in Excel file I want to open Solid Works file "example.SLDPRT" in Solid Works o course thx for any suggestio -- Message posted from http://www.ExcelForum.com Hi 1. one way: use CInt. e.g. second = CInt(first) >-----Original Message----- >1. How can I do number for example 812 from 812,03321...

Two IF Statements with two answers
In cell J9 I need a formula that would do the following: If G9="Yes" then F9, If G9="No" then -f9 Note: G9 has a drop down list with Yes, No answers. If there are no other alternatives that yes and no =IF(G9="Yes",F9,-F9) otherwise =IF(G9="Yes",F9,IF(G9="No",-F9,"") Regards, Peo Sjoblom "heater" wrote: > In cell J9 I need a formula that would do the following: > > If G9="Yes" then F9, If G9="No" then -f9 > > Note: G9 has a drop down list with Yes, No answers. > =IF(G9=&qu...

A question for the guro
How do I select a node that has both namespace and is prefixed? IE: <a:b> (and also in the default namespace!) Ayende Rahien wrote: > How do I select a node that has both namespace and is prefixed? > IE: > <a:b> (and also in the default namespace!) How exactly depends on the API you are using, but in general such selection requires additional information about which namespcae URI "a" prefix is bound to. In MSXML it's done via SelectionNamespaces property, in .NET - via XmlNamespaceManager class. -- Oleg Tkachenko XML Insider http://www.tkachenko.com/...

question about CStringT::Tokenize
Hi, I try to split a string "First,,Third" with ",". I suppose the result is "First", "", and "Third". But why it is "First","Third".(A empty string has been skipped.) BTW, how about ",First,Second,Third" and "First,Second,Third," If CString::Tokenize cannot support it directly, any other way can do it easily. //code CString str( "First,,Third" ); CString resToken; int curPos= 0; CString strSplitter( "," ); resToken= str.Tokenize(strSplitter,curPos); while (resToken != &...

5.5 to 2003 migration question
I currently have Win2003 Active Directory in a single domain running. Exchange 5.5 on a Windows NT box is the last remaining WinNT box on the network. If I put a test Win2003 server and do all the forestpreps, domainpreps and run the ADC and install a test copy of Exchange 2003 and I use one test mailbox on the 5.5 to move it, will this prevent me from moving the rest of the mailboxes and database to another Exchange setup that will be permanent later? Um...it depends by what you mean when you say 'another Exchange setup'. You should approach the Forest prep and domain prep...

access 97/2000 question
If an access 97 back end database containing only tables is accessed by an access 2000 front end, is there a danger or possibility that the tables will be no longer accessable by access 97 front ends? Hi Alan, If the 2000 front end just contains linked tables pointing to the 97 back end, no worries. But if someone using 2000 opens and converts the back end mdb itself, you're obviously hosed. On Wed, 25 Feb 2004 20:11:00 -1000, "Alan" <please@thanks.com> wrote: >If an access 97 back end database containing only tables is accessed by an >access 2000 front end, is...

COUNTIF (?) on two criteria
I have a spreadsheet that logs work activities and records team members' completion of project units. Column B records team member names. Column C has project unit status - 'pending', 'beta' or 'completed'. It's easy to set up a formula to count how many units are allocated to each team member - =COUNTIF(B1:B1000,"Joe"). But what I need to do is set up formulas to count how many units Joe has completed, or has pending or at beta, and this means assessing data in both columns - which COUNTIF won't do. This doesn't strike me as an especially...

viewing questions
Is there a way to sort the questions by date. I cannot figure out how the questions are ordered. It's not by date, it's not by alpha, it's not by popular - there appears to be no rhyme or reason to it. If you access the forum directly from the server http://www.gmayor.com/MSNews.htm you can sort them in any way you like. -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< >...

A CString question
Hi to all, Given the following member function: CString MyClass::MakeString(BYTE *pBuf, int len) { CString str; ... return str; } Where: - 'pBuf' is a buffer that contains letters (a-z). It does not contain a terminating null character. - 'len' is the number of letters in 'pBuf'. What would be the best way to return a CString object that contains the same letters in 'pBuf'. I appreciate any suggestions. Thanks, Geo You can create a new CString using the constructor with CString(pointer,len) so why not just do: CString str(pBuf,len); You ...

Two axis
Positioning the Vertical axes: I have a chart where there are two axes. The first implementation produced the two scales on the right side, one inside, the other outside the chart area. Unusual, but luckily it fits well the chart. I tried to duplicate the feat with another dataset (similar in structure) and did not even get the second scale to show right, left or center (data plotted with two different scales allright). Where is the info to control these functionalities? Tx, Pierre On Wed, 29 Oct 2003 17:48:13 +0100, pl.carry <pl.carry@wanadoo.fr> wrote= : > Positioning the Vertic...

TreeView XML Question
Hi Guys, I have different pages in ASP.NET.And i have a treeview that populates XML that i use as the MENU. Is it possible to have one asp.net page but populating different XML to the mENU? Any ideas? ...

plot two groups of numbers on one series of xy coordinates.
how to create a plot from separate x and y values. i.e. y values do not correspond to the same set of x values. example x1= 5, y1=10; x2= 8, y2=15 I can read your question 2 ways; A) you data looks like this x y 5 10 8 15 etc. Select the data and make an XY chart B) you have two sets of data x1 y1 5 10 10 12 17 15 21 20 and x2 y2 3 8 7 14 12 19 18 24 Make an XY chart of the first data series Select all the data (including headers) for second set, and use Copy Click the chart to activate it Use Edit | Paste Special .... indicate New Ser...

two variables
I have a query that asks for collcode, collcode2 and booth. On any given day an employee may be collcode or collcode2. I would like to know how many times a certain emp has been paired with another employee. So how do I ask if an employee has been collcode/collcode2 and who the other person was? I hope this question makes sense. Thanks for any help Query one: SELECT CollCode, CollCode2, Booth FROM YourTable UNION ALL SELECT CollCode2, CollCode, Booth FROM YourTable Query Two SELECT CollCode, CollCode2, Count(CollCode) AS TIMESPAIRED FROM QueryOne GROUP BY CollCode, CollCode2 IF y...

Find two PCI instance but load Init() one time
Hi all, I'm developing a stream interface driver for my PCI device under WinCE 6.0 and this driver works very well currently. Now, I got a problem when testing two the same PCI devices with my stream driver. The registry can find two PCI instances (i.e. PCI\Instance\mydrv1, PCI\ Instance\mydrv2) but system only call my XXX_init() one time. Does anyone know what the problem is? The following information is my registry setting: ================================================================== [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\PCI\Template\mydrv] "Dll"="myd...