How do you create branching logic/skipping rules in a Microsoft Access form? - forms

I'm creating a very simple Access database with a table and corresponding form. For some questions on the form, I'd like to disable following questions, or hide them using branching logic.
For example, in my form I have a combobox question that asks: Are you a smoker? - "Yes", "No", "Prefer not to answer". The following question is: If yes, how often do you smoke? If they chose the answers "No" or "Prefer not to answer" for the first question, then I don't want the second question to be visible/enabled.
I've been searching for a way to do this and the easiest way seems to be setting the Visible property of textbox "If yes, how often do you smoke?" to No. After that, I go to question "Are you a smoker?" and go to Event Procedure in the Properties menu. This brings up a VBA code editor with the following text:
Option Compare Database
Private Sub Text969_Click()
End Sub
Private Sub Combo367_Click()
End Sub
I've been looking at different pages but I can't seem to get the code to work. For the particular question I'm asking, the name of the form is "Chronic Smokers" and the field for the first question is named "Are you a smoker." and the second question is named "If yes, how often." This is the code I've been trying and it doesn't work, but I can't seem to figure anything else out:
Option Compare Database
Private Sub Text969_Click()
End Sub
Private Sub Combo367_Click()
If Chronic Smokers.Combo367='Yes' then
Chronic Smokers.If yes, how often.Visible = True
Else
Chronic Smokers.If yes, how often.Visible = False
End if
End Sub
I think part of my problem is that I don't know the way the naming conventions or syntax for this code works. I have a feeling part of the problem is that I have blank spaces without underscores in the names If anybody can help me out with this, I'd really appreciate it!

VBA code could be as simple as Me.[how often].Visible = Me.Combo367 = "Yes". No If Then Else needed. Code will need to be in combobox AfterUpdate as well as form Current events, not Click event. Code will apply to ALL instances of control, not just the current record.
NOTE: use of Me qualifier is shorthand for form/report object code is behind.
If you prefer to use If Then Else, correct syntax would be:
With Me
If .Combo367 = "Yes" Then
.[how often].Visible = True
Else
.[how often].Visible = False
End if
End With
Suggest you explore Conditional Formatting. It allows to enable/disable textboxes and comboboxes dynamically per record without VBA. Controls will still be visible but 'greyed out'.
And yes, strongly advise not to use spaces nor punctuation/special characters (underscore is only exception) in naming convention nor reserved words as names. If you do, then enclose in [ ] as shown above. Better naming would be ChronicSmokers and HowOften. And give objects more meaningful names than the defaults assigned by Access.

Related

Controlling responses to questions following branching logic in a MS Access Form

So this question is related to a question I asked about earlier. I'm creating a form that has several questions with branching logic questions that follow. For example: "Do you smoke?" is followed by several questions such as "If yes, how often?" and "If yes, what cigarette brand?" As you can imagine, if the response to the first question is 'No' then it doesn't make sense for the second two questions to be enabled.
I've successfully been able to disable (gray out) the second two questions by opening up the Event Procedure in the Properties for the first question and used code that looked like this:
Private Sub blood_traceback_AfterUpdate()
If Me.Smoker = "Yes" Then
Me.HowOften.Enabled = True
Me.CigBrand.Enabled = True
Else
Me.HowOften.Enabled = False
Me.CigBrand.Enabled = False
End If
End Sub
This works beautifully except for one caveat. Suppose I enter 'Yes' in the form in response to "Are you a smoker?" and then proceed to enter the frequency and cigarette type for the next two questions. But then if I go back and change the response to 'No' or 'Prefer Not to Say' the next two questions are greyed out but they still have the responses recorded if I didn't delete them before changing the first question.
So what I'm wondering, is how would I set up the form to delete the responses in the questions "If Yes, How Often?" and "If yes, what cigarette brand?" if I changed my mind and switch the question "Are you a smoker?" to 'No' or 'Prefer Not to Say'.

issue with DLOOKUP syntax error for new database

