I have a simple form with two combo boxes.
cboProvince which contains a list of provinces [Alberta,Saskatchewan,Quebec]
cboClient which contains a vast list of clients. Each with a Province.
I am attempting to populate cboClient with clients from the province chosen in cboProvince.
My current VBA Code is contained in the "After Update" event in cboProvince and is as follows:
Private Sub cboProvince_AfterUpdate()
Dim SQL As String
SQL = "SELECT tblCustomer.[Customer Name], tblCustomer.Province FROM tblCustomer WHERE Province = '" & cboProvince.Value & "'"
'Apply Row and requery
cboClient.RowSource = SQL
End Sub
I have double checked that my combo boxes are indeed named "cboProvince" and "cboClients"
Something to note as well is the "Province" Column in the "tbsCustomer" is a Combo-Box. I'm unsure if that makes a difference.
Unfortunately the cboClient combo-box does not auto-populate.
Related
I have one table that has purchases of material from several vendors, some as many as 50 to 100 items per vendor. I'm trying to use a split form with a combo box to select a vendor then show all records of the purchases from that vendor.
I listed the vendors in the combo box and tried a VBA code to filter the records
Private sub cboVendors_AfterUpdate()
Me.Filter= "[Vendors] = " & char(34) & Me.cboVendors & char(34)
Me.FilterOn =True
End Sub
I'm using Access 2016 and get a:
compile error
Sub or function not defined
If I dim char(34)
I get a syntax error :
(missing Operator) in query expression[vendors] = Ace Hardware
The vendor selected.
The code I used, I found on a Youtube video online that seemed to work with Access 2007.
I'm expecting to get the results of the records pertaining to the vendor I select from the combo box.
It is not char but Chr. However, try:
Me.Filter = "[Vendors] = '" & Me.cboVendors & "'"
I have a form with two combo boxes, one Wards the other room number. Wards are medical units, while the rooms are room numbers (like GMU-01).
I'm trying to limit the room names based on the wards value, ie list all the bed numbers for a particular unit.
SELECT DISTINCT [TblWards].[Wards] FROM TblWards ORDER BY [TblWards].[Wards];
The row source from the Wards combo box (First box)
Private Sub Wards_AfterUpdate()
Dim txt As String
txt = "SELECT TblWards.Room FROM TblWards WHERE (TblWards.Wards)= '" &
Me.Wards.Column(0) & "' ORDER BY TblWards.Room;"
Me.RoomN.RowSource = txt
End Sub
SELECT [TblWards].[Room] FROM TblWards WHERE ((([TblWards].[Wards])=AMU));
The row source from the second combo box RoomN
I get an error when I attempt to choose a value from the Wards combo box. If I line out the afterupdate code, I can choose a value. The error is unexpected error, access needs to shut down.
Then, I get an error if I attempt to select a value from the second combobox. asking for the AMU parameter.
I inherited this code and trying to determine how to go about it, rewrite or try to salvage it.
from what I understand from your message, you can try this:
Private Sub cboWard_AfterUpdate()
Dim strSQL As String
strSQL = "SELECT * FROM tblRooms WHERE rWardID = " & Me.cboWard
Me.cboRoom.RowSource = strSQL
End Sub
Try Following-
Clear ControlSource and RowSorce Property of comboBox 'RoomN' Manually.
Try following code in AfterEvent of 'Wards' comboBox
Private Sub Wards_AfterUpdate()
Dim txt As String
txt = "SELECT Room FROM TblWards WHERE [Wards] = '" & Me.Wards.Value & "' ORDER BY Room;"
Me.RoomN.RowSource = txt
End Sub
Just curious if filtering fields is a possibility in Access? Or if there is VBA code that allows the "Show" Box in the query to be checked or unchecked based on an if statement?
Basically I have about 80 fields in my database and I want to create a table with only 3 of them, based on a value in a combo box. 2 are always the same, and the 3rd would be based on a combo box.
Can I do that?
You could do this by altering the SQL of the query object you want to modify. So if your query object is named "qryMyQ", and your combo box object is named "cboDropDown", then the VBA would be the combo box's AfterUpdate event and would look like this:
Private Sub cboDropDown_AfterUpdate()
Dim qryDef As QueryDef
Dim sql As String
Set qryDef = CurrentDb.QueryDefs("qryMyQ")
sql = "SELECT [Column1], [Column2], [" & cboDropDown.Value & _
"] FROM yourTableName " & _
" WHERE [" & cboDropDown.Value & "] = ""applicable"""
Debug.Print sql 'This line will allow you to troubleshoot the SQL that is to be executed.'
qryDef.sql = sql
Set qryDef = Nothing
End Sub
Just a note here; but you'd want to make sure that users cannot add or edit the combo box options to avoid sql injections. Not sure how friendly your userbase is.
I have an ms-access db that has a form (FormA) with two sub forms (subFormB and subFormC). I am trying to use the value of a combobox (combo2) in FormA to get values (from a table) which will be input into subFormB and subFormC. i.e whatever values the user selects from combo2 will be used as a filter to query a table in the db and the values will be input into listboxs in either subFormB or subFormC
My code runs well with FormB but i cant seem to get it to work for FormC
The values that are displayed in combo2 depend on the value of another combo box (combo1).
If combo1 is "staff name" then the values in combo2 are string (names) and results are input into subFormB
If combo1 is "project name" then the values in combo2 are numeric (numbers)and results are input into subFormC
an example of my code is below
Private Sub Combo2_AfterUpdate()
If Combo1 = "Staff Name" Then
subFormB.Visible = True
ltemp = "SELECT Staff.department"
ltemp = ltemp & " FROM Staff "
ltemp = ltemp & " WHERE Staff.staff_name = '" & combo2 & "' "
Me!subFormB.Form.List3.RowSource = ltemp
If Combo1 = "Project Number" Then
subFormC.visible = True
TID = "SELECT Contracts.TargetIssueDate"
TID = TID & " FROM Contracts "
TID = TID & " WHERE Contracts.cms = combo2 "
Me!subFormC.Form.List25.RowSource = TID
End Sub
In other words the first part of my code works but the second part (starting from the second if statement) doesnt. i feel it is because the value of combo2 at this instance is numeric, and the problem is from the query but i dont know how to rewrite the query so that it would work.
In the first query, the value of combo2 is included in the WHERE clause.
But the second query includes the combo's name instead of its value. In other words, this is the WHERE clause built by the code ...
WHERE Contracts.cms = combo2
In that situation, I would expect Access to treat combo2 as a parameter and ask you to supply a value for it. But you didn't mention that, so something more may be going on.
The code includes ...
If Combo1 = "Staff Name" Then
... but there is no closing End If later. Perhaps, you've shown us an abbreviated version of your actual code, and the actual version does include End If?
Rather than sorting out those details, I'll suggest a different approach. Make a backup copy of your db file. Use this query as the RowSource for List3 on subFormB.
SELECT Staff.department
FROM Staff
WHERE Staff.staff_name = Forms!FormA!combo2;
Then, in Combo2_AfterUpdate of FormA, just Requery the subform's listbox, or even the entire subform, instead of altering the listbox RowSource ... one of these two ...
Me!subFormB!List3.Requery
Me!subFormB.Requery
If that approach is satisfactory, apply the same strategy to the other subform . And if it fails completely, revert back to your backup db.
I have a form with a subform which contains upto 100 columns however i only need to see 5 of these columns at any one time. The columns I need to see are always First name and surname plus three other columns based on the choices made in the main form (columns have labels such as HT001, HT002 etc.). One column for each of the three combo boxes available. I have looked everywhere for a solution and keep finding
Me![mycontrol].columnhidden = false
which I can see how it works for one or two columns but my code would need to be rediculously long if each column needs this. In addition to the vast quantity of columns I have 12 different subforms which are choosen from using a select case procedure. I am looking for a simple solution as I am still very much learning VBA.
If you are using a datasheet, you can dynamically set the record source according to the selections made.
Me.RecordSource = "SELECT HT001, HT002 FROM MyTable"
You can refer to the combo box when building your sql:
Me.RecordSource = "SELECT HT001, " & Me.Combo1 & " FROM MyTable"
Some notes on using a listbox with a field list from a table.
You can set the row source type to Field list and the row source to the name of a table:
RowSource: Table1
RowSourceType: Field List
In order to select multiple fields, it is important to set the multiselect property:
MultiSelect : Simple
You can iterate through the selected items in code and build a field list for use in your sql:
For Each itm In Me.List0.ItemsSelected
strSelect = strSelect & "," & Me.List0.Column(0, itm)
Next
strSelect = Mid(strSelect, 2)
sSQL = "SELECT " & strSelect & " FROM Table1"