Vba in access. Filtering query - vba

I am having a issue with vba in access. I wrote a code to filter a form and generate a report via button. Now when I run the query , it will only filter and generate the report if I put the report on the same page as the form. Is there a way to make it so that my report will be created seperately?
Private Sub Search_Click()
Dim strWhere As String
Dim i As Variant
Dim varItem As Variant
Dim strDelim As String
If Nz(Me.txtFullName, "") <> "" Then
strWhere = strWhere & "FullName Like '*" & Replace(Me.txtFullName, "'", "''") & "*' AND "
End If
If Nz(Me.txtCivil, "") <> "" Then
strWhere = strWhere & "CivilService_Status Like '*" & Replace(Me.txtCivil, "'", "''") & "*' AND "
End If
If Nz(Me.txtOffice, "") <> "" Then
strWhere = strWhere & "Office_Title Like '*" & Replace(Me.txtOffice, "'", "''") & "*' AND "
End If
If Nz(Me.cboPosition, "") <> "" Then
strWhere = strWhere & "[Full Time],[Part Time] = '" & Me.cboPosition.Value & "' AND "
End If
If Nz(Me.txtStartdate, "") <> "" Then
strWhere = strWhere & "AgencyStart_Date Like '*" & Replace(Me.txtStartdate, "'", "''") & "*' AND "
End If
For Each i In Me.lstBureau.ItemsSelected 'listbox
If Nz(Me.txtdivision, "") <> "" Then
strWhere = strWhere & "Division Like '*" & Replace(Me.txtdivision, "'", "''") & "*' AND "
End If
Next i
If strWhere <> "" Then
strWhere = Left(strWhere, Len(strWhere) - 5)
Report_Onboard_summary.Filter = strWhere
Report_Onboard_summary.FilterOn = True
'DoCmd.OpenReport "Onboard_summary", acViewReport, strWhere
Else
Report_Onboard_summary.Filter = ""
Report_Onboard_summary.FilterOn = False
End If
End Sub
Any help would be greatly appreciated

I usually open reports with the OpenReport command.
DoCmd.OpenReport "Report_Onboard_summary", acViewPreview, WhereCondition:=strWhere
for previewing, or
DoCmd.OpenReport "Report_Onboard_summary", acViewNormal, WhereCondition:=strWhere
for immediate printing.
This assumes that the report is created as a Report object stored separately in the database, not a Form object. You said that you stored it on the same page, this leads me to believe that you have a sub-Form and not an actual Report.

Related

Access VBA for checkboxes

