How do I disable the change events - vba

Currently I have several worksheets and plenty of values that are input or equal something. However with so many worksheets "talking" to each other, this appears to be causing issues. this occurs directly when a field is changed or button is pushed as I expect it to...
but it also happens indirectly when i'm capturing the data and writing it back to the userform with something like Form.textbox.value = range("A1") triggering the change event for the textbox.
Can someone comment if this is a code structure issue? Is there a best practice for what pieces of code should be separated?
I tried the application.enableevents = False but this does not seem to work with the user form the way i would expect. I found the link below and it seems to describe my issue to a tee but I'm not understanding what they mean by putting a public value in the "userform's module" the rest of disabling it and reenabling it makes sense
Link:
http://www.cpearson.com/excel/SuppressChangeInForms.htm
Can someone dumb this down for me or give a better option? Is this common?
Thanks,
Code after adding the public variable
Module where the information causes change event to trigger

Related

Can Access ensure a new form record displays all fields?

I have a database where I don't want some fields showing depending on data in other fields. I'm still new to VBA, having learnt how to do things I need via the internet (there's not much call for it in my job so like to try it out on side projects) for the things I need and have managed to create some code that hides certain fields that aren't needed, depending on what's been entered in another field and that works okay, if not perfectly (I'd like it to only work on the current record and not all of them at once but will worry about that later). My problem is, if I'm entering information onto a record and any of those fields become invisible exactly as I would want them to, then if I have more records to complete and load a new record, those hidden fields are also hidden on the blank record before any data has been entered and I want each new record to show all fields from the outset.
Another thing I've noticed is that if I close the database, next time I go into it the hidden fields have unhidden themselves again so I know I'm missing something important.
Here's a screenshot of a bit of the code where I want 2 other fields (What_reason and Date_sent_to_new_owning_School) to be visible depending on whether the answer in the current field after update is "Standard" or "Non-standard":
I'm sorry if this is really entry-level stuff but I AM entry level and trying to learn. This bit does work, albeit not perfectly as I'd like it to only work on the record I'm in at the time, and not go through and hide that field in all the other records at once (which it's doing).
I've searched everywhere but can't find the answer and although I've tried, I'm nowhere near good enough at VBA to try and use common sense to work it out. Is this something that can be done? I'm okay with computers generally and with Access too but I'm aware there's an awful lot I don't know and this is why I'm trying to do new things and learn stuff that I've not used before. I have tried all day to get this to work but am admitting defeat and am hoping somebody here will be able to help me. I'll probably need 'idiot level' advice if that's possible, I know my limitations. :)
Do you know how to use the Event tab in the Property Sheet? You can set all of your fields to [field].Visible = True on either: On Current, On Load, or On Open
Screenshot of the Property Sheet and for the field that determines the visibility of all of the other fields; you can use the Event: After Update so that way when you click/tab away from that field, it'll make those changes for you!
Property setting affects ALL instances of control. Control will be visible/not visible for all records depending on conditions of current record. Therefore, dynamically hiding controls on form set in Continuous or Datasheet will NOT give the desired result of
only work on the current record and not all of them at once
Db is not going to 'remember' dynamic setting - code needs to be executed when form opens and/or navigating records - so it is needed in OnCurrent event as well as control's AfterUpdate.
Conditional Formatting can dynamically enable/disable textbox/combobox by record although control is still visible.

Background macro/subroutine which is not shown in the Call Stack

I have a workbook with lots of sheets and several macros. When I enter VBA and try to write a new Sub into ThisWorkbook module, I see:
"This will reset your project, proceed anyway?"
assuming, that some project is currently running.
If I press Ctrl + L right after opening the file to check the Call Stack, it just shows nothing.
I did non run any macro myself and there's not a macro, which would handle any event (as far as I checked all sheets and modules in the project) except a little sub for saving event:
Workbook_BeforeSave (ByVal SaveAsUI As Boolean, Cancel As Boolean)
but this one AFAIK should be activated only before saving, thanks to Captain Obvious.
Another mysterious thing with this book is abnormally slow filtering for structured tables, which may be cured by turning off event handler:
Application.EnableEvents = False
Since both these facts are related to Events, I guess they might be somehow connected to each other.
Updated to include comments below.
Well, the problem still exists and I appreciate any idea that may help to locate this pesky macro running totally hidden.
Hm. Anyone with any ideas?
One way to reproduce the behavior that you describe is as follows:
1) Have a public variable which is initialized in Workbook_Open()
2) Have the option Notify Before State Loss enabled (under Tools/Options/General)
In this case when you first open the workbook and try to create a sub you see the warning about resetting the project even though no macro is currently running.
If this is the case, a simple fix (if it still bothers you) is to disable Notify Before State Loss.
On the other hand, it seems that your project has more general problems. VBA projects can become inexplicably corrupt and this might be the case here. A fix which sometimes works is to export all modules, userforms, etc., delete them, and then reimport them. Rob Bovey (a highly respected VBA guru) has written an add-in called Code Cleaner to automate the process. I haven't used it personally but it might be worth a try.

Need help on excel autocomplete function for drop-down list

