Ms-Access - IF checkbox = true THEN (Compile error) - ms-access

A Compile error occurs when i try this code allow a button to be pressed if the check boxes are true
Option Compare Database
Function CheckMyButton()
Me.Command414.Enabled = (Me.chk1 And Me.chk2 And Me.check4 And Me.chk3)
End Function
Private Sub chk1_AfterUpdate()
=CheckMyButton()
End Function
Private Sub chk2_AfterUpdate()
=CheckMyButton()
End Sub
Private Sub check4_AfterUpdate()
=CheckMyButton()
End Sub
Private Sub chk3_AfterUpdate()
=CheckMyButton()
End Sub

=CheckMyButton() doesn't go into the body of event procedures (in there you would use Call CheckMyButton() ).
=CheckMyButton() goes directly into the AfterUpdate property - in the property window of the form.
The point is to not have multiple event procedures that all do the same thing.
German Access here:

Related

Use VBA to enable a text box when a check box is checked

I am new to VBA and I'm trying to create a form in Access where a text box is enabled or disabled based on whether a check box is checked or unchecked.
So if the 'Survey Requested?' box is checked the 'Date Survey Requested' box is enabled for the user to enter a date.
I have the following code:
Private Sub CheckSurveyRequested_AfterUpdate()
If CheckSurveyRequested = True Then
DateSurveyReq.Enabled = True
Else
DateSurveyReq.Enabled = False
End If
End Sub
But this comes up with a '424 Object Required' error when I run line 5.
Anyone have a suggestion as to what I'm doing wrong here?
You should definitely use the AfterUpdate event---tying the behavior of your textbox to the click event means that a user using their keyboard to navigate the form won't get the same behavior.
Also, you should replicate this behavior when the form loads: If the default value for the checkbox is False, then your textbox should be disabled when the form loads.
Also, as #ErikA notes, you can do this with one readable line.
So I would recommend something like this:
Option Explicit
Private Sub chkSurveyRequested_AfterUpdate()
Me.txtDateSurveyReq.Enabled = Me.chkSurveyRequested.value
End Sub
Private Sub Form_Load()
Me.txtDateSurveyReq.Enabled = Me.chkSurveyRequested.value
End Sub
In order to not repeat yourself, you could move this code into a separate sub:
Option Explicit
Private Sub Form_Load()
' assign the function below to the AfterUpdate event of the checkbox.
Me.chkSurveyRequested.AfterUpdate = "=UpdateControls()"
' now execute the function directly
UpdateControls
End Sub
Private Function UpdateControls()
Me.txtDateSurveyReq.Enabled = Me.chkSurveyRequested.value
End Function
I would suggest the following -
Private Sub CheckSurveyRequested_AfterUpdate()
DateSurveyReq.Enabled = CheckSurveyRequested
End Sub

Button to turn change AllowEdits to true in Access using VB

I am trying create a button which changes the value of AllowEdits to False and another for true for a subform. I am using the below code. I get a Runtime error 424 each time I run it.
Option Compare Database
Private Sub Toggle_Edit_Click()
Dim strForm As String
strFormName = Me.Name
Call ToggleEdit(Me)
End Sub
and
Option Compare Database
Public strFormName As String
Sub ToggleEdit(myForm As Form)
Call Message
ctrlControl.AllowEdits = True
End Sub
and if you were interested
Sub Message()
MsgBox "Remember not to overwrite incorrect records"
End Sub
Please add Option Explicit at top of your modules!
I think AllowEdits is a Form property, not a Control property.
Option Explicit
Sub ToggleEdit(myForm As Form)
myForm.AllowEdits = Not myForm.AllowEdits
End Sub
If the code is behind the form itself, you can use Me.
Sub ToggleEdit() 'no parameter
Me.AllowEdits = Not Me.AllowEdits
End Sub
If you want to act at control level, use Locked or Enabled properties.

Access Listbox AfterUpdate Not Firing when Selected = True

