Hide record from continuous form if checkbox is marked in query - sql

I have a continuous Form with records, the records contain a yes/no. So if the checkbox in the form is marked I want the record to be hidden from the form next time you open it, but it does not seem to work.
Private Sub Form_AfterUpdate()
If Me.Checkbox = False Then
Me.Checkbox.Visble = True
Else
Me.Checkbox.Visible = False
End If
End Sub

Add a Boolean field named Hidden having a DefaultValue of False in the table and, in the form, bind this field to the checkbox.
Then, when opening the form, filter on [Hidden] = False.

Related

How do I hide/unhide a subform based on the input from two comboboxes?

I have a mainform, called TrainingsSU
In it I am calling subform qry_TrainingSU (built from the query of the same name)
I have it set up so that the records auto populate the subform based on the two comboboxes in the main form.
But what I need to do is hide the subform, and have it display only when the two comboboxes are populated and records are loaded.
Here's the current VBA (of which I am not really even a novice at)
Private Sub cbo_EmployeeLookup_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_EmployeeLookup.Value = "" Then
Forms!qry_TrainingsSU.Visible = False
Else
Forms!qry_TrainingsSU = True
End If
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_TrainingName.Value = "" Then
Forms!qry_TrainingsSU = False
Else
Forms!qry_TrainingsSU.Visible = True
End If
End Sub
I found the general form of this code in another answer, here: MS Access: Hide and Unhide subform based on drop down in Main Form
However the code doesn't seem to be working for me.
Currently the subform is set to Visible:No
So nothing shows up at all.
If I change that, it doesn't disappear and the empty subform is still visible.
This wouldn't be a problem, except I need to use this form for another query and want to layer them over eachother when the second subform is ready to be used.
Later this form will be used to push an UPDATE SET to a table based on the different subforms.
Is there something that is obviously wrong with the code, or did I miss a setting somewhere?
You can try this:
Private Sub updateStates()
Me!qry_TrainingsSU.Form.Requery
If (Me.cbo_EmployeeLookup.Value <> "" AND Me.cbo_TrainingName.Value <> "") Then
Me!qry_TrainingsSU.Visible = True
Else
Me!qry_TrainingsSU.Visible = False
End If
End Sub
Private Sub cbo_EmployeeLookup_AfterUpdate()
updateStates
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
updateStates
End Sub
Forms!qry_TrainingsSU searches form opened as main form, not a subform.

Disable a subform while editing data in another subform

I have two subforms, the first is a list and the second is a detail record of the record selected in the first subform.
When I select the edit button in the detail subform I want to lock out the list form.
I tried: forms!master!subformList.enabled=false.
Note:
_D is the detailed subform.
_L is the list subform.
cmb_Edit is the edit/save button located on detail subform.
Private Sub EnableFutureForm(EF)
Select Case EF
Case 1 'SubForm Detail is Locked Down
FuturePaint (False)
Forms!FrmFutureWork!FrmFutureWork_D!cmb_Edit.Caption = "Edit"
Forms!FrmFutureWork!FrmFutureWork_L.Form.Enabled = True
Case 2 'SubForm Detail is Opened up
FuturePaint (True)
Forms!FrmFutureWork!FrmFutureWork_D!cmb_Edit.Caption = "Save"
Forms!FrmFutureWork!FrmFutureWork_L.Form.Enabled = False
Forms!FrmFutureWork!FrmFutureWork_D!cmb_TRMM.SetFocus
End Select
End Sub
I want the list subform to not respond to any click, while the Form_Click continues to work.
You can disable events by setting the forms property.
ForForm_Clickit is Form.OnClick property.
'disable event
Forms!FrmFutureWork!FrmFutureWork_L.Form.OnClick = vbNullString
'enable event
Forms!FrmFutureWork!FrmFutureWork_L.Form.OnClick = "[Event Procedure]"
Or you can add a check to the events-code:
Private Sub Form_Click()
If Forms!FrmFutureWork!FrmFutureWork_L.Enabled then
' Actions on subform-control (not the form!) enabled
Else
' Action on subform-control disabled
End IF
End Sub

Set a sub-form hidden field to visible, based on a check box status

C, Thank you for your input and encouragement! I have changed my form and script slightly, I am afraid I kept the if then statement as I am comfortable with the formatting. The script now works when the 'On Open'event runs.
Private Sub Form_Open(Cancel As Integer)
Me.ChkAlbumNotes.SetFocus
If Me.ChkAlbumNotes.Value = False Then
Me.lblAlbumNotes.Visible = False
Me.txtAlbumNotes.Visible = False
Me.btnAlbumNotes.Visible = True
Else
Me.lblAlbumNotes.Visible = True
Me.txtAlbumNotes.Visible = True
Me.btnAlbumNotes.Visible = False
End If
Me.TrackName.SetFocus
If Me.TrackName = " " Then
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub
This is fine when the form opens for the first time but I have a set of navigation buttons that are installed by the application as Macros. I cannot add my script to the On_Click event when the button is clicked, as On_Click is linked to the Macro. Is there a way to incorporate the script from the On_Load process to the pre-defined macro? Or can you suggest a neater way to achieve my requirements which are;
When the form opens,a check is made for the existence of a false value in the checkbox
if the check box is set to false, then the Notes Text Box and label are hidden and the notes button is visible.
If the check box has a true value, then the Notes text box and label are made visible and the button is hidden.
On completion of the test I check the field Track Name
if this is empty, I assume I am at the last record and give the Add New Record button the focus
If Track Name is not empty, then focus is set to Next Record button
when this button is clicked, the next record page opens and the process starts again.
Many Thanks
Mike
You should use the Form_Current event instead of Form_Open . This fires on starting the form (2 times) and everytime you move to another record.
Private Sub Form_Current()
Me.lblAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.txtAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.btnAlbumNotes.Visible = Not Me.ChkAlbumNotes.Value
If Me.TrackName = "" Then ' I suggest If Me.TrackName = " " being a typo and you want to check if empty ( that's why you should use vbNullString instead of "")
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub

Prevent VBA code from other workbook working with objects

I need to disable a control (list box in this case), so user cannot get data from other departments. I do have a macro that can enable/disable it, and ask for password.
What I need is to prevent semidecent user to write a macro for enabling this list box in another workbook and unlocking it this way. It is possible in some way prevent VBA code from other modules to work with this control? So only code written in that one sheet can enable or disable it.
Thank you
What you can do is lock your VBA project and change your "subs" into "function", this way, a more advanced user have no access to the macro you've written without unlocking the project.
(Functions are not visible from the "assign a macro" button in the developer ribbon where Subs are)
I tried UserInterFaceOnly - and I'm not sure at this moment what it is supposed to do but it is not doing what I wanted
To be more clear, I have 2 list boxes and several comboboxes on Sheet1. When I'm sending file to users, I want to lock ListBoxes, or rather .enabled = False
What I'm trying to do is to prevent other user to write in some other workbook
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Enabled = True
this line woudl enable the list box, and user can change values, and macro will genarate new SQL string for Departments user is not supposed to have access to. I need to have comboboxes enabled, so user can view different dates, and versions of plan for his department.
By accident I found a way I guess.
Within Sheet1 I have this code
Sub lstDepartment_change()
'checks first if lock checkbox is true/false, clears selection and disables listbox, _
if checkbox is locked and yet listbox is enabled and allows change
If Me.chkLock = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstdirector").Enabled = False
Call clearlistbox
End
ElseIf Me.chkLock = False Then
Call ControlsM.directorpopulation
End If
End Sub
Sub lstDirector_change()
'checks first if lock checkbox is true/false, clears selection and disables listbox, _
if checkbox is locked and yet listbox is enabled and allows change
If Me.chkLock = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstdirector").Enabled = False
Call clearlistbox
End
ElseIf Me.chkLock = False Then
End If
End Sub
Private Sub chkLock_Click()
'locking/unlocking with password inbox call
If Me.chkLock.Value = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstDirector").Enabled = False
ElseIf Me.chkLock.Value = False Then
If PasswordInputBoxM.InputBoxPassword("Enter password", "Password Required") = "****" Then
Me.ListBoxes("lstDepartment").Enabled = True
Me.ListBoxes("lstDirector").Enabled = True
Else
Me.chkLock.Value = True
MsgBox "Wrong Password"
Exit Sub
End If
End If
End Sub
If someone tries to unlock just listbox, and tries to change value, it will check the checkbox value, and clears the selected list and locks the listbox
If someone tries to unlock listbox and uncheck lock checkbox, it will automatically ask for password
Problem is, that someone can change selected value from other workbook. I do not know how to handle that
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Enabled = True 'handled
Workbooks("opexRequest").Sheets("Report").chkLock.value = False 'handled
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Selected(1) = True 'do not know how to prevent this

Make combo box record as table field name , MS Access

I have combo box in form, i want to have it's drop down value to be Table fields name.Table name is tblCap, and the field are Year1,Year2 and Year3.I want that in the Combo box to have dropdown list as Year1,Year2,Year3 and when that year is selected it should display related field in subform.
Any help would be appreciated.
Thank you.
This is much simpler than you may imagine:
Open the Property sheet of the combobox, tab Data
Set RowSourceType: Value List
Set RowSource: "Year1";"Year2";"Year3"
To display the selected value (field name) in a subform, use this expression:
=[Parent]![NameOfYourCombobox]
To display, in a textbox on the main form, the value of the selected field name of the subform:
=[NameOfYourSubformControl].[Form]([NameOfYourCombobox])
or (?) on the subform:
=[Parent]([Parent]![NameOfYourCombobox])
Update after comments.
Once the RowSource has been set-up as per #Gustav answer, in order to hide/unhide the TextBox controls according to the value of ComboBox you need to set their Visible property to True/False.
On the Events tab set the AfterUpdate of your ComboBox control to [Event Procedure] and on the code behind file set the following:
Private Sub YourComboControlName_AfterUpdate()
With Me
Select Case .YourComboBoxName.Value
Case "Year1":
With .YourSubformName.Form
.Your2000TextBoxControlName.ColumnHidden = false
.Your2001TextBoxControlName.ColumnHidden = true
.Your2002TextBoxControlName.ColumnHidden = true
End With
Case "Year2":
With .YourSubformName.Form
.Your2000TextBoxControlName.ColumnHidden = true
.Your2001TextBoxControlName.ColumnHidden = false
.Your2002TextBoxControlName.ColumnHidden = true
End With
Case "Year3":
With .YourSubformName.Form
.Your2000TextBoxControlName.ColumnHidden = true
.Your2001TextBoxControlName.ColumnHidden = true
.Your2002TextBoxControlName.ColumnHidden= false
End With
End Select
End With
End Sub