Hide records that are not matching search box filter - ms-access

I am using MS Access 2010.
I have a table named "Dashboard"
I have a field named "Client Name"
In my tab named "Search Form" I have a subform called "Dashboard_subform"
The code is working corectly but I have two minor issues:
1. When combo box is empty and i press enter then Run-time error "Extra ) in query expression '([Client Name] = )'.
2. All records are showing up when the combo box is empty. I want no records to show up until i search the particular field data is typed in the combo box. Only records matching the data typed in the combo box must be listed.
Private Sub SearchClientRecords_AfterUpdate()
Me.SearchClientRecords.AutoExpand = False
Dim myclient As String
myclient = "Select * from [Dashboard] where ([Client Name] = " & Me.SearchClientRecords & ")"
Me.Dashboard_subform.Form.RecordSource = myclient
Me.Dashboard_subform.Form.Requery
End Sub
I assumed "AutoExpand" False will hide all records until a entry is typed in the combo box.
Also not sure why the run time error is showing up because the closed paranthesis is needed in this case.

Try using this code (Assuming client name is never empty field and always has name)
Private Sub SearchClientRecords_AfterUpdate()
Dim myclient As String
If Not IsNull(SearchClientRecords) Then
myclient = "Select * from [Dashboard] where ([Client Name] = " & Me.SearchClientRecords & ")"
Else
myclient = "Select * from [Dashboard] where [Client Name] IS NULL"
End If
Me.Dashboard_subform.Form.RecordSource = myclient
Me.Dashboard_subform.Form.Requery
End Sub

Related

MS Access Double click on Listbox run time error 3075

I have a listbox that is bound to a table (tblTransferDetail). The Primary Key is 'TransNum' (type INT) and the listbox is bound to this as the first column.
The double click event of the listbox (lstScannedItems) executes the below code:
CurrentDb.Execute "DELETE FROM tblTransferDetail WHERE TransNum = " & Me.lstScannedItems, dbSeeChanges
However, when I double click on a record in the listbox I keep receiving 'VBA Runtime error 3075' and the above line is highlighted.
Where am I going wrong?
You could simply iterate through .ItemsSelected and then execute the query with the itemdata for each selected item.
Dim v As Variant
For Each v In Me.lstScannedItems.ItemsSelected
CurrentDb.Execute "DELETE FROM tblTransferDetail WHERE TransNum = " & Me.lstScannedItems.ItemData(v)
Next
Me.lstScannedItems will work only when the value is bound column. if you need to use the selection and then go through the selection collection to find out which one is selected.
Dim opValue As String
Dim x As Long
With Me.lstScannedItems
For x = 0 To .ListCount
If .Selected(x) = True Then
opValue = .List(x)
Exit For
End If
Next x
End With
CurrentDb.Execute "DELETE FROM tblTransferDetail WHERE TransNum = " & opValue, dbSeeChanges

MS Access: Subform Datasheet - Variable RecordSources

