Access Forms - New Field opens if another field is selected - ms-access

I was wondering if it were possible to have a field shown or active if another previous field is selected.
For example, if I have a Status field and then choose Inactive then I would like another field to show for Inactive Date
Thanks

Let say, you have checkbox for status and textbox to capture date on your form. AfterUpdate event gets triggered when control value changes and you can enable/disable other controls here.
Private Sub chkStatus_AfterUpdate()
If Me.chkStatus Then
Me.txtDate.Enabled = True
Else
Me.txtDate.Enabled = False
End If
End Sub
To enable/disable textbox on dropdown change using VBA
Private Sub cmbStatus_Change()
If Nz(Me.cmbStatus, "") = "Active" Then
Me.txtDate.Enabled = True
ElseIf Nz(Me.cmbStatus, "") = "InActive" Then
Me.txtDate.Enabled = False
End If
End Sub
Using Macro builder - without VBA

Related

Check Box makes Label Visible

I have three Combo Boxes - When they are selected i would like it to check the tick box.
if the check box is ticked the Text box will then become Enabled, i currently have it set as disabled.
my question is how would i set the check box to be true when the three Combos are selected.
and how would i make it so that when the tick box is true it will enable the tex box
When all 3 combo boxes are set it will enable the checkbox. Once the value for any combo box is updated it calls a common function which checks whether all combo boxes have a value assigned and accordingly set the checkbox.
Private Sub cmbClientContact_AfterUpdate()
Call SetCheckBox
End Sub
Private Sub cmbClientName_AfterUpdate()
Call SetCheckBox
End Sub
Private Sub cmbProjectManager_AfterUpdate()
Call SetCheckBox
End Sub
Private Sub SetCheckBox()
If Nz(Me.cmbClientContact, "") <> "" And Nz(Me.cmbClientName, "") <> "" And Nz(Me.cmbProjectManager, "") <> "" Then
Me.Check25 = True
Me.Text27.Enabled = True
Else
Me.Check25 = False
End If
End Sub
Enable/disable textbox basis value of the checkbox
Private Sub Check25_AfterUpdate()
If Nz(Me.Check25, False) Then
Me.Text27.Enabled = True
Else
Me.Text27.Enabled = False
End If
End Sub
I would reccomend using the AfterUpdate event of all 3 combo boxes. Since the code is going to be the same (you're checking if all 3 combo boxes have a value), you can create one function to handle the check, and set that function to the AfterUpdate event of all 3 combo boxes when the form loads.
The function to update the controls (both the text box and check box) would be something like this:
Private Function UpdateControls()
Me.Text1.Enabled = Not (IsNull(Me.Combo1) Or IsNull(Me.Combo2) Or IsNull(Me.Combo3))
Me.Check1.Value = Not (IsNull(Me.Combo1) Or IsNull(Me.Combo2) Or IsNull(Me.Combo3))
End Function
You can call this function when the form initially loads, so the checkbox will be unchecked and the textbox will be disabled:
Private Sub Form_Load()
' update controls initially when the form loads
UpdateControls
End Sub
To make sure the same update happens whenever one of the combo box's values are updated, you can set each combobox's AfterUpdate event to the same function, like this:
Private Sub Form_Load()
' set each combo box's AfterUpdate event to run the check
Me.Combo1.AfterUpdate = "=UpdateControls()"
Me.Combo2.AfterUpdate = "=UpdateControls()"
Me.Combo3.AfterUpdate = "=UpdateControls()"
End Sub
So your final code might be something like this:
Private Sub Form_Load()
' set each combo box's AfterUpdate event to run the check
Me.Combo1.AfterUpdate = "=UpdateControls()"
Me.Combo2.AfterUpdate = "=UpdateControls()"
Me.Combo3.AfterUpdate = "=UpdateControls()"
' update controls initially when the form loads
UpdateControls
End Sub
Private Function UpdateControls()
Me.Text1.Enabled = Not (IsNull(Me.Combo1) Or IsNull(Me.Combo2) Or IsNull(Me.Combo3))
Me.Check1.Value = Not (IsNull(Me.Combo1) Or IsNull(Me.Combo2) Or IsNull(Me.Combo3))
End Function
Without knowing more specifics about the name schemas of your objects, this is my semi-vague answer:
One option (of many) is to use an On Click event procedure with the following:
If Not IsNull(Me.Combo1) _
And Not IsNull(Me.Combo2) _
And Not IsNull(Me.Combo3) Then
Me.Check1 = True
Me.Text1.Enabled = True
Else
Me.Check1 = False
Me.Text1.Enabled = False
End If
This assumes that the checkbox is named Check1 and the textbox is named Text1 and the comboboxes are Combo1, Combo2, and Combo3
It is a little confusing whether you meant Enabled or Visible, but if you meant Visible, just change the lines that say .Enabled to .Visible

Access : how do i cancel an action if the data entered in the fields on the main matches data from a table displayed in a subform?

I have a Main Form with 3 fields, where the rep would enter the Property, Room Type, and the requested Check-in Date - however on the same form i have a subform displaying Property, Room Type and Check-in Dates NOT AVAILABLE. If a rep enters data in those 3 fields and ALL 3 match what is in the NOT AVAILABLE subform - what code (either VBA or On Lost Focus etc) can i use to look up those values in the subform (so that if it matches what's in the subform it will not allow for submission) and also popup an ERROR message that the "Property, Room Type and Check-In Date you selected are not available" ?
(Main Form data entered will go into a table let's say RoomRequestTable and Not Available subform displays data from another table RoomNotAvailableTable)
Below is some sample code utilizing the BeforeUpdate event. I'm assuming you want to trigger this from any of the fields, so I wrote a sample function that returns false if all of the fields match. We access the subform by calling Me.[SubFromName].Form.
Private function validate() As Boolean
Dim sbfrm As Form
Set sbfrm = Me.SubFormName.Form
If Me.FieldName = sbfrm.FieldName And Me.FieldName2 = sbfrm.FieldName2 And Me.FieldName3 = sbfrm.FieldName3 Then
MsgBox "you can't do that", vbExclamation, "Sorry kid"
'return false
validate = false
Else
'return true
validate = true
End If
End Function
Private Sub ControlName1_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName1.Undo
End If
End Sub
Private Sub ControlName2_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName2.Undo
End If
End Sub
Private Sub ControlName3_BeforeUpdate(Cancel As Integer)
Cancel = Not validate
If Cancel then
Me.ControlName3.Undo
End If
End Sub

OldValue and Value same on radio button change Access 2007 VB

I have an access 2007 front-end app. On a particular form, there are 2 radio buttons in a radio button group. I am trying to detect when the radio button group is changed and capture the old and new values, but my OldValue and Value properties are = in the save event, even if I have changed it. The OldValue is equal to the New radio button value, not what it was originally.
I tried coding this in the form's Save subroutine. The intent was to compares the RB value with the original dataset value to force setting the old value, but it doesn't like the 'SET' statements
If fraResistOption.Value = 1 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "N"
Else
If fraResistOption.Value = 2 And (IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "N") Then
Set fraResistOption.OldValue = 1
[Dl_Resisted] = "Y"
Else
If fraResistOption.Value = 1 And (Not IsNull([Dl_Resisted]) Or UCase([Dl_Resisted]) = "Y") Then
Set fraResistOption.OldValue = 2
[Dl_Resisted] = "Y"
End If
End If
End If
End If
Could someone suggest a way to do this? Please and thank you.
The .OldValue property of the Option Group (sometimes referred to as "Frame") does work. I have a table named [optValues]:
[ID] - AutoNumber
[optValue] - Numeric (Long Integer)
It contains one record:
ID optValue
1 3
My form's Record Source is the [optValues] table. The form has an Option Group named "Frame0" whose Control Source is the [optValue] field. It contains three Option buttons
label: "foo", value: 1
label: "bar", value: 2
label: "baz", value: 3
The After Update event handler for Frame0 is:
Private Sub Frame0_AfterUpdate()
MsgBox "Old value: " & Me.Frame0.OldValue & ", New value: " & Me.Frame0.Value
End Sub
When I open the form, "baz" is selected (because [optValue] is 3 in the table):
When I click on "foo" I immediately see the (correct) old and new values:
The only way I can think of to detect and capture changes to Option-Group values is to handle the Form_Current event (save the Option Group value), then also handle the Option-Group After event. Although you can change the Option-Group.Value, OldValue is likely a protected (read-only) property. Hope something like the following helps:
Dim OldValue As Byte
Dim CurrentValue As Byte
Private Sub Form_Current()
OldValue = Frame0.Value
End Sub
Private Sub Frame0_AfterUpdate()
CurrentValue = Frame0.Value
Debug.Print "AFTER: OldValue=" & OldValue & "' CurrentValue=" & CurrentValue
End Sub
I don't know why but .oldvalue doesn't work for me.
If you are in the same boat as me, you can use the BeforeUpdate event of the optionGroup and set a static variable.
Then read the static variable in afterUpdate event and reset it for the next change.

Clear Textbox on key press

Is there any way to clear the textbox on keypress like in excel.
I tried the following code but it clears the text when clicking on the textbox. I want it to clear it when a certain key is pressed.
Private Sub Text10_GotFocus()
Text10.Value = ""
End Sub
You could select the control's entire text content whenever that control gets focus. Then your keypress would replace the selected text.
If you want that to happen for every text box on every form, you can set "Behavior entering field" setting to "Select entire field". (In Access 2007, find that setting from Office Button -> Access Options -> Advanced, then look under the Editing heading of that dialog. For Access 2003, see this page.)
Not only will that setting be applied to form controls, but also to tables and queries in datasheet view. If that's not what you want, you can use VBA in your form's module to select the text for only specific controls:
Private Sub MyTextBox_GotFocus()
Me.MyTextBox.SelStart = 0
Me.MyTextBox.SelLength = Len(Me.MyTextBox)
End Sub
If you want to do that for multiple controls, you could create a general procedure:
Private Sub SelectWholeField()
Me.ActiveControl.SelStart = 0
Me.ActiveControl.SelLength = Len(Me.ActiveControl)
End Sub
Then call that procedure from the got focus event of an individual control like this:
Private Sub MyTextBox_GotFocus()
SelectWholeField
End Sub
Private Sub Field1_KeyPress(KeyAscii As Integer)
If KeyAscii = 65 Then Me.Field1 = ""
'Or: If Chr(KeyAscii) = "A" Then Me.Field1 = ""
End Sub
change the number to the ascii value of whatever key you are wanting to use to clear the field
Declare a Form Level variable
Private CanDelete as Boolean
When the TextBox receives focus, set CanDelete to True
Private Sub txtTest_GotFocus()
CanDelete = True
End Sub
On the KeyPress event, clear the text if CanDelete is True
Private Sub txtTest_KeyPress(KeyAscii As Integer)
If CanDelete Then
Me.txtTest = ""
CanDelete = False
End If
End Sub

How to reference a textbox from another form?

I'm trying to modify existing code to add a popup box. This popup box is another form, and when a user clicks on a button on this form, I want a textbox on the base form to be populated. After that the popup disappears and then I need to access that value from the textbox on the base form.
The sequence should be:
Base form button click calls modal popup
Click in button on popup saves value to base form's textbox, then returns control.
Base form then uses this value to do something.
Base Form
Sub base()
DoCmd.OpenForm "PaperType", , , , , acDialog
MsgBox Me.TheAnswer 'This line gives a null error
End Sub
Popup Form
Private Sub btnRolls_Click()
'Me.Tag = 1
Forms!ReceiptDetail_sfrm!TheAnswer = 1
Me.Visible = False
End Sub
Private Sub btnSheets_Click()
'Me.Tag = 4
Forms!("ReceiptDetail_sfrm").TheAnswer = 4
Me.Visible = False
End Sub
You can do it that way, you probably need something like:
Private Sub btnRolls_Click()
Forms!ReceiptDetail_sfrm!TheAnswer = 1
Forms!ReceiptDetail_sfrm.Refresh
Me.Visible = False
End Sub
I've done what you're doing many times. I'm not at work to check my code right now for an example, but it's doable.
What I tend to do is hide the popup, then read from it:
Main Form:
Private Sub base()
DoCmd.OpenForm "PaperType", , , , , acDialog
'code waits for modal hide here
Me!TheAnswer = Forms("PaperType").SomethingOnThatFormThatStoresTheValue
DoCmd.Close acForm, "PaperType", acSaveNo
MsgBox Me.TheAnswer
End Sub
Popup
Private Sub btnRolls_Click
'may be hidden control
Me.SomethingOnThatFormThatStoresTheValue = TheValueToRead
Me.Visible = False
End Sub
Make sure that either A) Your popup can't be closed from the form itself, or B) Your calling code will handle the error of trying to read something that's no longer loaded (or both).
Apparently the correct answer is: "Don't do it this way!". Instead of trying to pass data between the forms, which is a pain, I made a table in Access which only has 1 field in 1 record. Then I store the value in there using DoCmd.RunSQL, and retrieve it from the other form using DLOOKUP().