Opening form with subforms with whereCondition - vba

I have a form with values dateFrom and dateTo.
On click of button i want to open a new form which contains two subforms. One of the subforms shows filtered records. It shows the records that have note_date in range between dateFrom and dateTo.
note_date is one of the columns in query which is Record Source in the filtered subform
So what doesnt work is the filter for one of the subforms.
Here is my code how i thought it would work
Overview_of_vacation_notes is the name of the form that contains two subforms
dtmFrom = Text56.Value 'start date
dtmTo = Text58.Value 'end date
Dim strCriteria As String
strCriteria = "[note_date] >= #" & Format(dtmFrom, "yyyy-mm-dd") & "# AND [note_date] <= #" & Format(dtmTo, "yyyy-mm-dd") & "#"
DoCmd.OpenForm "Overview_of_vacation_notes", whereCondition:=strCriteria
Is there a way i could do something like this?
DoCmd.OpenForm "Overview_of_vacation_notes", subformName.whereCondition:=strCriteria
Because my code doesn't work cause of the whereCondition using the strCriteria on the main form and not the subform

DoCmd.OpenForm "Overview_of_vacation_notes" is opening parent form. So, you can not apply where condition to subform to this line. You need to set filter criteria to subform and filter that subform to show filtered data. Try below codes.
dtmFrom = Text56.Value 'start date
dtmTo = Text58.Value 'end date
Dim strCriteria As String
strCriteria = "[note_date] >= #" & Format(dtmFrom, "yyyy-mm-dd") & "# AND [note_date] <= #" & Format(dtmTo, "yyyy-mm-dd") & "#"
DoCmd.OpenForm "Overview_of_vacation_notes"
Forms![Overview_of_vacation_notes]![YourSubform].Form.FilterOn = False 'Clear previous filter.
Forms![Overview_of_vacation_notes]![YourSubform].Form.Filter = strCriteria 'Set filter criteria
Forms![Overview_of_vacation_notes]![YourSubform].Form.FilterOn = True 'Apply filter.

Related

Filter between two dates MS Access

I have a form where i created two text boxes to choose the start and end date and a button to filter the table in the form by the dates chosen.
My vba code does not work. Can someone help?
This is the code:
Private Sub Command150_Click()
If IsNull(Me.Text153) And IsNull(Me.Text155) Then
Me.FilterOn = False
Else
Me.Filter = "[Datum] BETWEEN #" & Me.Text153 & "# AND #" & Me.Text155 & "#"
Me.FilterOn = True
End If
End Sub
Note: Text153 is start date, Text 155 is end date, Datum is the field in the table which is filtered.
The dates filtered should be >= Start date and <= End date.
Datum sounds German, so you may have to force a slash as the date separator:
Private Sub Command150_Click()
Dim Filter As String
If IsNull(Me.Text153) Or IsNull(Me.Text155) Then
Me.FilterOn = False
Else
Filter = "[Datum] BETWEEN #" & Format(Me.Text153, "yyyy\/mm\/dd") & "# AND #" & Format(Me.Text155, "yyyy\/mm\/dd") & "#"
' Print the filter string to the Immediate window (press Ctrl+G).
Debug.Print Filter
Me.Filter = Filter
Me.FilterOn = True
End If
End Sub

How to filter 3 text boxes and then run a report according to them in MS Access VBA