I have a database with 4 tables but primarily it is a diversion table/form (DiversionT/F) and a payback table/form (PaybackT/F). Basically, when my program loans parts to other programs in my organization a diversion is created in DiversionT. When the program want to payback they create a payback entry in PaybackT.
I have an issue that I am confused about: On PaybackF the user enters an NSN (long part code) that they want to payback. Ultimately, I want some of the form to auto populate with part info based on the NSN entered. The info is stored in DiversionT. I have created a few text boxes on PaybackF to show the info. The first text box I am trying to autofill based on the NSN is a textbox called PartName. It should search DiversionT for that NSN and fill in the appropriate PartName on PaybackF. In the control Source for the box I typed:
=DLookUp("[PartName]","[DiversionT]", "[PartName]=" & Forms![PaybackF]!NSN)
I get the following error:
The expression you entered contains invalid syntax.
To be perfectly honest I don't really understand VBA yet (spent my life until now with C, C++, Java, and Python) but looked up the function on the Microsoft site.
If I am not going about this right, please also let me know?
When using DLookup to get data, if you are dealing with text strings, you need to ensure that you use single quotes to wrap the text in.
Also, I think that your logic in the DLookup is slightly wrong. I think that this is what you are after:
=DLookUp("[PartName]","[DiversionT]", "[NSN]='" & Me!NSN & "'")
Regards,
You can't enter VBA in to form properties like source control. VBA is only used in procedural code in the VBE. That being said, there is a DLOOKUP function available to form fields and the syntax is similar. This is a cause of your confusion.
EXAMPLE SYNTAX FOR NUMBERS:
=DLookUp("[PartName]","DiversionT", "[PartName]=" & [NSN])
EXAMPLE SYNTAX FOR STRINGS:
=DLookUp("[PartName]","DiversionT", "[PartName]='" & [NSN] & "'")
NOTE:
I can't tell what form you are on or if NSN is from a parent form or subform. Basically, NSN needs to be readable from the same form where that text field exists.

How to explicitly save or close a recordset when a form deactivates?

