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

Some help with Do__Loop and Arrays

Hi everyone!
I have a form with 6 checkboxes (getting their values from a table/query)
When the form loads, I would like the buttons right of the checkboxes to "react" to the values in the chekcboxes.
For example: If "Value1" is 0, then disable "EDIT" button and enable "ADD" button Else enable "Edit" button and disable "ADD" button.
Here is a screenshot:



I read some help files and wrote this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3. Dim Edit As String
  4. Dim Add As String
  5. Dim Value As String
  6.  
  7. Edit = "cmd_edit1"
  8. Add = "cmd_add1"
  9. Value = "Value1"
  10.  
  11. If Me(Value) = 0 Then
  12. Me(Edit).Enabled = False
  13. Me(Add).Enabled = True
  14. Else
  15. Me(Edit).Enabled = True
  16. Me(Add).Enabled = False
  17. End If
The code above works as expected, but I don't think copy/pasting for the other five checkboxes and buttons is the right approach (altough it works).
I should probably use Array and store variables there and then use a Do Loop.
I figured out how to declare Arrays, but I get lost, when it comes to looping the above code for every value in the array.

Here is the array example:

Expand|Select|Wrap|Line Numbers
  1. Dim Edit(6) As String
  2. Dim Add(6) As String
  3. Dim Value(6) As String
  4.  
  5. Edit(1) = "cmd_edit1"
  6. Edit(2) = "cmd_edit2"
  7. Edit(3) = "cmd_edit3"
  8. Edit(4) = "cmd_edit4"
  9. Edit(5) = "cmd_edit5"
  10. Edit(6) = "cmd_edit6"
  11.  
  12. Add(1) = "cmd_add1"
  13. Add(2) = "cmd_add2"
  14. Add(3) = "cmd_add3"
  15. Add(4) = "cmd_add4"
  16. Add(5) = "cmd_add5"
  17. Add(6) = "cmd_add6"
  18.  
  19. Value(1) = "Value1"
  20. Value(2) = "Value2"
  21. Value(3) = "Value3"
  22. Value(4) = "Value4"
  23. Value(5) = "Value5"
  24. Value(6) = "Value6"
Any tips, or even examples of how you would solve this are more then welcome :)

Friderik
Attached Images
File Type: png example_form.PNG (17.0 KB, 558 views)
Apr 6 '11 #1

✓ answered by gershwyn

The loop is an excellent idea, but given that your controls all have similar names, with the only difference being the number at the end, I'm not sure it's worth it to store the values in an array. I think the simplest way to accomplish what you describe is like this:
Expand|Select|Wrap|Line Numbers
  1. Public Function ToggleButtons()
  2.   For i = 1 To 6
  3.     CheckValue = Nz(Form.Controls("Value" & i).Value, 0)
  4.     Form.Controls("cmdEdit" & i).Enabled = (CheckValue = 0)
  5.     Form.Controls("cmdAdd" & i).Enabled = (CheckValue <> 0)
  6.   Next
  7. End Function
  8.  
First we check the value of each checkbox (converting a null value to a zero, so an uninitialized checkbox is treated the same as an unchecked one.) The edit button is enabled if the checkbox is unchecked. The add button is enabled otherwise. Then I'd set it up so that the function is called as the form's OnCurrent event and from the OnAfterUpdate event of each checkbox.

5 2657
gershwyn
122 100+
The loop is an excellent idea, but given that your controls all have similar names, with the only difference being the number at the end, I'm not sure it's worth it to store the values in an array. I think the simplest way to accomplish what you describe is like this:
Expand|Select|Wrap|Line Numbers
  1. Public Function ToggleButtons()
  2.   For i = 1 To 6
  3.     CheckValue = Nz(Form.Controls("Value" & i).Value, 0)
  4.     Form.Controls("cmdEdit" & i).Enabled = (CheckValue = 0)
  5.     Form.Controls("cmdAdd" & i).Enabled = (CheckValue <> 0)
  6.   Next
  7. End Function
  8.  
First we check the value of each checkbox (converting a null value to a zero, so an uninitialized checkbox is treated the same as an unchecked one.) The edit button is enabled if the checkbox is unchecked. The add button is enabled otherwise. Then I'd set it up so that the function is called as the form's OnCurrent event and from the OnAfterUpdate event of each checkbox.
Apr 6 '11 #2
Hi, thanks for your fast reply.
I should mention, that the controls have different names, I just renamed them for this example. Like:
Expand|Select|Wrap|Line Numbers
  1. cmd_scheme_name_edit
  2. cmd_scheme_name_add
  3. ...
Also, the checkboxes are locked, they are there just to display data.
Apr 6 '11 #3
NeoPa
32,556 Expert Mod 16PB
It's unfortunate that you changed the details of the question that way Friderik. From the care you used to formulate your question though, it looks like it was a simple oversight, and you get brownie-points for the question anyway. Particularly unfortunate as Gershwyn has provided a particularly clever solution for what the problem was.

As an alternative that doesn't rely on the controls being named sequentially, you could try using the For Each ctl In Me.Controls construct instead. You would need the design to include something that identified which controls were logically linked. As we have little info as to their correct names it's hard for me to suggest an approach that fits your situation, but if there isn't something already available then you could redesign the form to ensure the linked controls are identifiable somehow.

I mentioned the question was asked well (excepting the unfortunate slip-up with the names), it also indicates good sound thinking on your part even to ask such a question, so good for you :-)
Apr 7 '11 #4
Hi, thanks for the tipps and the critique.

I solved my problem first with names of the conrols as varaibles in an array. The more I looked at my solution, the more I appreciated gershwyn's approach.

Long story short. gershwyn's way is clearer and simpler. So I renamed my controls, created a public function and all is well on my form :)

I will paste my application of gershwyn idea on monday, to clarifiy things.
Apr 8 '11 #5
NeoPa
32,556 Expert Mod 16PB
Excellent. A sensible approach and a good idea to post your eventual solution when you have it too :-)
Apr 8 '11 #6

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

Similar topics

3
by: nkparimi | last post by:
Hi, I'm facing issues with using associative arrays in non-IE browsers. In particular, I have code like the following: var IDToAddress = new Array(); IDToAddress = 'apple tree lane, mars';...
2
by: Tor Hovland | last post by:
I have the following procedure: TYPE testarray is table of int index by binary_integer; PROCEDURE testfast(par1 in testarray) is begin FORALL i IN par1.FIRST..par1.LAST insert into dummy...
4
by: sonjaa | last post by:
Hi last week I posted a problem with running out of memory when changing values in NumPy arrays. Since then I have tried many different approaches and work-arounds but to no avail. I was...
3
by: chrisjob012 | last post by:
hi guys... need some help....!! i am working on a form that looks like a calender....!!... in that form i have added a group box....!! during runtime 30 labels are inserted in it to look like a ...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
1
by: Slain | last post by:
I am a beginner and have some confusion with respect to pointers and strings. It seems that the pointers with dealing with integer arrays behave differently, as opposed to strings. Can some one...
6
by: Emiurgo | last post by:
Hi there to everyone! I've got a problem which may be interesting to some of you, and I'd be very grateful if there is someone who can give me some advice (or maybe redirect me to some other place)....
9
by: andreyvul | last post by:
I'm trying to do the following: typedef struct { char *bar; char **baz; } foo; const foo fop = { { "foo", { "bar", "baz", "bax" } }, { "goo", { "car", "cdr", "cfr" } }
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...

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.