Docmd.Findrecord finds in textbox - ms-access

I'm using the docmd.Findrecord to find a record based on a value the user enter in a textbox. The problem is that Findrecord considers the textbox as a field and just keep finding the string in the field and not the records in the table I tricked the command by deleting the string in the textbox just before the search and replacing it back after the search. How can I fix this without this extra trick ? Here is the code I use:
Private Sub SearchRecord_Click()
Dim strwhat2find As String
If SearchTxt.Value = "" Then
Else
strwhat2find = SearchTxt.Value
SearchTxt.Value = ""
DoCmd.FindRecord strwhat2find, acAnywhere, False, acSearchAll, True, acAll, True
SearchTxt.Value = strwhat2find
End If
End Sub
End Sub

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

Hide and Unhide a field based on another field value

I have a form in Access 2010. this form included a combo box named "Type" related to a field in a table that named "Type". another combo box that is "Car_1" is related to a field in the same table that named "Car_1"
I want when the "Type" combo box is empty (null) then the combo box Car_1 should be invisible.
I wrote a code but does not work. actually, the "Car_1" combo box is visible always!
the code is:
Private Sub Form_Current()
If Me.Type.Value = "" Then
Me.Car_1.Visible = False
Else
Me.Car_1.Visible = True
End If
End Sub
any advice is appropriate ...
You could reduce it to a one-liner:
Private Sub Form_Current()
Me!Car_1.Visible = Not IsNull(Me!Type.Value)
End Sub
That's because you're not testing if Me.Type.Value is Null, you're testing if it's a zero-length string. Test if it's Null instead:
Private Sub Form_Current()
If IsNull(Me.Type.Value) Then
Me.Car_1.Visible = False
Else
Me.Car_1.Visible = True
End If
End Sub
Or account for both Null and a zero-length string:
Private Sub Form_Current()
If Me.Type.Value & vbNullString = vbNullString Then
Me.Car_1.Visible = False
Else
Me.Car_1.Visible = True
End If
End Sub

Check if unbound text box is equal to a string

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

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.

Access VBA - All checkboxes on form have been checked

I am relatively new to Access VBA and have a form that has around 30 checkboxes on it. When saving the form I want to ensure that all checkboxes have been ticked (set to true). The tickboxes have all got names SC1, SC2....SCN Is there a way to loop through each control and see if it has been set to true?
This is what I have tried but it doesnt seem to read the tickbox -
Private Sub Validate_Data(rstTop)
Dim n As Integer
Dim count As Integer
count = 0
For n = 1 To rstTop
If Form.Controls("SC" & n).Value = False Then
count = count + 1
End If
Next
If count <> 0 Then
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _
"More information Required"
Else
End If
End Sub
Give this a try, it worked for me.
Option Compare Database
Option Explicit
Private Function Validate_Data() As Boolean
Dim ctl As Control
Validate_Data = True 'initialize
For Each ctl In Me.Form.Controls
If ctl.ControlType = acCheckBox Then
If (ctl.Name Like "SC*") And (ctl.Value = False) Then
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _
"More information Required"
Validate_Data = False 'something isnt checked
Exit Function
End If
End If
Next ctl
End Function
Private Sub cmdGo_Click()
Validate_Data
End Sub