I apologize if this is too vague a question, but we're having the following problem:
We use a search form to find a record, then load it in a bound-control form where changes are made. Then we return to the search form and open another record. When we do that, the form's BeforeUpdate property fires a 3020 error, "Update without Add New or Edit" and stepping through the code it's referring to the FIRST opened record. This is strange because there is no explicit update call, but after much trial and error I think the error is thus:
Record #1 is opened via the form and changes are made. Record #2 is opened on that same form without closing the first recordset. Even though we now re-opened the form with the second record, Access still assumes we're editing record 1, i.e. that we're trying to edit 2 records concurrently. Same as though we had a datasheet form and we edited one row and then tried to edit a second row without saving the first. What I want to be able to do is have it automatically do an update on the first record when the form deactivates so loading a new record doesn't cause this conflict.
So the bottom line is this: **Is there a way, on say the form's Dirty or Deactivate event, that we can force the form's recordset to update and close ** before loading a second record?
I hope I made this clear enough, it's a complex problem, so any small guidance would help. Btw, you may ask, "Why are you running the same code to open the same form twice?" Good question! Hey it's an old and badly written app (the thing has GoSubs in it for Pete's sake) but I have no choice but to make the best of a bad situation.
EDIT: I was asked to post code, which is reasonable, but it's in several different places. So I have the data form, it has a "Search" button to go back to the search form for opening another record. The search button on the data entry form is:
Private Sub CommandSearch_Click()
On Error GoTo Err_CommandSearch_Click
DoCmd.OpenForm "Reference Form", acNormal 'This is the form that does the searching
Exit_CommandSearch_Click:
Exit Sub
Err_CommandSearch_Click:
MsgBox Err.Description
Resume Exit_CommandSearch_Click
End Sub
When a record is selected on that search form, then the new form is opened with this code. Now this is where it gets tricky. I'm not the original programmer, as I said I think it was written in Access 97 by someone after taking an hour to read "Access for Dummies" :). But it always looks like only one copy of the form is open, so maybe it's re-opening it?
Public Sub CommandLoadCase_Click()
Dim LoadUTUCaseNumber As String, lengthUTUCaseNumber As Integer
lengthUTUCaseNumber = InStr(Forms![Reference Form]![Reference Query SubForm]![UTU Case Number], "-") - 1
If (lengthUTUCaseNumber = 0) Then
LoadUTUCaseNumber = ""
Else
LoadUTUCaseNumber = Left$(Forms![Reference Form]![Reference Query SubForm]![UTU Case Number], lengthUTUCaseNumber)
End If
On Error Resume Next
Forms![Case Appeal_Add-On Form].UTUCaseKeyToFind = LoadUTUCaseNumber
DoCmd.OpenForm "Case Appeal_Add-On Form", acNormal, , , , , LoadUTUCaseNumber
'Case Appeal Add On Form referred to here is the data entry form I refer to above.
End Sub
Finally, the Error 3020 (Update without Add/Edit) is occurring after it executes this line on the data entry form. (I know the code is complicated which is why I didn't enter it at first).
Private Sub Form_BeforeUpdate(Cancel As Integer)
[UTU Claim Sequence Key] = [UTU Claim Alpha Sequence] & CStr([UTU Claim Numeric Sequence])
End Sub

ms-access linking each checkbox(true/false column) to run a individual code in forms

New Edition June 2, 2017
I'm creating a database for a preventative maintenance program for the equipment in my facility. We have about 70 different machines with individuals preventative maintenance instructions, and I've been able to create tables for machines with the instructions attached to them by using forms that will update the database. I created a main form that will filter which PM needs to be done based on the date that is searched that will filter through the machines in the database, access does this fine. The problem I'm currently having trouble with is finding a way to add checkboxes to this main form, and print off only the ones that are selected with a button. The plan we have is to have the mechanics to be able to print off the PM instructions, and use the print off to update the database. Is there a way this can be done with checkboxes or optionboxes?Here's a picture of the main form that doesn't have the checkbox field attached to it yetHere's a picture of the forms that will show the preventative maintenance instructions that we will be printed.
Your question is a little vague and doesn't provide any insight on what you might have tried (this would actually help us determine what you want to do). That said, based on what's given:
If you have a single checkbox and want some action to be performed when it is checked/unchecked.
Private Sub CheckboxName_Click()
If CheckboxName.Value = -1 Then
'Do something
Else
'Do something else (Else is optional)
End If
End Sub
If you have multiple checkboxes you want to confirm which are checked and which aren't when a button or something is pushed you use the Controls object to cycle through all the checkboxes.
Private Sub ButtonName_Click()
Dim ctrl as Control
Dim i as Integer
For Each ctrl In Me.Controls
If ctrl.ControlType = acCheckBox and ctrl.Value = -1
'Do something (e.g., Select Case ctrl.name)
End If
End Sub
Hopefully that at least points you in the right direction.

Word 2010 can Field added via QuickParts be given an ID and later referenced in document.Fields collection

I need to add a few fields to a Word 2010 DOTX template which are to be populated automatically with custom content at "run time" when the document is opened in a C# program using Word Interop services. I don't see any way to assign a unique name to "Ask" or "Fill-In" fields when adding them to the template via the QuickParts ribbon-menu option.
When I iterate the document.Fields collection in the C# program, I must know which field I'm referencing, so it can be assigned the correct value.
It seems things have changed between previous versions of Word and Word 2010. So, if you answer please make sure your answer applies to 2010. Don't assume that what used to work in previous versions works in 2010. Much appreciated, since I rarely work with Word and feel like a dolt when trying to figure out the ribbon menuing in 2010.
You are correct in that fields don't necessarily have a built-in way to uniquely distinguish themselves from other field instances (other than its index in the Fields collection). However, you can use the Field.Type property to test for wdFieldAsk or wdFieldFillIn . If this is not narrow enough to ID then you will need to parse your own unique identifier from the Field.Code. For example, you can construct your FILLIN field as:
{ FILLIN "Hello, World!" MYIDENTIFER }
when you iterate through your document.Fields collection just have a test for the identifier being in the string. EDIT: example:
For Each fld In ActiveDocument.Fields
If InStr("CARMODEL", fld.Code) <> 0 Then
''this is the carmodel field
End If
Next
Another alternative - seek your specific field with a Find.Text for "^d MYIDENTIFIER" (where ^d is expression for 'field code')
Let me know if this helps and expand on your question if any gaps.