I have a subform datasheet whose RecordSource can be variable. My database constructs an SQL query based on user selections (a different collection of columns with each query). The resulting query is intended to be the RecordSource for a datasheet on a subform. (Read-only view for user)
Problem:
The various queries produce the desired results when run on their own
Setting a resulting query as the datasheet's RecordSource does not produce any result (no columns/rows)
I suspect I need to insert the query's attributes into the subform in order to see any results (much like "Add Existing Fields" in the menu strip).
Question:
Any pointers to get me off square one?
Thank you!
Remove the datasheet form from your subform object and leave the source object property empty.
Create a new query (sql doesn't matter) and name it qryTemp (or whatever you like).
Then whenever you want to set the source for the subform, use this
CurrentDb.QueryDefs("qryTemp").SQL = "<your new sql here>"
<yoursubformobject>.SourceObject = "Query.qryTemp".
Here is an example I use to populate a sub-form:
Private Sub cmdFind_DisplayName_Click()
Dim dbs As Database, rstPatient As Recordset
Dim txtDisplayName, strQuote As String
strQuote = Chr$(34)
On Error GoTo ErrorHandler
Me.OrderBy = "DISPLAYNAME"
Me.OrderByOn = True
Set dbs = CurrentDb
Set rstPatient = Me.RecordsetClone
txtDisplayName = Trim(InputBox("Please Enter Patient Name ", "Patient Find By Name"))
txtDisplayName = UCase(txtDisplayName) & "*"
If IsNull(txtDisplayName) Then
MsgBox ("No Patient Name Entered - Please Enter a Valid Patient Name")
Else
rstPatient.FindFirst "[DISPLAYNAME] Like " & strQuote & txtDisplayName & strQuote
If Not (rstPatient.NoMatch) Then
Me.Bookmark = rstPatient.Bookmark
Me.Refresh
Else
MsgBox ("Patient Not Found - Please Enter a New Patient Name")
End If
End If
GoTo Exit_cmdFind_Click
ErrorHandler:
MsgBox LTrim(RTrim(Me.NAME)) + "." + "Patient Find By Display Name - " + "Error: " + AccessError(Err.Number)
Exit_cmdFind_Click:
rstPatient.Close
Set dbs = Nothing
Set rstPatient = Nothing
End Sub

access combobox values not showing

I am trying to create a form with 3 combo boxes on which will run a report from existing data. The combo boxes will filter down based on the data chosen in the next level up combo box.
However I'm having issues with the Batch Number Combobox not populating it's list
Now this is my VBA script:
Private Sub Form_Load()
Me.cboDate.RowSource = ""
Me.cboBatchNo.RowSource = ""
End Sub
Private Sub cboBottleNo_AfterUpdate()
Dim sDateSource As String
sDateSource = "SELECT [tblNewCC].[Date] FROM [tblNewCC]" & _
" WHERE [tblNewCC].[BottleNo] = " & Me.cboBottleNo.Value
Me.cboDate.RowSource = sDateSource
Me.cboDate.Requery
End Sub
Private Sub cboDate_AfterUpdate()
Dim sBatchSource As String
sBatchSource = "SELECT [tblBatchTotals].[BatchNo] FROM [tblBatchTotals] INNER JOIN [tblNewCC] ON [tblBatchTotals].[RunNo]=[tblNewCC].[RunNo]" & _
" WHERE [tblNewCC].[BottleNo] = " & Me.cboBottleNo.Value & _
" AND [tblNewCC].[Date] = " & Me.cboDate.Value
Me.cboBatchNo.RowSource = sBatchSource
Me.cboBatchNo.Requery
End Sub
From what I can see this is working alright on the vba side as I can see it replacing the rowsource of the batch number combobox, and in datasheet view it is giving me results.
However the combobox isn't showing anything in it's list....
Unless I go in make a change and save the sql query again.
Any clues?
The last part of sBatchSource should be " AND [tblNewCC].[Date] = #" & Me.cboDate.Value & "#" As an aside, 'Date' is a terrible name for a field. It is a reserved word in Access and will eventually cause you problems. Here is a helpful link http://www.fontstuff.com/access/acctut15pfv.htm

MS Access combo box setting a record with two primary keys

I have a database with 2 primary keys, one for a LINE NUMBER and one for PHASE of construction. The reason for this is that we have projects that may use the same Line Number but must track several Phases of the project entirely seperatly. What I have is a combo box that will drive the record information on a form. This works fine, but now when I have more than one phase it will only bring up the line's first phase and not the other 4 phases. When something other than phas one is picked it results the first phase information.
Is there a way to tie a combo box with 2 fields to select the proper record based on both fields picked?
Or maybe I need to rething the way the form is brought up... Is there a better way to do this?
Code used to select the record:
Sub SetFilter()
Dim LSQL As String
LSQL = "select * from tblLineData_Horizon"
LSQL = LSQL & " where lineno = '" & cboSelected & "'"
Form_frmHorizon_sub.RecordSource = LSQL
End Sub
Private Sub cboSelected_AfterUpdate()
'Call subroutine to set filter based on selected Line Number
SetFilter
End Sub
Private Sub Form_Open(Cancel As Integer)
'Call subroutine to set filter based on selected Line Number
SetFilter
End Sub
A basic idea, but you'll most likely want to tweak the behaviour a bit and have some more checks. When the form loads, you only have the ability to select LineNo. When cbxLineNo has a value in it, it enables cbxPhaseNo for selection and upon selection, it changes the RecordSource of your subform.
Private Sub cbxLineNo_AfterUpdate()
If IsNull(cbxLineNo) Then
cbxPhaseNo.Enabled = False
Else
cbxPhaseNo.Enabled = True
cbxPhaseNo.RowSource = "SELECT PhaseNo FROM tblLineData_Horizon WHERE LineNo = " & cbxLineNo & ";"
End If
End Sub
Private Sub cbxPhaseNo_AfterUpdate()
If IsNull(cbxPhaseNo) = False And IsNull(cbxLineNo) = False Then
tblLineData_Horizon_sub.Form.RecordSource = "SELECT * FROM tblLineData_Horizon WHERE LineNo = " & cbxLineNo & " AND PhaseNo = " & cbxPhaseNo & ";"
End If
End Sub
Private Sub Form_Load()
cbxLineNo.Enabled = True
cbxPhaseNo.Enabled = False
cbxLineNo.RowSource = "SELECT LineNo FROM tblLineData_Horizon GROUP BY LineNo;"
End Sub
You question is a little unclear, but you can create a combobox with more than one column, then your select statement would be:
where lineno = '" & cboSelected.Column(0) & "' And otherfield='"& cboSelected.Column(1)&"'"
Go to the query behind your combobox by clicking into its RowSource property and clicking the Build button (...). Once you've added the columns you need, bring up the Properties for the query and set Unique Values to Yes, so that it doesn't repeat the combinations of the fields.
You will also need to change other properties of the combobox: 'Column Count' and 'Column Widths'.

MS access to view all record history data with the specific input field value but it is showing only first record of criteria

I tried to display a list of all requests from a specific dept of my table
So I created a form with all the tbl fields that I want to display on the form view of "details" section like this .
dept name Totalnum req# ticket
Then I have created a combo box with pre-defined values as 'depttest' field.
Then I used the following code on change value of field but form is displaying only the first record of the category and not showing all the records .... can some one please help me with this logic..
Option Compare Database
Option Explicit
'Set default record source of form
Const strsql = "SELECT tbl.dept,tbl.name,tbl.[Totalnum],tbl.[req#],tbl.[Ticket] FROM tbl"
Private Sub depttest_Change()
Dim strFilterSQL As String
strFilterSQL = strsql & " Where [dept] = 'me.depttest.value';"
Me.RecordSource = strFilterSQL
'DoCmd.RunSQL strFilterSQL
Me.Requery
End Sub
You are passing me.depttest.value as a string not a value. Try:
strFilterSQL = strsql & " Where [dept] = '" & me.depttest.value & "';"