I'm currently working on a project that gave me a lot of raw data I am trying to filter in a continuous form. I have one field that has a possibility of five letters (GRUBW) with each multiple letters per entry. The goal is to filter the form with a checkbox for each letter. Am I missing something or going about this the wrong way?
Below is the coding I'm attempting to use and when trying to debug I keep getting an error of an undefined variable. As you can see in the coding, I have a lot of ways I want to filter. I have a feeling that I'm missing some double quotes somewhere or my understanding of the way checkboxes are to be coded is totally wrong. I've only been working with VBA for a few months, so I may be out of my depth here. Thanks.
Option Compare Database
Option Explicit
Private Sub CBTN_CLICK()
Dim ctl As Control
For Each ctl In Me.Section(acHeader).Controls
Select Case ctl.ControlType
Case acTextBox, acCheckBox
ctl.Value = Null
End Select
Next
Me.FilterOn = False
End Sub
Private Sub requeryform()
Dim strwhere As String
Dim lnglen As Long
If Not IsNull([Forms]![sbx]!CTB) Then
strwhere = strwhere & "([CARD] LIKE ""*" & [Forms]![sbx]!CTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!OTB) Then
strwhere = strwhere & "([ORACLE] LIKE ""*" & [Forms]![sbx]!OTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!ARTB) Then
strwhere = strwhere & "([ORACLE] LIKE ""*" & [Forms]![sbx]!OTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!TTB) Then
strwhere = strwhere & "([ctype] LIKE ""*" & [Forms]![sbx]!TTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!CTTB) Then
strwhere = strwhere & "([ORACLE] LIKE ""*" & [Forms]![sbx]!OTB & "*"") AND "
End If
If Me.RC = True Then
strwhere = strwhere & "([color] LIKE ""*" & R & "*"") AND "
End If
If Me.UC = True Then
strwhere = strwhere & "([color] LIKE ""*" & u & "*"") AND "
End If
If Me.BC = True Then
strwhere = strwhere & "([color] LIKE ""*" & b & "*"") AND "
End If
If Me.GC = True Then
strwhere = strwhere & "([color] LIKE ""*" & g & "*"") AND "
End If
If Me.WC = True Then
strwhere = strwhere & "([color] LIKE ""*" & w & "*"") AND "
End If
lng = Len(strwhere) - 5
If lnglen <= 0 Then
Else
strwhere = Left$(strwhere, lnglen)
Debug.Print strwhere
Me.Filter = strwhere
Me.FilterOn = True
End If
End Sub
Private Sub STBN_CLICK()
requeryform
End Sub
Undefined variable error is due to not enclosing literal text within quote marks and using lng instead of lnglen. Consider revised code:
Dim strGRUBW As String
If Not IsNull([Forms]![sbx]!CTB) Then
strwhere = strwhere & "([CARD] LIKE ""*" & [Forms]![sbx]!CTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!OTB & [Forms]![sbx]!CTTB & [Forms]![sbx]!ARTB) Then
strwhere = strwhere & "([ORACLE] LIKE ""*" & [Forms]![sbx]!OTB & "*"") AND "
End If
If Not IsNull([Forms]![sbx]!TTB) Then
strwhere = strwhere & "([ctype] LIKE ""*" & [Forms]![sbx]!TTB & "*"") AND "
End If
With Me
strGRUBW = IIf(.RC, "r,", "") & IIf(.UC, "u,", "") & IIf(.BC, "b,", "") & IIf(.GC, "g,", "") & IIf(.WC, "w,", "")
End With
If strGRUBW <> "" Then strwhere = strwhere & "[color] LIKE '*[" & strGRUBW & "*]' AND "
lnglen = Len(strwhere) - 5

VBA/SQL issue access

I keep recieving compiling errors saying that txtlln in the where line cannot be found. I am fairly new to SQL/VBA so I am not sure I am using the correct expressions to have this work.
Private Sub btnlledit_Click()
Dim strSQL As String
SQL = "UPDATE tblll " & _
"SET [Component/Product] = '" & Forms!frmaddll!txtllcomponent & "',[HN] = '" & Forms!frmaddll!txtllhn & "' " & _
"WHERE [LLN] = '" & Forms!frmaddll!txtlln.value & "';"
debug.print sql
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
DoCmd.Requery
Me.Refresh
End Sub
You seem to have a few issues swith your string concatenation.
Private Sub btnlledit_Click()
Dim strSQL As String
SQL = "UPDATE tblll " & _
"SET [Component/Product] = '" & Forms!frmaddll!txtllcomponent & "' " & _
"WHERE [LLN] = '" & Forms!frmaddll!txtlln.value & "';"
debug.print sql
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
DoCmd.Requery
Me.Refresh
End Sub

MS Access vba export form results to excel with where condition

