Disallowing textbox editing if a ComboBox in the same Form is not filled (for each row individually) [duplicate] - vba

I am working on an Access 2010 form which where the user can select a record in the form header via a combobox and then build up elements related to the selected record in the detail section of the form. The default view of the form is set to continuous forms.
One of the controls in the detail section of the form is a combobox control. What I want to do is set the enabled property of a textbox on the same row of the form to false based upon a selection from the combobox. The code I am running is:
If Me.cboElementType = "Contract Shrink" Then
Me.txtElementID = ""
Me.txtElementID.Enabled = False
EndIf
This works, but it sets all instances of the textbox (txtElementID) to enabled = false. What I want to have happen is for the txtElementID to have a different enabled setting for each row in the detail section based upon the selection of the combobox cboElementType. So, if cboElementType = "Contract Shrink" on row 1 of the scrolling detail section, the txtElementID.Enabled would be set to false for that row. If cboElementType = "Cost Group" on row 2 of the scrolling detail section, then I'd like txtElementID.Enabled to be False on row 1 of the detail section and txtElementID.Enabled to be True on row 2.
Can anyone confirm or deny that this can be done and, if it can be done, how you would suggest it be accomplished? No matter which way this goes, thanks for help.

You cannot do it through VBA like you did, you need to use Conditional formatting, there you have an option to set the Enabled property.

try in Form_Current() event like
Private Sub Form_Current()
If Me.cboElementType = "Contract Shrink" Then
Me.txtElementID = ""
Me.txtElementID.Enabled = False
EndIf
end sub

I have been searching for days for how to access an individual record on continuous form and I am willing to say it is not possible, but I have a trick that I will share here. I have an investments database, user gets in and writes a proposal and then there is a meeting where either the project is approved (given money) or it is cancelled. However, there are many more states a project can be in, Proposal, Execution, etc. but only Approved/Cancelled can happen at this stage. I created a MockBool field in the Project table. I put that on the continuous form and when the form is closed I run this:
rs_frm=me.recordset
rs_frm.movefirst
while not(rs_frm.eof)
if rs_frm("MockBool") then
rs_frm.edit
rs_frm("ProcessStatus")="Cancelled"
rs_frm("MockBool")=false
rs_frm.update
end if
rs_frm.movenext
wend
I had days of searching and had an epiphany so I thought I would share.

Related

Button to check for missing values in a MainForm and Subforms in MS Access

New to Access (still), have only basic VBA skills.
I've got 3 subforms (subfrm_PackingSteps1 , subfrm_MetalDetection and subfrm_Weights - the first 2 are continuous and the other one is single form) within a main form (frm_daily_packing_record) that users go through and input data. The user should be able to input data in no particular order, and only at the end there would be a button to confirm that the user is ready to save this form.
I'd like to have this button on the main form that checks each control (in main form and subforms) for empty values. I found and adjusted a code to check the recordset of one of the continuous forms (see below), but I can't figure out:
how to include a code that checks each control instead of manually adding all of them (I've used a function before that utilises the Tag property, but can't add it to this)
how to keep the button in the main form while checking the controls/recordsets in the other subforms.
Thanks in advance.
Private Sub ConfirmBtn_Click()
Dim blnSuccess As Boolean
blnSuccess = True
Me.Recordset.MoveFirst
Do While Not Me.Recordset.EOF
If IsNull(Me.pc) Or IsNull(Me.InnerP) Then
blnSuccess = False
Exit Do
End If
Me.Recordset.MoveNext
Loop
If blnSuccess = True Then
MsgBox "You may proceed to save this record"
Else
MsgBox "You still have some empty fields to fill in!", vbCritical + vbOKOnly, "Empty Fields!"
End If
End Sub
I personally don't like this because it is too code-heavy and can easily break when the form is modified.
Instead may I suggest doing it slightly different, for each field have vba code .ondirty or .onupdate and do the validation checking right as the user is actually on that field.
This has 2 benefits, it is creating the validation when you are creating each form field and it STOPS the user right when their first mistake or bad data is entered. The last thing I want is to enter 50 fields, scroll to the bottom, submit fails then scroll back and try to find where the mistake was. If validation is done while the user it doing the actual data entry, when you get to the bottom you should have valid data and the submit should succeed without further testing.
Less code to debug and timely messages to the user if an error is caught!

Conditional visibility on MS Access Form - how to write in VBA or Macro

