Using checkbox to enable/disable other checkboxes not working in form - vba

I feel like I'm going crazy here. I've tried everything I can think of and nothing seems to work. I have a checkbox CRShtnvsp and if checked, I have vba to enable additional checkboxes. This checkbox is actually enabled/disabled via another checkbox itself CRShtn. And that sequence works perfectly well. I just can't seem to get the vba for CRShtnvsp to enable additional checkboxes to work. Please help! VBA for both CRShtnvsp and CRShtn below.
This code works perfectly well:
Private Sub CRShtn_AfterUpdate()
If CRShtn = True Then
CRShtnvsp.Enabled = True
Else
CRShtnvsp.Enabled = False
End if
End Sub
This code does not work:
Private Sub CRShtnvsp_AfterUpdate()
If CRShtnvsp = True Then
CRShtn_vaso_phen.Enabled = True
Else
CRShtn_vaso_phen.Enabled = False
End if
End Sub
What's happening here?
All checkboxes are set to Enabled = No in the property sheet so that they are disabled until prior checkboxes or comboboxes on my form are changed using AfterUpdate() or Form_Current().

Sometimes Access seems to lose track of an event procedure. I don't know why that happens, but if that's your situation, you can remind Access the procedure exists.
Go to the Event tab on the property sheet for your CRShtnvsp checkbox. In the "After Update" property box, make sure "[Event Procedure]" is selected and then press the button labelled with 3 dots. You should be at your existing procedure. And I think that should be enough to remind Access it exists.

CRShtnvsp is not updating as it's not being used - I would try adding the code for enabling the next button under the original button you're using and see if that works. To clarify: Add the code for enabling CRShtn_vaso_phen under the CRShtn_AfterUpdate sub.

Related

vb.net control's events only when user clicks [duplicate]

