select [combobox value] & "text" - sql

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"

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 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

My query for counting subrecords in Acces does not work?

I'm having a problem with my query. I'm having a database with a Table "Label" that contains some subrecords("ReleaseID").
What I want to do: I want to count the subrecords that are linked to the LabelID in my Label table. I have the following code:
Dim db As Object
Dim rst As Recordset
Set db = CurrentDb
Dim qryCount As String
Dim Value as integer
qryCount = "select count(ReleaseID) as aantal from(select LabelID, ReleaseID from Label where LabelID = " & Me!LabelID")
Set rst = db.OpenRecordset(qryCount, dbOpenDynaset)
Value = rst!aantal
The query is working when I try it in the Query design in Acces. But when I use it in VBA then it doesn't.
Appreciate any help.
It's hard to see your actual code when you don't format it.
This is what you last posted
qryCount = "select count(ReleaseID) as aantal from Label where LabelID = " & Me!LabelID
Set rst = db.OpenRecordset("Label", dbOpenDynaset)
Value = rst1!aantal
If that's what you actually have then you
Probably can't use Value - I think that's a reserved word
You've set the recordset name to rst but you're trying to use rst1 for value reference
I'm not sure if its just a copy mistake, but the closing brace is missing, isn't it?
qryCount = "select count(ReleaseID) as aantal from(select LabelID, ReleaseID from Label where LabelID = " & Me!LabelID & ")"
If your are just looking for a count you can do it with DCount
varCount = DCount("ReleaseID", "Label", "LabelID = " & Me!LabelID)
No recordsets required. If you're not iterating through the recordset Dcount is much simpler and less taxing on the system.
It's called a domain function, well worth looking up if you have a spare moment (Dlookup, DSum, Dmax, Dcount).
Why not use DCount:
Dim Value As Long
Value = DCount("*", "ReleaseTable", "[LabelID] = " & Me!LabelID.Value & "")

Update combobox based on another combobox through query

I have several combo boxes (CB) populating from my access database. They all pull from the same table and use the same hidden primary key from the table. When I select a value from one of the CB's I would like to have the others updated with the associated value based on the matching primary key.
Is this possible?
I've been trying to use variations of the following for a while now with no success:
Dim strSQL As String
strSQL = "SELECT gsr_id FROM task WHERE task_wid = " & Me.cboTask.Column(0)
Me.cboGSRTask.Section = CurrentDb.OpenRecordset(strSQL)
Debug.Print "SELECT gsr_id FROM task WHERE task_wid = " & Me.cboTask.Column(0)
Thank you.
If you open a recordset, you need to read a value from it, you can't use the recordset as value.
I guess what you are looking for is:
Dim strSQL As String
Dim RS As Recordset
strSQL = "SELECT gsr_id FROM task WHERE task_wid = " & Me.cboTask.Column(0)
Set RS = CurrentDb.OpenRecordset(strSQL)
' This assumes that gsr_id is the bound column of cboGSRTask
Me.cboGSRTask = RS!gsr_id
RS.Close
Or instead all of the above, using DLookup() :
Me.cboGSRTask = DLookup("gsr_id", "task", "task_wid = " & Me.cboTask.Column(0))
Or it might be even easier to add gsr_id to the row source of cboTask (as a column with width = 0) and use that column to assign to cboGSRTask.

Mismatch datatype criteria expression, string field

I have a ms access database and when I try to update a column in a module I get the error "Mismatch datatype criteria expression", when I create the column the datatype was TEXT(25) and the value that I'm trying to assign is a String:
Dim str As String
str = "test"
sql = "UPDATE Table "
sql = sql & "SET Table.[column] ='" & str & "' "
sql = sql & "WHERE Table.[id] = 1;"
MsgBox(sql)
Application.CurrentDb.Execute(sql)
In the MsgBox the string of query appears to be right: "UPDATE Table SET Table.[Column]='test' WHERE Table.[id]=1;
Which is the error?, how to fix this?
If Table.id is a numeric column your where clause is okay, but if it is a text column it should read:
WHERE Table.id = '1'