I'm trying to add an auto-complete function to some of the boxes on the first sheet that I have for entering data on an excel worksheet, but I can't figure out how so. Here's the link for the excel document. https://drive.google.com/file/d/0B2ksoDGxry1tR2JGNnhoSEZuYU0/view?usp=sharing
I have read some of the articles here but I didn't really get how to apply them to my work.
There is a very handy guide for doing just that, but you need to rely on a mixture of VBA and form controls, specifically comboboxes. The initial method was outlined in a slightly revamped guide by Contextures.
This guide was later expanded into, what I felt, was a slightly easier and more robust method dubbed "the magical floating activeX control".
I hope you have luck with those two - but knowledge of VBA will help you get the most out of it. You have to customize their setup to match your data structures. I use this method to produce 'autocomplete' forms for users in my organization. So far, my one extension has been to make an 'on/off' button for this code, because it's method runs constantly, meaning that you lose the ability to do UNDO in excel while the lookup/autocomplete code is available to the user. I strongly advise setting up a button/feature that allows a user to disable it when not in use.
For enable/disable, here is the very-rough code that will do the job to ensure the code doesn't execute when you don't want it to. This disables the 'catch' that watches for the intial value change in the script from the links above.
If Application.EnableEvents = False Then
Application.EnableEvents = True
btn_Enable.ForeColor = 0
Else
Application.EnableEvents = False
btn_Enable.ForeColor = 35653
End If

Design mode automatically running?

I'm working within Visio 2010 and I have been using my event handlers such as
Private Sub Document_DocumentOpened(ByVal Doc As IVDocument)
'Methods being called...
End Sub
For some odd reason these will no longer work when the document is opened. Nor any other of my event handlers. It happened all of a sudden and I'm not quite sure why. I noticed that it seems that Design Mode seems to be highlighted upon loading every load, even when I turn it off and save the document.
So this leads me to believe that Design mode is automatically coming on when I load the document (but I have no such code) thus preventing my event handlers to stop working. But my event handlers don't work even when I turn off Design Mode.
Please help if you have any insight.
I was able to figure out the issue. I used
Application.EventsEnabled = False
without ever turning it back to true after my code ran. I read to always turn it back on but it slipped through the cracks and before I knew it, I deleted the nonworking code along with the Application.EventsEnabled portion. So, if you ever turn the Events Handler off, be sure to turn it back on!
Thanks to anyone that was attempting to help me! Sorry for the vague question but I didn't have much else to go off of, I had just forgotten I turned Events to false earlier.

VBA Status Bar

I am working on a Word VBA macro app for 80 or so users. The office has high staff turnover, so training suffers, and so one of the self imposed requirements for this project is comprehensive, friendly documentation. However, to supplement this, and to save newbies having to open up a 100 page document when they want to try something new, I want a status bar on every userform (there are five) that provides contextual help. I find tooltips annoying.
I don't have a lot of experience, so I was wanting to
Essentially, I have a file containing every status string. (This is currently a text file, but I was wondering if I should use a spreadsheet or csv for ease of editing by other staff in future.) Every control has a MouseMove event which refers to a function: getStatus(cID) that opens the file, grabs the line and displays it in the status label. It also grabs a few parameters from the same line in the file, such as whether the label is clickable (to link to a page in the help file), and what colour the label should be.
So a few questions really:
Will the application be slow if a userform is constantly referring to a file? It feels fine to me, but I've been in it far too long, and I'm the only user accessing that file. There will be 80 constantly accessing it.
Is MouseMove over a control the best way? Should I instead use co-ordinates?
Most importantly (in terms of me having to do as little work as possible) is there some way to do this so that I do not have to have a MouseMove event on every single control? I have a good few hundred or so controls, each with their own identifier (well, not yet, but they will if this is the only way to do it). Maybe when the form loads I could load ALL the possible status lines so they're ready for whenever the control is moused over. But then, maybe the loading time is negligible?
Appreciate any ideas or thoughts - especially if VBA already has a whole range of functions to do this already and I'm just trying to reinvent the wheel. I can't use the application status bar, because the user rarely sees the application itself.
Thanks!
EDIT:
It is for both data entry, clicking around and a bit of document generation.
It is a controlled environment so macro security issues aren't a big concern for me - and if something goes wrong it's someone else's fault or problem :)
Is this data entry app or do they just click stuff? Because often the field with focus is different to the item the mouse is hovering over, this can cause a lot of confusion.
Constantly reading from a file is a huge waste of time and resources - it is much better to load them only once into an array or collection when the form is loaded.
On MouseMouse event is better than coordinates because you can move things around without worrying. It's a lot of code but you should be able to generate most of that if you have a list of control names because the code should be identical.
ie
Sub Control_MouseMove()
DisplayStatus(Control)
End sub
I would consider the StatusText property and ControlTipText property of controls for this kind of help.
StatusText
This example sets the status bar help text for the form field named "Age."
With ActiveDocument.FormFields("Age")
.OwnStatus = True
.StatusText = "Type your current age."
End With
ControlTipText
This can be assigned from the property sheet for the control.
Private Sub UserForm_Initialize()
MultiPage1.Page1.ControlTipText = "Here in page 1"
MultiPage1.Page2.ControlTipText = "Now in page 2"
CommandButton1.ControlTipText = "And now here's"
CommandButton2.ControlTipText = "a tip from"
CommandButton3.ControlTipText = "your controls!"
End Sub