Consider a simple .NET form with a couple of radio buttons and a checkbox.
Each of the radio buttons has a CheckedChanged handler setup that performs some action based on the state of the checkbox.
My problem is, when I initialize on the default radiobutton to be checked (from the designer properties window) the CheckedChanged event is fired for that radio button, but the Checkbox hasn't been initialized yet so I either get a null pointer exception or the wrong value is used in the handler. Either way, I don't want that handler code to be run unless the user picks a radio button after the form has been loaded.
I currently get around this by not initializing the radio button, but I need to set that default eventually and the best place is from the designer. I also can add a boolean field that's not set to true until the form is fully loaded and not process the events if that is false, but it's a dirty hack.
What can I do to prevent that handler from running its code?
To make it feel slightly less dirty, if you initialize the controls in the constructor of the form you might be able to use the forms IsHandleCreated property rather than your own bool to check if it should actually validate or not.
I would think that normally you wouldn't want to validate anything before it's been shown for the first time and handle isn't created until it is.
Code Example:
Private Sub myRadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles myRadioButton.CheckedChanged
If myRadioButton.Checked AndAlso myRadioButton.IsHandleCreated Then
'Do Work
End If
End Sub
"I also can put a boolean field that's not set to true until the form is fully loaded and not process the events if that is false, but it's a dirty hack."
It's also the easist and best way to do it!
Lets say .NET provides a neat way to turn an and off all the event handlers until the form is loaded. Even just the ones YOU are handling. It would still not be sufficiently flexible to disable what you wanted to enable but disable what you didn't. Often form setups happen and you want the events to fire. Also the form won't build right if no events fire.
The easy solution is to declare an initializing variable:
Private Initializing as boolean = True
Private Sub rb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbNuevos.CheckedChanged, RbDesaparecidos.CheckedChanged, RbModificados.CheckedChanged, RbNoDesap.CheckedChanged, RbDesHoy.CheckedChanged, RbChT.CheckedChanged
if Initializing then return
'Your Code
End Sub
Public Sub New()
' Llamada necesaria para el Diseñador de Windows Forms.
InitializeComponent()
' Agregue cualquier inicialización después de la llamada a InitializeComponent().
initializing = false
end sub
Most sophisticated: Remove the "handles" from the method, and use AddHandler on the new method.
Public Sub New()
' Llamada necesaria para el Diseñador de Windows Forms.
InitializeComponent()
' Agregue cualquier inicialización después de la llamada a InitializeComponent().
AddHandler RbChT.CheckedChanged, AddressOf rb_CheckedChanged
end sub
For radiobutton see Hans Olsson answer
For numeric up down, do it like this
Private Sub myNumeric_ValueChanged(sender As Object, e As EventArgs) Handles myNumeric.ValueChanged
If myNumeric.Value >= 0 AndAlso myNumeric.IsHandleCreated Then
'Do the work
End If
End Sub
The keyword is myNumeric.Value and IsHandleCreated
Yet another way:
Private Sub dgvGroups_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvGroups.CellValueChanged
If Me.Visible = False Then Exit Sub ' Sub gets called on form load which causes problems
wksGroups.Cells(e.RowIndex + 1, 1) = dgvGroups.Item(e.ColumnIndex, e.RowIndex).Value
wksGroups.Cells(1, 5) = dgvGroups.RowCount
One thing I've found that works is adding the events manually after you load the form.
To do this you can simply go into the generated form code found in the designer file of that form, and pull out the lines that add the event. It would look something like this:
this.controlName.CheckedChanged += new System.EventHandler(this.controlName_CheckedChanged);
Then put all of these calls into a method that you call after the InitializeComponent call in your form's constructor.
Just in case anyone is still searching for this the event is fired upon initializing the form BUT the form is not yet visible, Also Say that you have a foreign key relationship upon which you have a default value needed issue that gets fired every row update too. So the following code worked for me....
if (Visible && !(e.ColumnIndex == 0))
{
phoneEdited = true;
MessageBox.Show("A Phone entry has been entered");
}
Don't set checked on a control that really does much in designer.
The global flag and conditional exits where needed.
Try..Catch the sore spots to ignore a meaningless exception.
(Using VS 2017) It appears to me that it is an annoyance but not a bug. It is consistent with the model in use. The event is fired by normal operation of code, but code I did not write (but can access where fools fear to tread) and where there appears to be no (decent) place earlier in the normal flow to anticipate it.
The cleanest answer seems to be not to check radio button or checkbox controls in the designer at all if they trigger any significant code. Instead these controls should be changed by code (e.g. checked = true) in the Load event (for example) AFTER all the initialization is done.
There is no loss of flexibility here since both are fixed before the build, only in different places. The event handlers will handle it exactly as if a user had clicked the control in the natural flow of a well designed GUI application. (This reminds me of the ancient RPG proverb "Don't buck the cycle". (Anyone here remember RPG? I, not part of IBM-oriented team, never used it but had interesting discussions with some who did. ) Pre-checking controls hits the wrong part of the VS cycle.)
If for any reason that will not work, the next best thing is the kludge suggested elsewhere of a single status boolean initialized false and set true at the appropriate time with conditional exits in the necessary places to prevent them from crashing until then. It will get the job done, but it's ugly. Better than failure.
Another thing I tried before I decided that designer level pre-set checks were the problem and there was a very acceptable alternative was to put the danger spots in a Try..Catch to be able to ignore the exception. Also a kludge.
For the cleanest code, reverse the True/False approach used in some other examples. Focus on 'ready' rather than 'busy'. Here's an example for a Windows Form:
At the Class level, add Private app_ready As Boolean (it will be False by default).
At the end of the Form.Shown event handler, add app_ready = True.
In each control event handler where it's needed, add:
If app_ready Then
' code
End If
Starting a routine with something like If initialising Then Exit Sub just doesn't feel right!
Maybe for some functionality you can use the click event instead of the check changed event.
I put a public variable in the Module1 file
Dim Public bolForm_LoadingTF as Boolean = True
In each formLoad event I put
bolForm_LoadingTF = True
In each control with an OnSelectedIndexChanged
event I put if bolForm_LoadingTF = True then Exit Sub
At the end of the form load event I put
bolForm_LoadingTF = False
I am probably breaking a bunch of rules but this works
for me.

Programming VBA in an Outlook form

