Why is my WHERE clause not working in my combo box - vba

I have a combo box called NameFilt. I wish the row source to be as follows:
SELECT DISTINCT VoucherListTbl.BuyerName
FROM VoucherListTbl
WHERE (((VoucherListTbl.BuyerName) Is Not Null)) OR (((VoucherListTbl.BuyerName)<>""));
i.e show all the unique BuyerNames from my table and dont include any blanks
The above SQL is generated in the query bulider by clicking on the 3 dots in combo box's row source in the property sheet, then selecting the BuyerName field and then entering Is Not Null Or <>" in the criteria. Clicking run in the query builder displays the exact result I expect.
On closing and saving the query builder and then clicking in the combo box on the form I get a different result - All the DISTINCT names are there, but there is a blank at the start of the list.
When I attempt to use this SQL in my VBA code I get another result. The code is:
Private Sub NameFilt_GotFocus()
Me.AllowEdits = True
Me.NameFilt.RowSource = "SELECT DISTINCT VoucherListTbl.BuyerName
FROM VoucherListTbl WHERE (((VoucherListTbl.BuyerName) Is Not Null))
OR (((VoucherListTbl.BuyerName)<>""));"
Me.NameFilt.Dropdown
End Sub
This results in the combo box's dropdown showing only one option - a blank! There are no names listed.
Moreover, If the WHERE clause is removed i.e. the code is:
Private Sub NameFilt_GotFocus()
Me.AllowEdits = True
Me.NameFilt.RowSource = "SELECT DISTINCT VoucherListTbl.BuyerName FROM VoucherListTbl;"
Me.NameFilt.Dropdown
End Sub
Then the DISTINCT names are shown, with a blank option at the top of the list which is what one would expect
Please could you help by explaining why the WHERE clause will not work for me when entered into the VBA code
Many thanks

If you use "" within "" it will break the string and that's the reason of non-working WHERE condition. Encode quotes with "" i.e. end of your original string should look like <>""""));" or replace "" with '' and try again.
Note, same query can be written as
SELECT DISTINCT BuyerName
FROM VoucherListTbl
WHERE IsNull(BuyerName,'')<>''

Related

Access report : Set unbound textbox value depending on other textbox value

I have report in Access 2016 with two columns (Name and Group) from a query.
I want to fill an unbound textbox in my report depending on the value in Group.
The value I'm getting is the last group value. I want it to look for each row value.
Please help me.
I've tried get the value from Group to display in textbox.
if me.group.value = "1" then me.textbox = "blablabla" end if
it only works if I use the last group.value ("4"), but every row will get "blablabla"
I have tried with _load and _open , _activate. Don't know the difference though.
Please help
sincerely
Dan
Don't use VBA. Put expression in textbox ControlSource or do calc in query used as report RecordSource and bind textbox to that field.
IIf([Group] = 1, "blablabla", Null)

Access VBA - SQL Statement using VBA to fill List Box on form using Text Box Combo Boxes Values Where OR

Need some help, examples, and advice
I have 2 tables | tblMAIN | tblRECORDS
and One Form | frmFAMILY - data source: tblMAIN
I am trying to create a simple SQL Statement using VBA in access that will
match records From tblRECORDS that match WHERE text box OR 2 combo box values on the form frmMAIN match and then fill List Box listRECORDS with data And ignore any values if they are 0.
all of the values are numbers but would like to know how to format if i decide to search for text, like, etc.
I would also like to incorporate some If Then Else. Does anyone know a good resource for SQL statements using VBA examples?
This is what I have but it is not working.
Private Sub Form_Current()
Dim strRecords As String
strRecords = "Select * FROM tblRECORDS WHERE tblRECORDS.RecordID = [Forms]![frmFAMILY].[txtID] OR tblRECORDS.RecordID2 = [Forms]![frmFAMILY].[cboSpouse1]"
Me.listRECORDS.RowSource = strRecords
Me.listRECORDS.Requery
End Sub
I have resolved it by adding OR instead of AND.

fill textbox based on combobox selection vb.net *data not bound [duplicate]