I have some very (very) basic MS Access knowledge. I'm trying to expand a bit into either VBA or macros as I'd like to put in some conditional visibility for my form. Basically, I have a checkbox. If it's checked, I want three or four more fields to pop up. Someone was able to point me to a basic VBA formula of if (this checkbox) = true then, (fieldx).visible = true, else, (fieldx).visibility = false, end if.
But I'm so new to this that I need more help and explanation. I tried putting it in but couldn't get it to work (no error message, just nothing changed at all).
Specific questions:
-Does this formula seem right?
-If I want multiple fields to be visible, can I combine them into one formula or should I create a new "if" statement for all?
-Where do I enter this code? I'm running the Office 365 version. For all I know, I'm not even putting it in the right place.
-How do I determine the field names to replace the (this checkbox) and (fieldx) in the formula? I tried entering the name I title the fields as, but with the spaces in the name I got an error message, and without the spaces nothing happened. Is there a specific naming convention to turn the field names into formula-appropriate titles? Is the name listed somewhere?
-Once I get the formula entered, is there something I have to do to get it to run/take effect? I tried saving, closing and reopening with no changes.
-Is this the best way to go about this?
If there's anything else you think I should know, I would love to hear it - but please keep in mind I'm very new to this so if you could keep it at "dummy" or ELI5 levels of explanation, I'd appreciate it!
after creating a form with 4 textboxes and a checkbox put the form in design mode (lower right corner has design mode selected, select a textbox and hit property sheet on the ribbon (or f4).
On the property sheet note the visible property. set the visible property to false. Now the textbox will be invisible when the form starts.
Tip you can select all the textboxes at the same time and set their properties all at once.
Every control on the form and even the various parts of the form have properties you can set and play with. For instance you can give any name you want to any control. On the property sheet go to the other tab and set the name property.
Tip: choose a name you you will remember without having to look it up and describes the controls function.
Next select the checkbox (not the checkbox's label). On the property sheet go to the event tab and select the on click event. hit the ellipsis and choose code builder. Access is Event Driven. We want the textboxes to appear when the checkbox is selected so we put that code in the checkbox click event.
after choosing code builder we get the code window where we can browse among all the events for all our forms. for now all you should see is something like:
Private Sub mycheckbox_Click()
End Sub
So insert some code to handle the checkboxes like:
Private Sub mycheckbox_Click()
If mycheckbox = True Then
txtbox1.Visible = True
txtbox2.Visible = True
txtbox3.Visible = True
txtbox4.Visible = True
Else
txtbox1.Visible = False
txtbox2.Visible = False
txtbox3.Visible = False
txtbox4.Visible = False
End If
End Sub
now when the checkbox is not checked no textboxes are visible.
but when the checkbox is checked they appear

Getting MS-Access form to save invisible combo boxes as a null or 0 value in query and table

I don't know that how I have built my form is necessarily the best way that I can do it, but it was the way that I could get it to work, at least partially. I have built a form in ms-access 2007 that uses vba to either hide or make available certain combo boxes. The first choice and the one on which the rest of the form is based is a yes/no option, being that either the customer requires outside services for their job or not. Once that is selected the user can then choose from the outside service options(Which are the combo boxes, either visible or no based on the first choice). So this is where the problem comes in, I have code written so that if the user chooses no in the very first box the rest of the boxes are made invisible. However if the user chooses yes they must then choose values, again yes or no to either retain or remove other options for the remainder of the form.
What I am looking to do is to make it so that when the user returns to the form what choices they made are still there. So if they chose no then the form would basically be blank and if they had said yes initially than that answer along with only the other choices they made would be available.
What I am currently using is a simple if-then statement to make the boxes either visible or not.
Private Sub Combo36_AfterUpdate()
If Combo36.Value = "No" Then Me.Combo18.Visible = False
If Combo36.Value = "Yes" Then Me.Combo18.Visible = True
If Combo36.Value = "No" Then Me.Combo20.Visible = False
If Combo36.Value = "Yes" Then Me.Combo20.Visible = True
End Sub
Obviously I am not experienced with access and have stumbling my way through it. I am sorry if any of what I have said above is confusing. If clarity is needed please let me know.
Well for a start, "Flase" should be updated to "False".
Instead of storing and then repopulating the selected values it might be easier to turn the visibility of the whole form true/false based on selection which would keep the last values the user selected.
For showing visibility of the controls try:
Private Sub Combo36_Change()
If Me.Combo36.Value = "No" Then
Me.Combo18.Visible = False
Me.Combo20.Visible = False
ElseIf Me.Combo36.Value = "Yes" Then
Me.Combo18.Visible = True
Me.Combo20.Visible = True
End If
End Sub
Your initial code is okay, to be able to have the form retain the choices then copy the code you have under the Combo36_AfterUpdate() event into the Private Sub Form_Current() event. This will do it.

How to use VBA to show / hide multiple combinations of tables in MS Word? - explained further within

I have a word 2010 document with some basic VBA code attached to a number checkboxs that show / hide a sections throughout the document.
There are a number of these throughout the document however they all share the same basic code which appears to be working well enough (example below)
Private Sub PlanningBox_Click()
If PlanningBox.Value = False Then
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = False
End If
End Sub
The issue i am having is that some of these sections then have checkboxes within them that show / hide further section which is fine, however if the user unchecks the first initial checkbox (hiding all sections) then re-checks it, it will open up the initial section again but not the ticked subsections - in order to do this they have to uncheck/check the subsection again. I know this seems minor but i need to ensure the form is as fluid and user acessible as possible..
For example let's say there is a single row table with the text 'Have you spoken to anyone for feedback?' (checkbox1) - the user will tick the check box which will unhide a section of text below giving directions, asking some questions, general blurb etc. At the bottom of this is another question 'Who did you speak to?" - and then multiple checkboxes 'mother','father', 'Child' (checkbox A,B,C,etc). Checking any of these boxes opens up a table with additional questions for each one selected, the user can check any number / combination of the checkboxes.
Now if the user unchecks the initial checkbox 1. all sections will be hidden no problem, but then if they then check it to reveal the section again the mother, father etc boxes remain ticked but will not reveal the sections without needing to be unticked/ticked again. Is there a way so that ticking the intial box will also reveal any previously unhidden sections again?
My knowledge of VBA is very limited, i have considered using If + ElseIF statements but my understanding is that i would need an ElseIf for every potential combination. Is there a more sophisticated way around this at all?
Hopefully i have articulated this well enough but happy to provide further information. Thank you for any assistance given
You could create a common routine that evaluates every checkbox and sets the corresponding section accordingly. Call that one routine whenever a checkbox is clicked.
Private Sub PlanningBox_Click()
SetRangeVisibility
End Sub
Private Sub SomeOtherBox_Click()
SetRangeVisibility
End Sub
Public Sub SetRangeVisibility()
If PlanningBox.Value = False Then
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = False
End If
If SomeOtherBox.Value = False Then
ActiveDocument.Bookmarks("SomeOtherRange").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("SomeOtherRange").Range.Font.Hidden = False
End If
' Etc...
End Sub
Further, if you use an array of check boxes you'd only need to write a single SomeBox_Click event procedure. Here is some information on Control Arrays in VBA.

navigation subform requery stopped working

I've built an unbound form which allows a user to select from two combo boxes that contain related information (Zone and Watershed Unit -- each Zone contains multiple watershed units) in order to see what regulations apply to each. Based on these selections (stored in txtZone and txtWU on the main form) a subform would show the existing regulations (sfrmRegsbyZWU based on qryRegsbyZWU). This serves as a reference for the user, who picks a new regulation to add from another subform, clicks a button, and the selection is added to the regulations for the Zone/Watershed Unit selected. I had this successfully built with a workaround in the OnCurrent event of the subform (which was undesirable because the user couldn't click on a record and delete it) and everything worked until I added inserted code to change the query definitions for the query which is the basis for the subform as opposed to having it in the "On current" event of the subform.
At that point the requery syntax in the main form which had previously worked stopped working. This is in all embedded in a navigation form in Access 2010, so the previously working syntax was:
Forms!frmNav!NavigationSubform.Form.sfrmRegsbyZWU.Requery
I've tried every permutation of Requery I can think of, and I can not get it working. The underlying query has been changed, but the form doesn't update to reflect it. Can anyone explain to me what has gone wrong, and how to fix it? The code (attached to the _After Update() event of the combo boxes) is:
Private Sub cboZone_AfterUpdate()
'Changes WU combo box as well as the underlying text boxes
Me.cboWU.SetFocus
Me.cboWU = ""
'Blank out other selections
Me.txtWU.SetFocus
Me.txtWU = ""
'Update text box with combobox selection
Me.txtZone.SetFocus
Me.txtZone = Me!cboZone.Column(0)
'Change the query underlying the Existing Regs panel (frmRegsbyZWU, qryRegsbyZWU) to reflect selection
Dim strZSQL As String
Set qdfZ = CurrentDb().QueryDefs("qryRegsbyZWU")
Dim myZVar As Variant
myZVar = Forms!frmNav!NavigationSubform.Form.txtZone
'MsgBox (myZVar)
If IsNull(myZVar) Then
strZSQL = "SELECT * FROM tblRegulations WHERE FALSE"
Else
strZSQL = "SELECT * FROM tblRegulations WHERE Zone_No=" & Forms!frmNav!NavigationSubform.Form.txtZone
End If
'MsgBox (strZSQL)
qdfZ.SQL = strZSQL
'DoCmd.OpenQuery ("qryRegsbyZWU")
Forms!frmNav!NavigationSubform.Form.sfrmRegsbyZWU.Requery
Thanks in advance for any help!