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.
Related
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
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.)
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.
Fairly new to VBA. I have a list box on a form within Access which is populated with data from a table. Selecting a value from the list box gives and ID which is then used to perform a query. I need this ID to be available for use in another form to perform a query based on the Value. What is the best way of achieving this?
`Dim IDValue As String
IDValue = Me.lstBoxCompanyName.Value
CompDetailSQL = "SELECT * FROM Companies WHERE Companies.CompanyID = " & IDValue`
In a module
Dim IDValue AS Integer
Sub setIDValue(id As Integer)
IDValue = id
End Sub
Function getIDValue() As Integer
getIDValue = IDValue
End Function
Then from any of your code
'This will set you ID
setIDValue YOUR_VALUE
'This will retrieve the value
getIDValue
'so your code could be
setIDValue Me.Me.lstBoxCompanyName.Value
CompDetailSQL = "SELECT * FROM Companies WHERE Companies.CompanyID = " & getIDValue
Obviously this could use some Error Handling in the event that No IDValue is set. Also I used integer even though I see you are using a String the data type should be the same type as CompanyID so you can change Integer to String if needed but your will also have to change your query to SELECT * FROM Companies WHERE Companies.CompanyID = '" & getIDValue & "' because your query implies a number currently.
You can gain access to another forms controls by specifying the full path. Example :
Forms!MyFormsName!MyControlsName.Value
and
Forms!frmCustomer!CboCustomer.Column(0)
So if you want to access a value in VBA that is contained in another form and use it as part of a query it would look like:
CompDetailSQL = "SELECT * " & _
"FROM Companies " & _
"WHERE CompanyID = " & Forms!frmCustomer!lstBoxCompanyName.Value
in ms Access you can also simply use form_NAMEofFORM.NAMEofCONTROL
Example you can reference
dim myAmount as double
myAmount = form_receipt.total
to access the value showing on form receipt textbox total.
I have created a form in Access which queries a single record in a set date range. This works beautifully. My question is how can I query multiple records from a form text box.
Something Similar to the SQL function
IN ('XXX','XXX','XXX')
I want my users to be able enter multiple values on the form and it will spit out the corresponding Data.
Try the Split() function. Something like this:
Dim arr() As String
Dim MySQL as String
If Len(Me!MyTextbox) > 1 Then
MySQL = "SELECT * FROM MyTable WHERE MyField in ("
arr = Split(Me!MyTextbox.Text, ",")
For i = LBound(arr) To UBound(arr)
MySQL = MySQL & "'" & arr(i) & "', "
Next
MySQL = Left(MySQL, Len(MySQL) - 2) & ")"
Me.RecordSource = MySQL
Else
MsgBox "There are no values to search for."
EndIf
You would just loop through the array and add each value to a WHERE string. You would have to instruct your users (via a label on the form) to separate values using a comma.
When you want to allow multiple entries it's generally better to have a datasheet subform where users can enter data one per row. Then you use the subform data in queries just like a regular table.