filter selection prompt by user - vba

i want to run my code such that it can pause and allow user to filter a column and then the code continues to run. Is this possible?
I thought of using inputbox to prompt user to key in the name. However, this is not feasible because user may not be able to remember the exact name. It will be more convenient for the user to look at the drop down list and select the name he wants to filter.
Please advice me how can this be done. Thank you

You can't stop a VBA macro to wait until an action was done within Excel.
I see the following possibilities:
a) Create a small userform showing a dropdown. If you show that form modal (that is the default), the macro will pause and wait until the form is closed. However, you will need to implement code to fill up the dropdown and to set the filter by code.
b) End your macro and implement an event that fires when the user changes the filter. There is no "On filter change"-event in Excel, but there is a work around: Excel VBA Filter Change event handler. Put the second part of your code into a separate sub and call that from the event handler.

Related

How do I get to a control in a subform

I want to go to a control in a subform in order to be able to enter data.
I can make a DoCmd.OpenForm but then I get an extra copy of the subform on top of the main form. I would rather go directly to the subform. I have tried a lot of options and the following I thought was the most promising
Me.Subform.SetFocus
Me.Subform.Form.mycotrol.SetFocus
DoCmd.GoToControl "mycontrol"
This piece of code does not stop at the Subform to let me enter data. I have also tried Me!Subform with the same result. I have thought of adding a Stop statement, but then I don't know how to resume execution when data has been entered to the subform.
I think there is something I have not understood. Can someone help me out?
Biørn Veirø
VBA is single threaded. It can't work with Access forms this way. You could do it with a modal popup that returns a value, like an InputBox. Access forms don't return a value this way.
If you really want to have a step by step using Access forms then you need to hook in to form events. For this specific case I think the event you want is TextBox.AfterUpdate
https://learn.microsoft.com/en-us/office/vba/api/access.textbox.afterupdate-event
You can have different events on each control object, allowing you to have code that runs after each input.
First set focus to the subform control, then the control in the form:
Me!YourSubformControlName.SetFocus
Me!YourSubformControlName.Form!YourControl.SetFocus

Using Excel while macros are running

There are a half-dozen answers to this. "Open a second instance" "Have a pause" Etc. I'm not looking for that.
I'm looking for the user of the workbook to be able to manipulate the workbook while the macro is running. I've seen this working before, where the user could scroll around, change tabs, even add and remove data, all while the macro was running. Unfortunately, I couldn't get permission to look at the code (And committing CFAA violations ins't my cup of tea), so I have no idea how they did it.
How can you enable a user to edit the workbook as macros are running? For a specific example, I have Conway's Game of Life running. Users select cells to flip live/dead, then can run a macro to run the entire thing. I think it'd be nice for users to be able to change cells as the macro is running. (which is a second on select macro)
Thank you
Sorry just reread the question. I wouldn't expect the permutation to run for very long - not long enough to interrupt really.
But if it does, then the advice about using lots of DoEvents stands.
The other option is that you can use the OnTime event to have a "heartbeat"
VBA Macro On Timer style to run code every set number of seconds, i.e. 120 seconds
You can set the timer to say 3 seconds. Every time the OnTime event occurs you do one step of your permutation. In the three seconds in between they can edit.
Refactor your macro to use Events. In which case, you would have a series of event handlers (instead of one monolithic macro) to respond to various triggers. This is assuming that the macro is influenced by what the user is doing in the worksheet.
One way of (sort of) doing this is to use a Modeless Userform (UserForm.Show vbModeless)
The user form stays visible but the VBA stops running when the form is shown and the user can then interact with Excel. Then when the user clicks a button on the form the code behind the button starts running again.
So in reality the user is either interacting with Excel or interacting with the form ...

How to pause code using userform

so basically I need to pause the macro code, wait for user input and then continue the rest of the code. I know that by using the "modeless userform", this can be done.
But the point now is that all the code after the userform popped up needed to written in the command button part (basically it is the userform own module). And because of that, all the initialization, all the variables that I still need has been wiped off.
So I am asking is there a way to pause in the middle of a vba code then, wait for user input, then continue the rest of the code
Thank you very much for your help
What do you mean as "user input"?
Standard input methods as MsgBox, InputBox or modal user forms do what you are asking;
"code after the userform popped up needed to"...
Instead of closing a form, hide it. Code continues and form data
still available;
Now, if you mean by "user input", manipulating workbook, you must go on events:
Example: Create a before user input macro and then place remaining code in a Worksheet_Change event.

How to use Mouseout Function in VBA with a condition Check?

Hi I have a VBA application with a combobox selection option.One of the combobox option is Other which when user select that option an invisibled label will pops up and prompt the user to fill the txtOther control.now my question is how I can get rid of the prompted label and make it to invisible after user fill the txtOther and move(focused)in other control?
here is a shot of my app:
Thanks for your time and help
It depends on how often or when you want the check to be done. I couldn't find a good reference to show you all of this, but you can view your VBE.
Within the code behind your userform you can use events for the text box.
If you want the check done every time you leave the text box:
Use the TextBox_Exit event.
If you want it to fire whenever the contents are changed, used TextBox_Change. There's lots of options, but based on your explanation I'd probably use Exit.
This answer shows some syntax examples for using the Textbox_Exit event.

Hiding columns in a datasheet

I am trying to hide specific columns in an Access 2007 split form through code. I need the form to check certain conditions to see whether it needs to display a column or not. I have code in the form's 'Activate' event to hide the column like this:
txtControl.ColumnHidden = True
This code works in the "Open" event, but if I hide the column on Activate, it won't display these changes until I close the form and open it again. I have tried calling the form's refresh, repaint, and requery methods, but this doesn't work. Please help!
Edit: Ideally, I need this event to occur whenever the focus switches to this form.That's why I'm using the Activate event rather than the Open event.
Try setting it in either the form's Current or Load events. You will also probably need to requery the control after setting that property: Me.TextControl.Requery Current is called every time a form's record is changed, the form is repainted or requeried. Load, as its name suggests, is called once, after the form has opened when the form loads its records. These have always been more reliable for me than using Activate, which really has to do with more the focus of the form, not really what you want.
I've had a problem like this before working in Access 2002. I was able to solve the problem with a subform by setting the subform source object equal to itself and then running the requery.
Me.SubForm.SourceObject = Me.SubForm.SourceObject
Me.SubForm.Requery
See if this technique works for your particular situation.