MS Access : Add Select All to combo box - vba

i tried this :
To add an (All) item to the list, return to Design view and select the combo box control. Then, replace the Row Source property with the following SQL statement:
SELECT City FROM Employees
UNION SELECT "(All)" FROM Employees;
from here : https://msdn.microsoft.com/en-us/library/aa140084%28v=office.10%29.aspx
My Sql statement looks like this :
SELECT LastName FROM Employee
UNION SELECT "(All)" FROM Employee;
I can see the (All) in my drop down,the problem is when I select the Option (All) i receive a blank page.
Am I missing something? Do I need some VBA Code ? This is my current Code:
Private Sub cboFilter2_AfterUpdate()
Dim myFilter As String
myFilter = "Select * from Employee_Filter_Query where ([LastName] = '" & cboFilter2 & "')"
Me.Employee_Filter_subform.Form.RecordSource = myFilter
Me.Employee_Filter_subform.Form.Requery
End Sub
Br,

You likely do not have a last name of "(All)" and SQL doesn't automatically know to select everything when you pick "(All)" form the combobox. You'll need to adapt your query:
"Select * from Employee_Filter_Query where ([LastName] = iif('" & cboFilter2 & "'='(All)',[LastName],'" & cboFilter2 & "'))"
WHat this will do is look for the combobox to say "(All)" and if it does then match [LastName] = [LastName] which will always be true and return all records, otherwise it will match [LastName] against your filter and act like normal.
An alternative is to set up like this:
If cboFinter2 = "(All)" Then
myFilter = "Select * from Employee_Filter_Query"
Else
myFilter = "Select * from Employee_Filter_Query where ([LastName] = '" & cboFilter2 & "')"
End If

Related

MS Access Dropdowns to filter through table

i have a big table, and have created a form that filters through that data.
However, at the moment, these filters do not work.
There are dropdown filters which correspond to columns and the idea is to choose a value from a specific column and get the rows with only the choosen value, also to filter few columns at once like that if neccessary - each box has mostly the same code, just the change of names.
Here is an example of one box code :
Private Sub klk_AfterUpdate()
Dim klkxas As String
klkxas = "Select * from daca where ([wdwqd] = '" & Me.cboLiefName & "') and ([sdcxsadc] = '" & Me.cboSRat & "') and ([Verantwortlicher] = '" & Me.cboVerant & "') and ([cxsacx] = '" & Me.cboLiefKat & "') and ([qxqxsa] = '" & Me.cboliefart & "') and ([sxsa] = '" & Me.cboLand & "') and ([sdxs] = '" & Me.cboAudErg & "')"
Me.Form.RecordSource = klkxas
Me.Form.Requery
End Sub
Can someone help me on how to change the code so it works?
Thanks in advance
If I were to guess, your issue is that even selecting one or more but not all comboboxes (i.e., leaving some comboboxes blank) results in no returned rows. Per your attempted query, this is not an error but valid result of SQL logic. Your users would need to correctly select values in all seven comboboxes to get any rows back.
One solution of this dynamic filtering problem is to use NZ (or IIF) to match each field to itself for non-selected comboboxes. Also, use a saved SQL query that points to form controls without any concatenation of VBA values and punctuation of quotes.
Below will return values if any one or all controls are selected. But do note after combobox selection, only rows with non-NULL across all fields will be returned. Be sure to adjust myForm to actual form name in query:
SQL (save as separate query and as recordsource to form)
SELECT *
FROM Lieferant
WHERE [Bezeichner] = NZ(Forms!myForm!cboLiefName, [Bezeichner])
AND [S_Rating] = NZ(Forms!myForm!cboSRat, [S_Rating])
AND [Verantwortlicher] = NZ(Forms!myForm!cboVerant, [Verantwortlicher])
AND [Lieferantenkategorie] = NZ(Forms!myForm!cboLiefKat, [Lieferantenkategorie])
AND [Lieferantenart] = NZ(Forms!myForm!cboliefart, [Lieferantenart])
AND [Land] = NZ(Forms!myForm!cboLand, [Land])
AND [Audit_Ergebnis] = NZ(Forms!myForm!cboAudErg, [Audit_Ergebnis])
VBA (single line!)
Private Sub cboSRat_AfterUpdate()
Me.Form.Requery
End Sub
If needing to capture rows with potential NULLs, integrate OR conditions:
SQL
SELECT *
FROM Lieferant
WHERE ([Bezeichner] = NZ(Forms!myForm!cboLiefName, [Bezeichner])
OR (Forms!myForm!cboLiefName IS NULL AND [Bezeichner] IS NULL)
)
AND ([S_Rating] = NZ(Forms!myForm!cboSRat, [S_Rating])
OR (Forms!myForm!cboSRat IS NULL AND [S_Rating] IS NULL)
)
AND ([Verantwortlicher] = NZ(Forms!myForm!cboVerant, [Verantwortlicher])
OR (Forms!myForm!cboVerant IS NULL AND [Verantwortlicher] IS NULL)
)
AND ([Lieferantenkategorie] = NZ(Forms!myForm!cboLiefKat, [Lieferantenkategorie])
OR (Forms!myForm!cboLiefKat IS NULL AND [Lieferantenkategorie]IS NULL)
)
AND ([Lieferantenart] = NZ(Forms!myForm!cboliefart, [Lieferantenart])
OR (Forms!myForm!cboliefart IS NULL AND [Lieferantenart] IS NULL)
)
AND ([Land] = NZ(Forms!myForm!cboLand, [Land])
OR (Forms!myForm!cboLand IS NULL AND [Land] IS NULL)
)
AND ([Audit_Ergebnis] = NZ(Forms!myForm!cboAudErg, [Audit_Ergebnis])
OR (Forms!myForm!cboAudErg AND [Audit_Ergebnis] IS NULL)
)
Somehow you (your code) must expand the where clause, for example:
myLieferantName = "Select * from Lieferant where [Bezeichner] = '" & Me.cboLiefName & "' And [Land] = '" & Me.cboLand & "'"
As a side note, a requery isn't needed after adjusting the recordsource:
Me.RecordSource = myLieferantName
' Me.Form.Requery