I have a table that has a lot of fields and then a form that takes only a few of those fields. I have a search button on the form where you can select certain records. Is there a way in VBA to export the results from the form but include all the fields from the table.
Here is my attempt from some code that I found:
Private Sub Command49_Click()
Dim strWhere As String
Dim strFile As String
Const strcStub = "SELECT * FROM tblMaster " & vbCrLf
Const strcTail = "ORDER BY ID;"
Const strcExportQuery = "Query1" 'Name of the query for exports.
'Keyword
If Nz(Me.tKW, "") <> "" Then
strWhere = strWhere & "[iavmtitle] Like '*" & Replace(Me.tKW, " '", "''") & "*' AND "
End If
'Release Date From
If Nz(Me.tRF, "") <> "" Then
strWhere = strWhere & "[releaseDate] between " & "#" & Me.tRF & "# AND #" & Me.tRT & "#" & " AND "
End If
'Expliots
If Nz(Me.cmbExploits, "") <> "" Then
strWhere = strWhere & "[knownExploits] = '" & Me.cmbExploits & "' AND "
End If
'Incidents
If Nz(Me.cmdIncidents, "") <> "" Then
strWhere = strWhere & "[knownDodIncidents] = '" & Me.cmdIncidents & "' AND "
End If
'Release Date From
If Nz(Me.txtSaveSend, "") <> "" Then
strWhere = strWhere & "[lastSaved] > " & "#" & Me.txtSaveSend & "#" & " AND "
End If
If strWhere <> "" Then
strWhere = Left(strWhere, Len(strWhere) - 5) 'Remove the extra AND
Me.Filter = strWhere
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
If Me.FilterOn Then
strWhere = "WHERE " & Me.Filter & vbCrLf
End If
CurrentDb.QueryDefs(strcExportQuery).SQL = strcStub & strWhere & strcTail
strFile = "C:\Data\MyExport.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
strcExportQuery, strFile
End Sub

Form with multi-criteria searches - uses strings and filters

I have a search form with blank fields tied to a table, four criteria search boxes, and a button to take the input from the search boxes, search the table, and populate the results on the form's blank fields.
As of now, it works as long as all four criteria boxes aren't null.
I used filters to achieve this, and here's the code that works as long as all four boxes are not empty. (My criteria boxes are as follows: a textbox called "Keyword" and three combo boxes called HRCombo, BuildingCombo, and RoomCombo, and the fields they're tied to are as follows: "Item Description" "HR Holder" "Building" "Room") My first line "Me.Filter = ..." was broken up to make it easier to view.
Me.Filter = "[Item Description] Like " & Chr(34) & Me.Keyword & "*" & Chr(34) & "
AND [HR Holder] = '" & Me.HRCombo & "'" & " AND [Building] = '" & Me.BuildingCombo
& "'" & " AND [Room] = '" & Me.RoomCombo & "'"
Me.FilterOn = True
Me.Requery
I need it to be able to do the search no matter which combination of criteria boxes have input. Someone recommended using if statements to do the following:
Create four strings, one for each criteria box. Use 4 if statements to check if the box is null - if it is null, assign an asterisk to its string, and if its not null, assign the value I used for the above Me.Filter statement to each box's string. Then, use Me.Filter and concatenate the four strings at the end.
Here's the code I used for this, and, with my limited knowledge, I can't get it to work.
Dim StrA as String, StrB as String, StrC as String, StrD as String
If Me.Keyword is null then
StrA = "*"
else
StrA = [Item Description] Like " & Chr(34) & Me.Keyword & "*" & Chr(34)
End If
If Me.HRCombo is null then
StrB = "*"
else
StrB = [HR Holder] = '" & Me.HRCombo & "'"
End If
If Me.BuildingCombo is null then
StrC = "*"
else
StrC = [Building] = '" & Me.BuildingCombo & "'"
End If
If Me.RoomCombo is null then
StrD = "*"
else
StrD = [Room] = '" & Me.RoomCombo & "'"
End If
Me.Filter = "StrA & " AND "StrB & " AND "StrC &" AND "StrD"
Me.FilterOn = True
Me.Requery
Like I said, I have a limited knowledge, so I'm sure there's probably missing quotes and commas, or too many of them. Any ideas?
You're missing some important quotes and your logic to check for null is correct for SQL but not correct for VBA. I'm posting here what I believe is a cleaner way of doing this. Just be aware that you're not escaping single quotes that might be entered in your controls and neither am I in this code (except in the first one, just so you can see how to do it using the Replace function). That's a pretty important thing to do any time a single quote could possibly land up in one of the search/filtering controls.
Dim strWhere as String
If Nz(Me.Keyword, "") <> "" Then
strWhere = strWhere & "[Item Description] Like '*" & Replace(Me.Keyword, "'", "''") & "*' AND "
End If
If Nz(Me.HRCombo, "") <> "" Then
strWhere = strWhere & "[HR Holder] = '" & Me.HRCombo & "' AND "
End If
If Nz(Me.BuildingCombo, "") <> "" Then
strWhere = strWhere & "[Building] = '" & Me.BuildingCombo & "' AND "
End If
If Nz(Me.RoomCombo, "") <> "" Then
strWhere = strWhere & "[Room] = '" & Me.RoomCombo & "' AND "
End If
If strWhere <> "" Then
strWhere = Left(strWhere, Len(strWhere)-5) 'Remove the extra AND
Me.Filter = strWhere
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If

