Access listbox rowsource not updating via VBA - ms-access

Using: MS-Access 2013; Windows 8.1 Professional
I am trying to update the displayed values in a ListBox in Access by setting the RowSource property in VBA to an SQL statement. However its not working. The listbox does not show the results of the SQL.
Here is my VBA code:
Private Sub cmbStudent_Change()
Dim s As Integer
s = cmbStudent & ""
' cmbTerm is a ListBox
cmbTerm.RowSourceType = "Table/Query"
cmbTerm.RowSource = "SELECT DISTINCT Terms.TermID, Terms.TermName " & _
" FROM Terms " & _
" ORDER BY Terms.TermCode;"
cmbTerm.Requery
End Sub
After cmbTerm.Requery, the listbox is still empty ie. its not drawing the values from the TERMS table.
Without using a RecordSet (ADODB.RecordSet) object is there any way to make this work?
Thank you in advance for any helpful inputs.

I have been having the same issue I had to fix it by putting this code at the end.
Me.Listbox.RowSourceType = "Value List"
Me.Listbox.RowSourceType = "Table/Query"
Note I had to also put it in a sub above the one that declared the RowSource.
so the RowSource is in a sub that is called by the main RowSource

Related

MSAccess VBA - How to sort values in a Listbox

I have an MS Access list box that is being populated through DAO Recordset. When the data is displayed the values are not sorted. The code that I have is not working.
Dim sqlString As String
sqlString = "SELECT ReportName FROM MetadData.ReportDB_Test ORDER BY " & Me.ListReports.Value & " ASC;"
Me.ListReports.RowSource = sqlString
If you're just setting the rowsource of a listbox, there's no need to use VBA. Just set the rowsource property of the listbox to the SQL you need (you can even use Access's query builder).

access combobox values not showing

I am trying to create a form with 3 combo boxes on which will run a report from existing data. The combo boxes will filter down based on the data chosen in the next level up combo box.
However I'm having issues with the Batch Number Combobox not populating it's list
Now this is my VBA script:
Private Sub Form_Load()
Me.cboDate.RowSource = ""
Me.cboBatchNo.RowSource = ""
End Sub
Private Sub cboBottleNo_AfterUpdate()
Dim sDateSource As String
sDateSource = "SELECT [tblNewCC].[Date] FROM [tblNewCC]" & _
" WHERE [tblNewCC].[BottleNo] = " & Me.cboBottleNo.Value
Me.cboDate.RowSource = sDateSource
Me.cboDate.Requery
End Sub
Private Sub cboDate_AfterUpdate()
Dim sBatchSource As String
sBatchSource = "SELECT [tblBatchTotals].[BatchNo] FROM [tblBatchTotals] INNER JOIN [tblNewCC] ON [tblBatchTotals].[RunNo]=[tblNewCC].[RunNo]" & _
" WHERE [tblNewCC].[BottleNo] = " & Me.cboBottleNo.Value & _
" AND [tblNewCC].[Date] = " & Me.cboDate.Value
Me.cboBatchNo.RowSource = sBatchSource
Me.cboBatchNo.Requery
End Sub
From what I can see this is working alright on the vba side as I can see it replacing the rowsource of the batch number combobox, and in datasheet view it is giving me results.
However the combobox isn't showing anything in it's list....
Unless I go in make a change and save the sql query again.
Any clues?
The last part of sBatchSource should be " AND [tblNewCC].[Date] = #" & Me.cboDate.Value & "#" As an aside, 'Date' is a terrible name for a field. It is a reserved word in Access and will eventually cause you problems. Here is a helpful link http://www.fontstuff.com/access/acctut15pfv.htm

Add Criteria To VBA Code and Requery

So the following VBA code I have will requery the form based on the Combo1 value equal to January. Eventually, I'll have X number of years in the Combo value and only want to display all records based on each year.
Is it possible to add additional VBA code to use the Criteria from the query instead of having X amount of queries and requery them based on the year.
Private Sub Combo1_AfterUpdate()
If Combo1.Value = "2013" Then
[Form_Main Form].RecordSource = "Main_Form_Query"
[Form_Main Form].Requery
End If
End Sub
You can construct the query in runtime (remember: a query object in access is just an sql statement)
So, let's asume that your data is stored in a table called tblMyData that has columns called month (String, month name) and year (Numeric, Integer) (just examples). You can generate the SQL query in VBA this way:
...
Dim strSQL as String
...
strSQL = "SELECT * FROM tblMyData " & _
"WHERE month='" & ComboMonth.Value & "' " & _
" AND year=" & ComboYear.Value ";"
...
[Form_Main Form].RecordSource = strSQL
[Form_Main Form].Requery
...
Hope this helps you.
Open the form in a different way.
DoCmd.OpenForm "MyForm",,,"MyYear=2013"
Or
DoCmd.OpenForm "MyForm",,,"MyYear=" & Me.MyCombo
Assuming that MyYear in the table or query is numeric. If it is not, you can use single quotes.
The general idea is that you use a form with all records, and use the Where argument of OpenForm to specify the records that should be shown. You can do the same with reports.
I have only a vague idea about what you're trying to accomplish. It might help to show us the SQL from Main_Form_Query. Meanwhile, consider whether you can use the form's .Filter property to do what you need.
If the form's Record Source includes a text field named fiscal_year, you could filter the rows displayed based on the year selected in your combo box.
Private Sub Combo1_AfterUpdate()
' .Value is the default property, so not required after Me.Combo
Me.Filter = "fiscal_year = '" & Me.Combo1 & "'"
Me.FilterOn = True
End Sub
Yet another approach could be to reference the combo value in Main_Form_Query.
WHERE
Forms!YourForm!Combo1 Is Null
OR some_field = Forms!YourForm!Combo1
Then do Me.Requery in the combo's After Update event.

Databound CheckBoxList for Microsoft Access

In ASP.NET I have the option for building a CheckBoxList control from a data source where the values of the items can be the "Id" column (for example) while the text displayed next to the checkbox is from a "Name" column.
Is it possible to do something similar to this for Access 2003 with VBA? I am trying to avoid hardcoding the list of items if at all possible, but can't figure out how to do anything similar to this.
Just use a listbox. A list box in Access is great because it allows multiple columns, but will hide the first column as you ask. And you can just set the listbox to allow multiple selections (in the other tab of the property sheet for the listbox, set Multiselect to "simple"
And even better is you don't need any code to fill out the listbox, but can type in the sql or simply base the listbox on a SQL query.
So a listbox can look like this:
The code behind the button to turn the selected items into a useful list of PK id looks like this:
Private Sub cmdFax_Click()
Dim strIDList As String
Dim vID As Variant
Dim strSQLWhere As String
For Each vID In Me.lstFaxList.ItemsSelected
If strIDList <> "" Then strIDList = strIDList & ","
strIDList = strIDList & lstFaxList.ItemData(vID)
Next
If strIDList <> "" Then
strSQLWhere = " ID in (" & strIDList & ")"
End If
DoCmd.OpenReport "rptFax", acViewPreview, , strSQLWhere
End Sub
And if you want, you can in code for supply or set the SQL of the listbox like this:
Sub mytest()
Dim strSql As String
strSql = "Select ID, firstName, LastName, City " & _
"where city = 'Edmonton'"
Me.lstFaxList.RowSource = strSql
End Sub
So you don't need to type in any kind of list here. It not clear if you "must" have a actual check box here - you can use a continues form if you wanted, but I think the above is oh so much more simple and you don't need much code to set this up at all.

Runtime query does not work.Why?

I have a Problem with my codes and it made me confuse.this is my scenario:
in my access Project I have a Form and subform that work together.I wanted to enhace my performance . I deceided to load my form and subforms in Runtime(from this article) .I put my query on my form code, in Form_load event like this:
Private Sub Form_load()
Me.RecordSource = "SELECT DISTINCTROW tb_bauteile.* " & _
"FROM tb_bauteile LEFT JOIN FehlerCodes_akt_Liste ON tb_bauteile.CDT = FehlerCodes_akt_Liste.CDT " & _
"WHERE (((FehlerCodes_akt_Liste.Steuergerät)='MEDC17'))" & _
"ORDER BY FehlerCodes_akt_Liste.Fehlerpfad;"
End Sub
but another problem occured with another controls in my form.when i click another control .this function should be run:
Private Sub subPfadFilter(Kombifeld As Variant, Obd As String)
Dim strKrit, strAuswahl, strSg As String
If (IsNull(Me.CDT) Or (Me.CDT = "")) Then
strAuswahl = "*"
Else
strAuswahl = Me.CDT
blnFiltOn = True
End If
.....
but it doesn't work and say me method or dataobjectt not found( without this if statement works the function works fine .the problem come from CDT ) if I put the query in Datasource in my form property that works properly
I cant understand the relation between these two things(putting the query on datasource of form property then working the if statement (CDT) good but when i put the query in Form load that does not work ) ,would you please help me if posible?have you any idee?
thank you so much for your helps
When using the "Me" reference, you need to ensure the code is behind the present form, you've mentioned that the Form_Load code is on there, but is the "subPfadFilter" somewhere else?
If so then it needs to either be moved on to this form, or change your reference to the forms name instead of "Me".
If it is on the form then the error seems to be complaining about the CDT item, you will need to confirm that this is the name of either a text field or a control on this form.
If it is then you can try inserting:
Me.Requery
After your SQL statement, this may wake up the forms reference to the object.
This bit is not essential but to avoid carrying out two tests on CDT and streamlining your code slightly I would amend to this:
If Me.CDT & "" = "" Then
strAuswahl = "*"
Else
strAuswahl = Me.CDT
blnFiltOn = True
End If
This captures both Is Null or "" in one shot.
UPDATE:
For an unbound form, you need to set your form's controls via VB as well as the forms recordsource e.g:
Private Sub Form_load()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT DISTINCTROW b.* " & _
"FROM tb_bauteile As b LEFT JOIN FehlerCodes_akt_Liste As f " & _
"ON b.CDT = f.CDT " & _
"WHERE f.Steuergerät = 'MEDC17' " & _
"ORDER BY f.Fehlerpfad;")
rs.MoveFirst
Me.CDT = rs!CDT
'Any other fields go here
rs.Close
Set rs = Nothing
End Sub
However if you are returning more than one record at a time then you will either need to stick with a bound form or perhaps use a listbox to display your information. Unbound forms do not use the Forms "Recordset" property.
Note: I have also 'Aliased' the table names in your SQL statement, this is a good practice to get into : )