How to sum column values of a select query

I am trying to display the "total of all values" in the column NetPrice to a textbox which is in the form footer if a user chooses items in the two combo boxes. This should be true for only the displayed records.
I don't know how to go about this and hope someone has done this before. I would really appreciate some help.
Private Sub comboPeriod_AfterUpdate()
Dim strSQL As String
If Me.comboPeriod.Value = "Q1" And Len(Me.comboPostCode.Value & vbNullString) <> 0 Then
strSQL = "SELECT * FROM qrySpending WHERE (Month(orderDate) = 4 OR Month(orderDate) = 5 OR
Month(orderDate) = 6) AND (PostCode = '" & Me.comboPostCode.Value & "') ORDER BY Description ASC"
Me.frmDatasheet.Form.RecordSource = strSQL
Me.frmMain.Form.Requery
Me.txtNetPrice.Value = DSum(Nz("NetPrice", 0), "strSQL")
Exit Sub
End If
End Sub
Domain aggregate functions cannot reference SQL statement, only table and query objects. Do aggregate calc in a grouping query which can be the SQL statement in your code. However, SQL statement is not really needed for this. Simply use filter criteria in DSum() in textbox or VBA.
DSum("NetPrice", "qrySpending", "Month(orderDate) = 4 OR Month(orderDate) = 5 OR Month(orderDate) = 6) AND (PostCode = '" & Me.comboPostCode & "'")

How to populate combo box from a select Query using VB in access?

I'm new to VB and trying to populate combo box in access from a select query's result which is getting value from another combo box and the code that i'm trying is:
Private Sub addPartsButtonForm_Click()
ssql = "SELECT * FROM EquipmentTbl WHERE [EquipmentID] = '" & Me.equipmentCombo.Column(0) & "'"
Me!comboParts = ssql
Me.comboParts.Requery
End Sub
and when i press the button i got the following result in combo box:
SELECT * FROM EquipmentTbl WHERE [EquipmentID] = '34'
though its giving me the exact equipmentID but expected is something else other than that. Please help me
Set the combo RowSource. Also, no need for quotes in case field is Number.
Dim sSql As String
sSql = "SELECT * FROM EquipmentTbl WHERE [EquipmentID] = " & Me.equipmentCombo.Column(0)
Me.comboParts.RowSource = ""
Me.comboParts.RowSource = sSql

VBA LIKE operator with string

I have this code:
Private Sub Item_AfterUpdate()
Me.SApp.Form.RecordSource = "SELECT * FROM Application WHERE Item = '" & Me.Item.Value & "';"
End Sub
Now , this works so it filters SApp (a subform) base on the value in the combobox Item.
I wanna do something similar however not with equal but with like.
So , something like:
Private Sub Item_AfterUpdate()
Me.SApp.Form.RecordSource = "SELECT * FROM Application WHERE Item LIKE '" & Me.Item.Value & "';"
End Sub
I tried :
'% Me.Item.Value %'
'% '" & Me.Item.Value & "' %'
'% s %' with (Dim s as string ; s = Me.Item.Value)
None of this solutions works , any idea on how to fix this?
Use the * wildcard in MsAccess, in other dbs it's usually %.
Ref: https://msdn.microsoft.com/en-us/library/bb208897(v=office.12).aspx

select [combobox value] & "text"

I am trying to build SELECT statement in Access VBA, based on value chosen by user in ComboBox.
Example :
"SELECT [8_SV_RT] FROM DATA WHERE condition = value
I need 8 to be dependent on ComboBox value from form. (8,10,12 ....)
The name of ComboBox is DN, and I created string COL = "_SV_RT"
So far I have :
"SELECT [DN] & '" & COL & "' FROM DATA WHERE condition = value
It returns value 8_SV_RT to ComboBox, which I want to use after SELECT statement, but not as result. I must be missing some syntax or something? Can anyone please advise?
If I understand your question right:
Dim SQL As String
SQL = "SELECT [" & Me.MyComboBox & "_SV_RT] FROM DATA WHERE condition = value"
EDIT:
To use the exact object names from the question (before Siddharth edited it, I over-read that the combobox is named DN) and to take Siddharth's (now deleted) comment into consideration:
Dim SQL As String
Dim COL As String
COL = "_SV_RT"
SQL = "SELECT [" & DN & COL & "] FROM DATA WHERE condition = value"