I have 3 combo boxes:
Company - cboCOMP - tblCOMPANY
Category - cboCAT - tblCATEGORY
FLEET NO - cboFLT - tblFLEET_NO
These (1&2) are then sorted(criteria) via cboCOMP with the row source of tblFLEET_SETUP
I have setup each combo box so that they filter into each other but have only managed to figure out how to do this according to the cboCOMP as long as it has a value selected.
Basically I want the combo boxes (1,2 & 3) to show their individual full list of options in the drop down even if the cboCOMP does not have a value selected but I then want the combo boxes to filter according to each individual combo box accordingly. This this possible and how would I do this?
Once I have selected the values I want to filter by I will click Run Report but everytime I do the only combo box that then gives me an error with regards to running the report is when I have selected a value for cboFLT. If I leave cboFLT blank but put a value in the other 2 combo boxes, the report runs fine.
Here is the vba code I am using for this…
Private Sub btnRUN_Click()
Dim vcomp As String
Dim vcat As String
Dim vflt As String
Dim filter As String
If Me.cboCOMP.Value <> "" Then
vcomp = "'" & Me.cboCOMP.Value & "'"
' MsgBox vcomp
filter = "COMPANY =" & vcomp & ""
' MsgBox filter
End If
'NEW IF STATEMENT
If Me.cboCAT.Value <> "" Then
vcat = "'" & Me.cboCAT.Value & "'"
If filter <> "" Then
filter = filter & " and "
' MsgBox filter
End If
filter = filter & "CATEGORY =" & vcat & ""
' MsgBox filter
End If
'NEW IF STATEMENT
If Me.cboFLT.Value <> "" Then
vflt = "'" & Me.cboFLT.Value & "'"
If filter <> "" Then
filter = filter & " and "
' MsgBox filter
End If
filter = filter & "FLEET NO =" & vflt & ""
' MsgBox filter
End If
DoCmd.OpenReport "rptQuick_Fuel_Report", acViewPreview, , filter
DoCmd.Close acForm, "Quick Fuel Lookup", acSaveNo
End Sub
The error code is "
Run-time error '3705': Syntax error (missing operator) in query
expression 'COMPANY = 'JB' and CATEGORY = 'SOAP' and FLEET NO = 'Q
16''.
When I click debug the error line in the code is:
DoCmd.OpenReport "rptQuick_Fuel_Report", acViewPreview, , filter
Field FLEET NO has a space. Object names with space or punctuation/special characters must be enclosed in [ ]: [FLEET NO]. Advise not to use those features in naming convention. – June7 yesterday

MS Access Link Criteria from Two Sources

I have a report that is opened and a filtered to a specific record by linking to piece of data. (Lot #)
Private Sub ServiceRequest_Click()
DoCmd.RunCommand acCmdSaveRecord
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "ServiceRequest"
stLinkCriteria = "[Lot_Number]=" & "'" & Me![Lot_Number] & "'"
DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
End Sub
What I need is an additional filter on the report to happen where [Trade] field is filtered to the selection on the originating form. [Trade] = "tradeselect.value"
I tried simply adding an additional stlinkCriteria, like this....
stLinkCriteria = "[Trade]=" & "'" & Me![TradeSelect] & "'"
but then nothing works. The form just opens on the first record, instead of being filtered to a particular one.
Any help greatly appreciated.
When combining two criteria, you need to do it just like an SQL statement (Field1 = "1" AND Field2 = "2")
For your case, you can just add the necessary text when adding the second criterium:
stLinkCriteria = stLinkCriteria & " AND [Trade]=" & "'" & Me![TradeSelect] & "'"

Searching a name in a subform table via vba using combo box

I am trying to make a search filter for names on a subform from a parent form using a combo box, I already managed to do it searching dates, Im trying to do it depending on a client name now... I have, but it doesnt work...
Me.subform.Form.Filter = "[Client]=& me.cboClientName&"
I manage to do the search by dates instruction like this....
Me.subform.Form.Filter = "[AppointDate]=#" & Format(Me.cbSelectDate, "yyyy-mm-dd") & "#"
You have to concat the string with the searched column and the value of the combobox and you have to apply the filter.
Dim strFilter as string
'first print what you did
strFilter = "[Client]=& me.cboClientName&"
Debug.Print "Your faulty filter: " & strFilter ' shown in immediate window
'now with concat
strFilter = "[Client]= '" & Me.cboClientName & "'" ' suround by quotes because I assume it's a string
Debug.Print "filter: " & strFilter
Me.subform.Form.Filter = strFilter
Me.subform.Form.FilterOn = true ' activate the Filter

Perform a search for a user input date that falls between two dates

I have been trying to create a search that pulls up all of the records with a user specified date that falls between the start and end dates of that record. I also want to user to be able to pull it up by "Property" unless they leave it blank.
I am very new to VBA as in started yesterday and this is the latest version that I could come up with:
Private Sub Command4_Click()
Dim strFilter As String
strFilter = [Start_Date] <= Format(Me.RateDate, "Short Date") _
And [End_Date] >= Format(Me.RateDate, "Short Date")
If Not IsNull(Me.Property) Then
strFilter = strFilter & " AND [Num_Code] = '" & Me.Property & "'"
End If
DoCmd.OpenReport "rpt_RatesAll", acViewPreview, , strFilter
End Sub
The most recent error message returns: Run-time error '2465': Microsoft Access can't find '|1' referred to in your expression.
Any help would be very appreciated!
strFilter = "[Start_Date] <= #" Me.RateDate & "# And [End_Date] >= #" & Me.RateDate & "#"
Should do it.
What you had is being taken as a literal string and not using the values you desired.