open a query based on many choices

hello i wrote this code to open a report based on a query
and this query is based on the toggle button
the problem is if i press one toggle button all work
but if i press more than toggle button in the same time it will not give me the right records or it will give me an empty report
this is the code
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tbl_Mouzakarat"
Dim p_7abes As String
Dim p_gharame As String
Dim p_done As String
Dim p_undone As String
Dim p_khoulasa As String
Dim p_mouzakara As String
Dim p_karar As String
Dim p_jaze2e As String
Dim p_lebanese As String
Dim p_foreign As String
Dim p_SQL_criteria As String
p_7abes = Trim(Me!text_7abes & " ")
p_gharame = Trim(Me!text_gharame & " ")
p_done = Trim(Me!text_done & " ")
p_undone = Trim(Me!text_undone & " ")
p_khoulasa = Trim(Me!text_khoulasa & " ")
p_mouzakara = Trim(Me!text_mouzakara & " ")
p_karar = Trim(Me!text_karar & " ")
p_jaze2e = Trim(Me!text_jaze2e & " ")
p_lebanese = Trim(Me!text_lebanese & " ")
p_foreign = Trim(Me!text_lebanese & " ")
If p_7abes <> "" Then
p_SQL_criteria = "[Punish]" & " LIKE '" & p_7abes & "'"
End If
If p_gharame <> "" Then
p_SQL_criteria = "[Punish]" & " LIKE '*" & p_gharame & "*'"
End If
If p_done <> "" Then
p_SQL_criteria = "[Status_Check]" & " LIKE '*" & p_done & "*'"
End If
If p_undone <> "" Then
p_SQL_criteria = "[Status_Check]" & " LIKE '*" & p_undone & "*'"
End If
If p_khoulasa <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_khoulasa & "*'"
End If
If p_mouzakara <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_mouzakara & "*'"
End If
If p_karar <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_karar & "*'"
End If
If p_jaze2e <> "" Then
p_SQL_criteria = "[Type]" & " LIKE '*" & p_jaze2e & "*'"
End If
If p_lebanese <> "" Then
p_SQL_criteria = "[Nationality]" & " LIKE '*" & p_lebanese & "*'"
End If
If p_foreign <> "" Then
p_SQL_criteria = "[Nationality]" & " NOT LIKE '*" & p_foreign & "*'"
End If
If Me.chk7abes.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkGharame.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkDone.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.ChkUndone.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKhoulasa.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkMouzakara.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKarar7abes.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
If Me.chkKararJaze2e.Value = True Then
DoCmd.RunSQL "INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
End If
DoCmd.OpenReport "rpt_Mouzakarat", acViewPreview
DoCmd.Close acForm, "frm_Printing"
will someone check the code please
You are overwriting p_SQL_criteria each time then running the same exact query each time no matter how many toggle buttons are pressed.
"INSERT INTO tbl_Mouzakarat select * from " & "[qry_Mouzakarat]" & " where " & p_SQL_criteria
This is always the exact same SQL statement for each of your DoCmd statements.
I'm not really sure how your toggle buttons interact with the parameters you are trying to create so it's hard to suggest solutions, but this is why your having the problem you are having.