Check if unbound text box is equal to a string - ms-access

In an access form, I have an unbound text box when contains a formula. The formula returns "Yes" or "No" based on whether the form has been completed (it ensure that all required boxes are filled in).
On the same form, I have a command button which I want to use to filter out completed forms. Basically, it is a button that save "Save Record" and when clicked, the form - if completed - should disappear (i.e. be filtered out).
Below is what I put in, but I don't know VBA.
Private Sub bttn_Save_Click()
Me.Filter = Me.MainFormComplete = Yes
Me.FilterOn = True
End Sub
How do I get check to see if my field (MainFormComplete) is equal to "Yes"?

Not quite sure what you are trying to do, but it could be:
Private Sub bttn_Save_Click()
Me.Filter = "MainFormComplete = 'Yes'"
Me.FilterOn = True
End Sub
or:
Private Sub bttn_Save_Click()
Me.Filter = "MainFormComplete <> 'Yes'"
Me.FilterOn = True
End Sub

or
if Me.MainFormComplete = Yes then
....
end if
the textbox might not be readable if text was simply entered and not saved. read the text:
if Me.MainFormComplete.Text = Yes then 'or "Yes" or True
....
end if

Related

Filter Form Based on 2 Values in the Same Text Box

I have a text box called [Status] that a user can populate using a combo box.
I want to filter my form based on two potential values in the text box value - Pipeline or Forecast. The filter is activated by a checkbox. Click check box get records where the [Status] is either Pipeline or Forecast.
The checkbox code is easy enough. I am having trouble getting Access to filter on two possible values in the same text box.
I've tried
Private Sub checkboxFilterActive_AfterUpdate()
If checkboxFilterActive = True Then
Me.Filter = "[status]='Pipeline'" And "[status]='forecast'" 'Filter Code
Me.FilterOn = True
Else
Me.FilterOn = False
End If
End Sub
This throws a type mismatch error.
I've tried
Private Sub checkboxFilterActive_AfterUpdate()
Dim strFilter As String
strFilter = "[Status]='Forecast'" & "'AND [Status] = 'Pipeline'"
If checkboxFilterActive = True Then
DoCmd.ApplyFilter , strFilter
Else
DoCmd.ShowAllRecords
End If
End Sub
This throws a Syntax error (missing operator) query expression error.
Any help is much appreciated.
Use OR instead of AND. Also, OR operator is literal text and needs to be within quote marks to build criteria string.
Me.Filter = "[status]='Pipeline' OR [status]='forecast'" 'Filter Code

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

Viewing combobox values from textbox values Microsoft Access 2007

I have a form ("Patient Complications") where users input data to a form using 2 cascading combo boxes ("catcombo" and "speccombo"). The combo boxes pull their values from a table ("Complications"). The table has a field for complication category (for example, infection, bleeding, mechanical). A second field lists specific complications (for example, if the complication category is "bleeding", the specific complication could be "GI" or "other"). The input from the combo boxes are concatenated and put into a text field on the form ("Complication"). That part works fine.
My form has several command buttons, including "edit" and "save" command buttons. Since I don't want users interacting with the "complication" field on the form, I have the field become not visible when the "edit" button is clicked. Instead, the 2 combo boxes become visible and allow users to input data. When "save" is selected, the reverse occurs. The two combo boxes become not visible, and the complication field becomes visible and locked.
Unfortunately, when "edit" is selected, the combo boxes are visible but show up blank (nothing is selected or displayed). I am trying to have the boxes display the inputs that were given to the text field. For example, if the text field displays "Bleeding, Other", I want the catcombo box to display "Bleeding" and the speccombo box display "Other". I have been unable to find anything to this effect. If anyone has any ideas it would be greatly appreciated.
The relevant code is included below. Please let me know if I can provide further clarification.
Private Sub catcombo_AfterUpdate()
Me.speccombo.Requery
End Sub
Private Sub speccombo_OnCurrent()
Dim strsql As String
strsql = "SELECT [Complications]![Specific Complication] FROM tblComplications" & _
"WHERE [Complication Category]=Forms![Patient Complications]![catcombo].value"
End Sub
Private Sub speccombo_AfterUpdate()
Forms![Patient Complications]![Complication] = Me.catcombo.Value & ", " & Me.speccombo.Value
End Sub
Private Sub save_Click()
Me.recordcount.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.recordcount
Me.Patient_Initials.Visible = False
Date_of_Complication.Locked = True
Complication.Visible = True
Complication.Locked = True
comments.Locked = True
catcombo.Visible = False
speccombo.Visible = False
Me.edit.Visible = True
Me.edit.SetFocus
Me.help.Visible = False
Me.save.Visible = False
Me.first.Visible = True
Me.next.Visible = True
Me.previous.Visible = True
Me.last.Visible = True
Me.addnew.Visible = True
Me.close.Visible = True
Me.cancel.Visible = False
End Sub
Private Sub edit_Click()
Me.recordcount.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.recordcount
Me.Patient_Initials.Visible = False
Date_of_Complication.Locked = False
Complication.Visible = False
comments.Locked = False
catcombo.Visible = True
catcombo.Locked = False
catcombo.Enabled = True
speccombo.Visible = True
speccombo.Locked = False
speccombo.Enabled = True
Me.cancel.Visible = True
Me.cancel.SetFocus
Me.edit.Visible = False
Me.help.Visible = True
Me.save.Visible = True
Me.first.Visible = False
Me.next.Visible = False
Me.previous.Visible = False
Me.last.Visible = False
Me.addnew.Visible = False
Me.close.Visible = False
End Sub
I figured it out. I added a field to the "Complications" table called "Input". This field contains the concatenated values that were put into the patient record (in the example above, the input field would be "Bleeding, Other"). The values in the Input field are the exact values that would be recorded in the "Complication" field on the Patient Complications form. I added the vba code below to the "Edit" command button code.
If Not IsNull(Forms![Patient Complications]![Complication]) Then
Dim comptext As String
Dim spectext As String
comptext = DLookup("[Complication Category]", "Complications", "Input = Forms![Patient Complications]![Complication]")
catcombo.Value = comptext
spectext = DLookup("[Specific Complication]", "Complications", "Input=Forms![Patient Complications]![Complication]")
speccombo.Value = spectext
End If
That solved my initial problem, but I then had an issue with the speccombo box displaying the last value it was given. For example, if speccombo.value="GI" when I click "edit", it would continue to display "GI" until another selection was made. This isn't a huge deal, just inconvenient. I wanted to make the speccombo box essentially zero out if catcombo was changed. I added the code below to fix that problem.
Private Sub catcombo_AfterUpdate()
Me.speccombo.Requery
Me.speccombo.Value = Null
End Sub
Please let me know if I need to provide clarification for anything.