I have an Access form with a listbox and a combo box.
Private Sub Form_Open(Cancel As Integer)
'... misc code
me.comboBox.RowSource = "sql statement"
call comboBox_AfterUpdate
End Sub
Private Sub comboBox_AfterUpdate()
listbox.Selected(0) = true
End Sub
Private Sub listbox_AFterUpdate()
'.... important code that updates the form
End Sub
On the form load/open, the the code runs as
Form_Open > comboBox_AfterUpdate > listbox_AfterUpdate
However after the form has already loaded, when the combo box is changed and the comboBox_AfterUpdate is triggered, the listbox_AfterUpdate() is NOT triggered.
I would move the code in the listbox_AfterUpdate() to a separate sub-routine, but that would call the code to run twice when the form loads. Any ideas on why this happens?
Similar issue here:
http://www.tek-tips.com/viewthread.cfm?qid=1395160
You could use two custom functions and use optional parameters to know the context in which it is called. That way you have full control over when which function does which.
E.g.
Public Sub MyFunc1 (Optional CalledFromLoad As Boolean)
Public Sub MyFunc2 (Optional CalledFromLoad As Boolean)
Private Sub comboBox_AfterUpdate()
Call MyFunc1 False
End Sub
Private Sub listbox_AFterUpdate()
Call MyFunc2 False
End Sub
Private Sub Form_Open(Cancel As Integer)
Call MyFunc1 True
End Sub
Moving all your code gives you a greater degree of customization, and can help you avoid calling the same code twice.
You could possibly set a global variable in MyFunc1, then trigger MyFunc2 by changing the listbox, and then reset that global variable again. Numerous solutions are possible.

get Access VBA have a Sender

In Access I have a form that has about 200 text boxes on it. I Know that in C# on every on click event there is textbox1_Click(object sender).
how can I in VBA have reference the current sender without saying Me.txt_Whatever?
this is what I have so far
Private Sub txtHotMix_DblClick(Cancel As Integer, sender As Object)
Dim txt As TextBox
Set txt = sender
MsgBox txt.Name
End Sub
I think the ActiveControl property of the Screen object is what you're after (https://msdn.microsoft.com/en-us/library/bb225491(v=office.12).aspx):
In a module:
Option Compare Database
Option Explicit
Public Sub ctrl_Print(objCtrl As Control)
MsgBox objCtrl.Value, vbOKOnly, "Control Value"
End Sub
You would still have to add a click event to every control to call this sub, like this:
Option Compare Database
Option Explicit
Private Sub Text0_Click()
Call ctrl_Print(Screen.ActiveControl)
End Sub

Two different Private Sub statements same code

I have received code for Access 2010 in VBA and there are two separate sub statements that contain the same code which is as follows:
Private Sub Test_Type_AfterUpdate()
If Not IsNull(Me.Test_Type.Value) Then
Me.TestNameID.Value = frmTestNameIDSmallToBigUpdate
Me.Test.Value = frmTestSmallToMedUpdate
End If
End Sub
Private Sub Test_Type_Change()
If Not IsNull(Me.Test_Type.Value) Then
Me.TestNameID.Value = frmTestNameIDSmallToBigUpdate
Me.Test.Value = frmTestSmallToMedUpdate
End If
End Sub
Also other Private Sub statements contain the same coding but a different variable after If Not IsNull. I am new at VBA but have experience programming in Python and C#, is there a way to combine these statements into one statement.
Thanks
Yes. Create a third private sub to handle Test_Type's events.
Private Sub HandleTestTypeEvent(object)
If Not IsNull(object.Value) Then
Me.TestNameID.Value = frmTestNameIDSmallToBigUpdate
Me.Test.Value = frmTestSmallToMedUpdate
End If
End Sub
And call it from your existing event procedures.
Private Sub Test_Type_AfterUpdate()
HandleTestTypeEvent Me.TestType
End Sub
Private Sub Test_Type_Change()
HandleTestTypeEvent Me.TestType
End Sub
Make sure properly type the parameter and give it a meaningful name.
If you're feeling really froggy, you can have a look at this.
Overriding all Access form control AfterUpdate methods
For better maintainability, refactor that code into a single sub that is called from both event subs, like this:
Private Sub Set_Test_NameID_and_Value
If Not IsNull(Me.Test_Type.Value) Then
Me.TestNameID.Value = frmTestNameIDSmallToBigUpdate
Me.Test.Value = frmTestSmallToMedUpdate
End If
End Sub
Private Sub Test_Type_AfterUpdate()
Call Set_Test_NameID_and_Value()
End Sub
Private Sub Test_Type_Change()
Call Set_Test_NameID_and_Value()
End Sub