Combo Box depends on another combo box - vba

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

Related

Filter records based on a combo box selection

OS - Win 10
Office 365
Hello,
I am attempting to create a form with a combo box to filter results.
I have one table that I want to filter. The filter is one of the columns in the table (using unique values from that column).
The following code returns a "Data type mismatch in criteria expression" error;
Sub SetFilter()
Me.cboAcc.RowSource = "select * from frmQryLedger where ACCOUNT_ID = '" & Me.cboAcc & "'"
'For trouble shooting
MsgBox Me.cboAcc.RowSource
End Sub
Private Sub cboAcc_AfterUpdate()
'Call subroutine to set filter based on selected ACCOUNT_ID
SetFilter
End Sub
Thank You
Ben

MS Access use Combo-Box value as Query Criteria

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.

ComboBox max records reached

I have a table in MySQL accessed through a linked table (via ODBC) in Microsoft Access 2013.
This table contains over 124,000 records and I need a ComboBox in a form to be able to search through the UPC column.
This is the query that is the current datasource for the ComboBox:
SELECT [ID], [UPC_Case], [Description] FROM itemlist ORDER BY [UPC_Case];
This works perfectly except that the table view under the ComboBox won't go past record number 62287 (however the auto-fill still works for records that the table can't see), is there a way to make it able to view all the records?
Access ComboBoxes have a maximum record count of 65535.
To circumvent this, I found an article that gave me the groundwork required to write a function that sets the rowSource dynamically once a certain number of characters have been typed.
This is the function that sets the rowSource. I refactored the code so that it can be used on any comboBox in any Form with any Query.
Dim inputStub As String
Function ComboLimiter(targetCombo As ComboBox, minChars As Integer, Query As String, searchField As String)
Dim inputStr As String: inputStr = targetCombo.Text 'Set input string
Dim newStub As String: newStub = Nz(Left(inputStr, minChars), "") 'Set first n characters of targetCombo.Text
If newStub <> inputStub Then 'If first n chars are the same as previously, do nothing.
If Len(newStub) < minChars Then
'Remove the RowSource
targetCombo.RowSource = Query & " WHERE (False);"
inputStub = ""
Else
'New RowSource
targetCombo.RowSource = Query & " WHERE (" & searchField & " Like """ & newStub & "*"") ORDER BY " & searchField & ";"
inputStub = newStub
End If
End If
End Function
And the function can be bound to the ComboBox change event like this:
Private Sub UPCCombo_Change()
Call ComboLimiter(Me.UPCCombo, 1, _
"SELECT ID, UPC_Case, Description FROM itemlist", "UPC_Case")
End Sub
There's a known bug where there are sometimes issues with large recordsets like this. Are you sorting on a text based field? Try removing the sort if so and seeing if it fixes the issue.

How to use a query as a control source for a textbox on a form?

I have a form myForm that's binded to a table tbl in my database. (I don't know if binded is the correct term, but It shows records from tbl on by one.)
In the form:
contact: textbox, binded to tbl.contact.
dailyCount: textbox, should show the amount of contacts entered today.
In the table:
contact
dateEntry
The query I want to use is:
SELECT count(*)
FROM tbl
WHERE contact = currentContact
AND month(dateEntry) = month(now)
AND day(dateEntry) = day(now)
AND ear (dateEntry) = year(now)
Where currentContact is the contact that is showing on the form now.
I tried putting the query in the dailyCount dataSource, but It's not working. When I click on the three dots on datasource to access the wizard, I get a window to build functions and not queries.
How do I get the currentContact showing on the form into the query?
There are multiple ways to do this. For a couple of reasons, I don't like to hardcode queries in the datasource of a specific field, and I mostly build/assign all my queries in VBA. So here's how I would do it.
In the load event of you form :
Private Sub Form_Load()
Dim SQL As String
Dim RST As Recordset
dim theCOntact as string ' Change accordingly
theCOntact = Me.currentContact ' I don't know how your fields are named, so change accordingly
SQL = "SELECT count(*) AS cnt FROM tbl WHERE contact = " & theContact & "' AND month(dateEntry) = month(now) AND day(dateEntry) = day(now) AND Year(dateEntry) = year(now)"
Set RST = CurrentDb.OpenRecordset(RST)
If RST.BOF Then
dailyCount.Value = RST!cnt
Else
dailyCount.Value = 0
End If
End Sub
Assuming your contact field is string, if its a number remove the quotes in the SQL
Probably the simplest approach is to use the DLookup function with an associated query:
Create and save a named query with your SQL code or equivalent. Let's call it "qryDailyCount". Note that it should be modified to look something like this (in particular, name the column and change the record reference to a GROUP BY for now):
SELECT count(*) as DailyCount
FROM tbl
WHERE month(dateEntry) = month(now)
AND day(dateEntry) = day(now)
AND year (dateEntry) = year(now)
GROUP BY contact
In the dailyCount textbox, set the Control Source to something like this:
=DLookUp("Count";"qryDailyCount";"contact = [contact]")
(Note that if the contact field is a text field, you must enclose it with single quotes.)

Group meetings MS ACCESS

I have a database that monitors staff leave and training, every 8 weeks a group will gather and then do training, what i am after is a way to rather than individually assign each member a training event id rather open the event and select the participants, from here each staff member will have this specific training logged against them
So far i have each staff recognised by ID and each training by GID but am having trouble entering this training event in against all IDs?
This BULK INSERT could be probably done by using an Unbound Form. The minimal controls would be one Listbox, ComboBox, a TextBox and a Button.
ListBox (staffList), which should be allowed to select multiple items; will obtain its RowSource from the Staff table, something like
SELECT
staffID,
staffName
FROM
staffTable;
The properties would be Bound Column : 1, Column Count : 2, Column Widths : 0cm;2.542cm
The ComboBox (eventCombo), will obtain its RowSource from the Event table, something like
SELECT
eventID,
eventName
FROM
eventTable;
The properties would be Bound Column : 1, Column Count : 2, Column Widths : 0cm;2.542cm
Then the TextBox (dateTxt) would just have a Date control (for the date of the event).
The code behind the Button (addEventBtn) click would simply be,
Private Sub addEventBtn_Click()
Dim varItem As Variant
If Me.staffList.ListIndex = -1 Then
MsgBox "No Staff selected. You have to select at least one staff before you can proceed !", vbCritical
Me.dateTxt.SetFocus
Exit Sub
End If
If Me.eventCombo.ListIndex = -1 Then
MsgBox "No Event selected. You have to select at least one staff before you can proceed !", vbCritical
Me.dateTxt.SetFocus
Exit Sub
End If
If IsNull(Me.dateTxt) Then
MsgBox "No Date selected. You have to select Date before you can proceed !", vbCritical
Me.dateTxt.SetFocus
Exit Sub
End If
For Each varItem In Me.staffList.ItemsSelected
CurrentDB.Execute "INSERT INTO tbl_EventList (eventID_FK, staffID_FK, eventDate) VALUES (" & _
Me.eventCombo & ", " & Me.staffList.ItemData(varItem) & ", " & Format(Me.dateTxt, "\#mm\/dd\/yyyy\#") & ")"
Next
End Sub
This should work out. Please change it according to your design ! If you have any questions please add them to comments.