I got a bounded combobox to a group name in vb.net. i can view in the drop down items of the combobox the set of GROUP NAMES. but when i do an insert or update query, i need to make the selected group name refers to the GROUP NUMBER. I don't want to store letters in the database, instead i prefer numbers. how can i do that?!
Here is my code so far :
cmd1.Parameters.AddWithValue("#group", DirectCast(Additemcombobox.SelectedItem,
DataRowView).Item("GroupName"))
Storing the group name in database is currently working well.
My question might not be well explained. Please ask me in case...
any help would be appreciated
You can show one element to the user such as the name, but use a different one for the code to identify the item using DisplayMember and ValueMember
Dim SQL = "SELECT Id, Name FROM GroupCode ORDER BY Name"
...
GrpDT = New DataTable
GrpDT.Load(cmd.ExecuteReader)
cboGroup.DataSource = GrpDT
cboGroup.DisplayMember = "Name"
cboGroup.ValueMember = "Id"
The user will only see the names, while the code can use ValueMember:
Private Sub cboGroup_SelectedValueChanged(...etc
Console.WriteLine(cboGroup.SelectedValue)
End Sub
It prints the Group ID not the name. Depending on the ORDER BY clause in the SQL and the ID, the SelectedIndex may or may not match, so respond to SelectedValueChanged event. If you use SelectedValue instead of SelectedItem you wont have to thrash about with a DataRowView item.
Note that SelectedValue is Object so you will have to cast to integer or whatever for use elsewhere.

Vba code to show selected data from combobox

I have an Access 2010 form which has a combobox listing three columns:
ID
first name
last name
The combo box is bound to a table containing this data. The ID column in the combo box is hidden, it only shows the first and last name.
When the user selects a row only the the first name is shown. in the property section, I chose:
Column Count: 3
Column widths:0;3,3
Bound Column: 1
I made another text field and in the combobox I wrote the following vbcode:
text=combo.value
that shows in the text field the chosen ID.
I want to show in another field (text\combo?) the last name.
How can I do that?
well I did it with recordset.
if someone know a simpler solution, I will be glad to lrn.
this is my code:
Dim dbs As ADODB.Connection
Dim id As String
Set dbs = CurrentProject.AccessConnection
Set rsRep = New ADODB.Recordset
Set rsRep.ActiveConnection = dbs
rsRep.Open "tblRep"
id = Me.cbPrvFirstName
rsRep.MoveFirst
rsRep.Find "RepId=" & id
Me.txtPrvLastName = rsRep.Fields(2)
rsRep.Close
You can pull in the value from another column within the combo box using the below code. By default combo.value will always give the value from the first column even if hidden.
try
text=combo.column(x)
in your case to retrieve last name it would be
text=combo.column(2)
where x is a NUMBER of the column you want to retrieve 0 being the first column
You could also try an alternative - This works like a VLookup in excel if you are familiar with that.
(Not as simple as the first option ;) )
text=Dlookup("[last name]","tblRep","RepId=" & combo.value)

Linking result of a query that related to a Listbox value to textboxes?

I have a problem in MS Access and I don't know how can I solve this problem.
I have a listbox on my Form. If the use changes the value of the Listbox,a SQL query should be run and the result should be displayed is some Textboxes. For example:
The user select 'A' value From the list box then this query should be run:
SELECT XValue, YValue,Wert FROM Mytable WHERE Name='A' and ID=fzgID
then I want to display XValue, YValue and Wert in three textboxes. The problem is may be I have more than one pair for this combination (XValue, YValue,Wert).
How can I do that? How can I Link the list box and the query to the textboxes?
thank you so much
Every ListBox has a ListBox_Click() procedure, which is run when an element of the listbox is clicked.
You can get, what you want, if you do something like this:
Sub ListBox1_Click()
Dim valueOfListbox As String
valueOfListBox = ListBox1.Value
' **** here comes your code ****
' get the actual value from the listbox and do the query
' change the values of the textboxes to the result of the query
End Sub
If your listbox is named different than "ListBox1" you have to change the name of the procedure. For example if the listbox is named "blaBox" it has to be blaBox_Click().
Depending on the structure of your data, I would do something like this. Set the table/query of your listbox to have all the data you want to display.
SELECT ID, Name, XValue, YValue, Wert FROM Mytable
In the Format tab of the property sheet of the listbox, set the column count to 2, and the column widths to 0";2" or your preference. This will hide the other values in the listbox rows.
In the three textboxes, set the control source like so.
Textbox1.ControlSource: =[Listbox1].[Columns](3)
Textbox2.ControlSource: =[Listbox1].[Columns](4)
Textbox3.ControlSource: =[Listbox1].[Columns](5)
You may need to adjust the numbers. A listbox has a property called Columns that enable you to access the values at different columns of the listbox. If you don't want to or cannot have all the data in the listbox, then you can use the DLookUp function in each textbox.
Textbox1.ControlSource: =DLookUp("XValue", "Mytable", "ID=""fzGID"" And Name=""" & [Listbox1] & """")
The reference to [Listbox1] will pull the value of the bound column. If the bound column is not the data you are looking up by then you will need to reference a column (i.e. [Listbox1].[Columns](2)).