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

VBA connected to Checkboxes doesn't work - always proceeds only first option

Hello, please could you advise how to connect checkboxes with VBA to adjust excel files.

I have excel files for 11 entities (countries).
I need to do adjustment sometimes on each of them or sometimes only chose which file should be adjusted.

I created checkboxes with labels:
Expand|Select|Wrap|Line Numbers
  1.  x  Select All
  2.  x  country1
  3.  x  country2
  4. ......
  5. ......
  6.  x country11
I need Access VBA for open, save and close related excel files. The issue is that it always opens, saves and closes only file for first country and then states "debug".
Files names are the same as label captions connected with checkbox and ".xls" at the end.

Here is what I tried:

Expand|Select|Wrap|Line Numbers
  1. Dim LoopIndex As Integer
  2. Set xlapp = CreateObject("Excel.Application")
  3. xlapp.DisplayAlerts = False
  4. filepath = "path to the file"
  5.  
  6. For LoopIndex = 1 To 11
  7. Select Case Me.Controls("CB" & LoopIndex)
  8. Case Me.Controls("CB" & LoopIndex) = True
  9. xlapp.workbooks.Open filepath & Me.Controls("LB" & LoopIndex).Caption & ".xls"
  10. xlapp.Visible = True
  11. xlapp.ActiveWorkbook.Save
  12. xlapp.ActiveWorkbook.Close
  13. Set xlapp = Nothing
  14. End Select
  15.  
  16. Next LoopIndex
Thanks for your advice in advance.
May 22 '14 #1
6 1491
jimatqsi
1,271 Expert 1GB
Katerina8457,
Welcome to Bytes.com Please be sure to use code tags around any code you post by clicking the [CODE/] button and placing your code between the tags.

The Debug message means you have hit an error in the code. You should add some error handling to your code. Here's a generalized sample, from Allen Browne http://allenbrowne.com/ser-23a.html
Expand|Select|Wrap|Line Numbers
  1. 1 Sub|Function SomeName()
  2. 2     On Error GoTo Err_SomeName          ' Initialize error handling.
  3. 3     ' Code to do something here.
  4. 4 Exit_SomeName:                          ' Label to resume after error.
  5. 5     Exit Sub|Function                   ' Exit before error handler.
  6. 6 Err_SomeName:                           ' Label to jump to on error.
  7. 7     MsgBox Err.Number & Err.Description ' Place error handling here.
  8. 8     Resume Exit_SomeName                ' Pick up again and quit.
  9. 9 End Sub|Function
I don't understand why it works at all if this is your actual code. Do you know what a file path is? It should be something like "C:\foldername\filename.xls". Perhaps you are only using "path to the file" here to represent some path that is actually in your code.

It is not really appropriate to use Select Case to test True/False values. Normally you would just say
Expand|Select|Wrap|Line Numbers
  1. if Me.Controls("CB" & LoopIndex) =True then
  2.    do stuff here
  3. end if
  4.  
since there's no need to test any other cases.

With the error handling you may get additional info about your error from the Msgbox.

Jim
May 22 '14 #2
zmbd
5,501 Expert Mod 4TB
Katerina8457 - please check your bytes.com inbox.
May 22 '14 #3
NeoPa
32,556 Expert Mod 16PB
Jim makes a good point about using If when there is only a single test, but spoils it somewhat by comparing a CheckBox (Boolean) to True. That is both unnecessary and potentially misleading in some cases (though not here). If you already have a boolean value then there is no need to compare it to anything in order to produce a boolean result.
May 23 '14 #4
jimatqsi
1,271 Expert 1GB
NP, I always considered that a matter of form, not substance. In this instance, I chose specifically to spell it out like that, in order to be crystal clear to someone who might be new to the language.

Please describe a case where that is potentially misleading. Is it the use of True/False vernacular vs Yes/No vs Checked/Unchecked ?

Thanks,
Jim
May 23 '14 #5
zmbd
5,501 Expert Mod 4TB
True/False vernacular vs Yes/No vs Checked/Unchecked
True = -1 or 1 depending on flavor
False = 0

Yes IFF== vbYes = 6
No IFF== vbNo = 7

But note in the query table, these are nothing more than display formatting and the record field can be -1,0,1,null depending on how the properties are setup and if VBA is involved

control check
initial state unbound = null
initial state bound no record entry in field = null (maybe)
toggled once so that checked = -1
toggled again so that unchecked = 0
can be set in vba to = -1,0,1,null

null can, but does not always, branch to the false side in a logic test. Found this out the hard way with a Mac translation of the VBA code. I was depending on the default null value to test to the false side of the branch - OPPS, was testing as true and giving the user DBA rights - Now I explicitly set or test for all such controls to desired state starting with isNull()

so... perhaps not as straight forward as one would like.
May 23 '14 #6
NeoPa
32,556 Expert Mod 16PB
Hi Jim.

The other, more common, confusion is that TRUE = -1 (in VBA and most languages with sense). This is because all bits are set. Boolean values are stored as signed integers (Integer in VBA) so they are never an exact match for a single bit boolean. FALSE = 0.

A side-effect of this is that there are a very number of Integer values which are neither. These are nevertheless treated as True by the compiler. By which I mean that any non-zero value causes the True branch to be taken.

This is fine, until people try to treat the value as a proper boolean (A variable that can have only two possible states.), which it is not.

Consider a value of X=32.
Expand|Select|Wrap|Line Numbers
  1. If X Then ...
This resolves to True. The ... part is executed as you'd expect. However, :
Expand|Select|Wrap|Line Numbers
  1. If X = True Then ...
This resolves to False. Numerically, 32 <> -1.

The only reliable way to compare values to produce a compatible boolean result is to compare them to False :
Expand|Select|Wrap|Line Numbers
  1. If X <> False Then ...
This always produces a result consistent with using X directly.

NB. While I applaud the idea of making the code easier to understand, in this case I believe that such a use will actually help people to keep hold of their flawed understanding of how things work. It only makes sense if you assume that booleans must be the result of a comparison. However, this is not the case. Booleans are so much more flexible than that. It's a real shame that a reliable implementation of the boolean concept was never provided for VBA. You're certainly not the first to fall over this one, and I doubt you'll be the last.
May 25 '14 #7

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

Similar topics

3
by: amith shetty | last post by:
I am using CDONTS to send mail to the user from my ASP application. However, the code doenst works all the time. I am able to get mails but nothing happnes most of the time. The code works maybe...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
1
by: Station Media | last post by:
Hi, here my problem, i use stored procedure in ACCESS database(latest version), and i would like to use this procedure but it doesnt work, do you know why ? Procedure: PARAMETERS MyField Text...
3
by: Bruno Paquette | last post by:
I have installed visual studio .net and now i started to write the first few examples from the 70-315 book by Kalani. It looks like all server side code that i put between <% %> doesnt process...
2
by: effendi | last post by:
Hi I tested the following function in Safari and it doesnt work. This is tested fine in IE. function processOutcome(){ mainDatabase=document.forms.AssessDatabase.value var...
0
by: Juna | last post by:
I have been working in vs2003, but now started to work in vs2005 but the problem, I have simple web application not website, which work i mean open in browser when we press F5 or run the...
4
by: libsfan01 | last post by:
in firefox the following alert does not get created from my function: ch = container1.childNodes(0).id; alert(ch) can anyone tell why this might not work in firefox but does in safari? ...
3
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it...
9
by: AGP | last post by:
I've been scratching my head for weeks to understand why some code doesnt work for me. here is what i have: dim sVal as string = "13.2401516" dim x as double x = sVal debug.writeline ( x)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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: 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...

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.