I created my own Outlook form to use it as standard surface to enter certain orders instead of the normal message form. The creation, editing and sending works perfectly fine and in the next step I want to insert some code via VBA.
My problem is that I can´t access the objects of my form in the VBA editor. E.g. I want to show a message box when a certain checkbox is checked. According code would be:
Sub example()
If CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
When I run the code I get the error that the object could not be found. The same goes for every other object, like textboxes or labels etc.
I guess the solution is pretty simple, like putting Item. or sth. like that in front of each object. But so far I wasn't able to find the solution.
I´m using Outlook 2010.
I know this is a year too late but you'll want to do something like this example below. It's kinda a work around but you can get whatever value was selected.
Sub ComboBox1_Click()
Set objPage = Item.GetInspector.ModifiedFormPages("Message")
Set Control = objPage.Controls("ComboBox1")
MsgBox "The value in the " & Control.Name & _
"control has changed to " & Control.Value & "."
End Sub
You should be able to get the value, just get a handle on the object you want using the Inspector
The following is an excerpt from here
When you use a custom form, Outlook only supports the Click event for
controls. This is a natural choice for buttons but not optimal for
controls like the combo box. You write the code by inserting it into a
form’s VBScript editor. You need to have the Outlook form open in the
Form Designer and click the View Code button found in the Form group
of the Developer tab.
Sub CheckBox1_Click()
msgbox "Hello World"
End Sub
The code page is fairly minimal with no syntax highlighting. I just tried this now and it does work. Dont forget to Publish your form to pick up the new changes.
I know this is almost 6 years late but, in VB and VBA, simply start with the form name. (And if that doesn't work, just keep going up a parent object and you'll get there.) So, your code becomes:
Sub example()
If MYFORMNAME.CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
Of course, after typing "MYFORMNAME." you'll know if it will work because typomatic will kick in when the system recognizes "MYFORMNAME" after you hit the period.

Treeview behaving differently when tapped than when clicked

The situation: I have an Access 2010 db that is meant to be deployed on a Windows 8 tablet. The main form of the app contains a Treeview control. Selecting a node on the Treeview sets the visibility of one of several subforms that are used for viewing/editing details of the selected node item. I have a yes/no message box and some basic code on the BeforeUpdate event for each of the subforms. So when the record on the subform is dirty and the user clicks anywhere on the main form (including anywhere in the Treeview control) this code is triggered.
The problem: When the subform record is dirty and the user taps anywhere on the Treeview control, the message box pops up but cannot be interacted with because the app is busy. Doing what, I don't know, but it stays that way until Access is shut down via Task Manager. There is no code attached to the Treeview for anything but the Click event. This happens even when they touch white space in the Treeview below the existing nodes.
If the record is not dirty, everything works fine.
If the record is dirty and the user hits the "Save" button on the subform to trigger the BeforeUpdate event, everything works fine.
If the user taps a different control or in the empty space on the main form, the BeforeUpdate event is triggered and everything works fine.
If you plug a mouse into the tablet and do the same series of steps by clicking instead of tapping, everything works fine.
I've done a ton of searching and haven't been able to find anything relevant to this, so any suggestions or guidance to new places to look for suggestions would be deeply appreciated.
I've attached a sample of the BeforeUpdate code that exists on each of these subforms. It's pretty basic, but maybe there's something in it that tapping and Treeviews just don't like.
Private Sub Form_BeforeUpdate(Cancel As Integer)
'If the form data has changed a message is shown asking if
'the changes should be saved. If the answer is no then
'the changes are undone
On Error GoTo BeforeUpdate_Error
If Me.Dirty Then
'Add PropertyID, LPParentNodeID and TreeNodeID if Record is new
If Me.NewRecord Then
Me.PropertyID = Me.Parent!PropertyID
Me.LPParentNodeID = Me.Parent!txtCurrKey
Me.TreeNodeID = DateDiff("s", Date, Now())
End If
'Display prompt to save the record
If MsgBox("The record has changed - do you want to save it?", _
vbYesNo + vbQuestion, "Save Changes") = vbNo Then
Me.Undo
End If
End If
'If the record is still dirty, then record the change in the Audit table
If Me.Dirty Then
Call AuditTrail(Me, InstanceID, PropertyID)
End If
BeforeUpdate_Exit:
Exit Sub
BeforeUpdate_Error:
MsgBox Err.Description
Resume BeforeUpdate_Exit
End Sub
08/30/2013 Addition: I forgot to mention the debugging behavior in the original question. When I set a breakpoint on the BeforeUpdate Sub of the subform on any line from the actual Sub entry point down to the If statement with the message box, the code window comes up but the app again becomes busy, and I can't interact with either window. Just like before, this behavior is unique to tapping that accursed Treeview control.
What you could do is put a kind of edit/save structure into each of the subforms, whereby controls in the subform are locked until edit is clicked, and re-locked after save is clicked. So:
private sub bEdit()
editMode true
end sub
private sub bSave()
...save logic
editMode false
end sub
private sub editMode(isEdit as boolean)
dim ctl as control
for each ctl in me.controls
if ctl.controltype is actextbox or ctl.controltype is accombobox then
ctl.locked = (not isEdit)
end if
next
end sub
With this approach, it's then a small task to add editmode control for the parent form by adding
me.parent.editmode isEdit
to the end of of the editmode procedure.
In the parent form, editMode will need to be a PUBLIC sub.
In this sub, control whether the tree will do anything when clicked on:
public sub editMode(isEdit as boolean)
tree.enabled = (not isEdit)
end sub