Making a cell required based on combobox selection

Nw to the forum and may as well be new to programming.
I would like to make 'Invoice No.' text box required, ONLY when 'INVOICED' is selected in my combobox.
If your form is bound, you could add the validation in the before update event like this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.cboType = "INVOICED" And Nz(Me.InvoiceNo) = vbNullString Then
MsgBox "Invoice No. is required!", vbExclamation, Me.Caption
Me.InvoiceNo.SetFocus
Cancel = True
End If
End Sub
If unbound form, add the above statements (except Cancel = True) to your button click event.

Dirty event not firing on Access 2007 form

I have a bound pop up form with a Clear data button. It runs the Undo command.
The Form_Current event sets this button's enable property to False.
The Form_Dirty event sets this button's enable property to True.
The button's property is always set to False even after I enter data into the form. I think this is because my Form_Load event is populating two fields. One is passed from the main form as an OpenArgs, the other is the unique ID which is calculated based on the OpenArgs value.
Any suggestions as to how to get the Dirty event to activate under these circumstances? If not, then an alternative approach perhaps?
Thanks in advance.
Code below:
Private Sub cmdUndoChanges_Click()
CustID_temp = Me!CustID
FacNo_temp = Me!Unique_ID
DoCmd.RunCommand acCmdUndo
Me!CustID = CustID_temp
Me!Unique_ID = FacNo_temp
End Sub
Private Sub Form_Current()
Me!cmdUndoChanges.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Me!cmdUndoChanges.Enabled = True
End Sub
Private Sub Form_Load()
Dim test As Integer
Me!CustID = OpenArgs
test = DCount("Unique_ID", "tbl_Table2", "CustID = '" & Me!CustID & "'")
If IsNull(Me!UNIQUE_No) Then
test = test + 1
Me!Unique_ID = CustID & "-" & test
MsgBox "No Previous Data"
Else
' these will be turned back on when the user selects the
' modify data button or add new data button.
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acOptionGroup, acCheckBox
ctl.Locked = True
ctl.BackColor = 15066597
Box40.BackColor = 15066597
End Select
Next ctl
End If
MsgBox Me!Unique_ID
End Sub
Have you tried Form_BeforeInsert() instead of Form_Dirty()?
I found another case where the Form_Dirty event never fires, the hard way. I thought I'd add it here since this is the top search result for the "not firing" question.
If your Form_Load function does anything to change the value of an existing record, the form will be set to dirty immediately, apparently before the logic that fires the event begins to run. Because of that there will never be an event if other data on the form is changed.
We had a "smart" Save button that was disabled until Form_Dirty fired. After I added a fixup to Form_Load for old records with a junk field, editing those records no longer ever enabled the Save button.