VBA Listbox becomes unresponsive after first use

I have a VBA (Excel 2010) system which involves selecting an item from a listbox and then displaying it in another form. Here is a very simplified version of what happens.
' Part of frmForm1 code module
sub lstListbox_Click
dim MyEvent as string
dim i as integer
i=me.lstListbox.listindex
MyEvent=me.lstlistbox.list(i)
' Now show the item in the second form
Load frmForm2
me.hide
ThisWorkbook.LoadDataIntoForm2 (frmForm2, MyEvent)
frmForm2.show
unload frmForm2
me.show
end sub
The listbox accepts the click, and first the event (the event handler is giver above). Key parts of the event handler are:
Load the second form (to display the detail data)
Pass the second form as a UserForm parameter to a procedure (LoadDataIntoForm2)
Hide the host form (frmForm1) and show the second form (frmForm2)
When the second form processes an Exit click, the code looks like this:
' Part of frmForm2 code module
sub cmdExit_Click
me.hide
end sub
The first time round it works fine - but when I return to frmForm1 (in the tail end of the lstListBox_Click procedure), even though the rest of the form is operative, the listbox remains stubbornly unresponsive.
I've managed to abstract this down to a little demo system if that would help - the same behavior is seen there. (It's regular .xls file, but that seems not to be easily acceptable as an upload)
Has anyone seen this before? And does anyone have any ideas how I might get this to work the way I want it to?
Thanks,
Tony
The default for the .Show method is to make the form modal. Explicitly set it to modeless:
Sub lstListbox_Click
...
Me.Show vbModeless
End Sub

Visual Basic - how to force event to fire

I'm new to VB (using VBA, actually) and I would like to force an event to fire. Specifically, I am updating the value in a textbox and I would like to call that textbox's AfterUpdate() event.
Private Sub cmdCreateInvoice_Click()
Me.txtInvDate = '11/01/10'
Me.txtInvDate.AfterUpdate
End Sub
During run time, I get an error that says "Compile Error: Invalid Use of Property". If I try something similar, but with the click event (ex: cmdCreateInvoice.Click), which does NOT have a property that shares the name, I get an error that says "Compile Error: Method or Data member not found".
I know there must be a way to fire one event from another. But what is the proper syntax?
Thanks!
AFAIK, there is no way to "fire an event manually" in VB(A). What you can do is call the event handler manually, and for this, rdkleine has given you the answer already:
Call txtInvDate_AfterUpdate()
This will have exactly the same effect as if the event had fired (though it does not give you the whole chain of events that may also fire along with it--unless you Call their handlers as well).
IgorM has another valid point, in comments on his answer--it's "cleaner" to write a different Sub to do the work you want done, then call it from both the event handler & wherever you're trying to do it now (button click?). So:
Private Sub txtInvDate_AfterUpdate()
DoWhatever
End Sub
Private Sub button_Click()
DoWhatever
End Sub
Private Sub DoWhatever
'your desired effect
End Sub
You could even make DoWhatever a Public Sub in a Module.
Edit
And no, in VB(A) it doesn't matter what order you define your Sub (or Function) routines.
Call
txtInvDate_AfterUpdate()
Try to use something like this:
Private Sub TextBox1_LostFocus()
TextBox1.Value = "5"
End Sub
Use LostFocus event if you change value manually.
Or you can use another way:
Private Sub CommandButton1_Click()
TextBox1.Value = "new value"
End Sub
Private Sub TextBox1_Change()
MsgBox TextBox1.Value
End Sub
Try to use Change event.
Some code apparently will (regretfully) force events to fire:
Me.Dirty = False
forces BeforeUpdate and AfterUpdate to fire, as I understand it. There are probably more cases too. Any secret hooks which attach to your code in that manner reflect bad design in my view and will inevitably lead to inadvertant & frustrating loops being created.
Is there a way to keep a textboxes event firing until I click on another text box? I'm needing to stay "in" the text box which has Do While loop for a selection. Once the selection is made, I want the user to be able to replace the selection without having to click on the text box again. If the user is satisfied with the select, they can click on a different text box.