Referring to a list box value in Access VBA - vba

I have a sql query in access which result it being transferred to a list box in an Access form.
What I need to do is insert the values from my form to an access table, for which I am using the following code in VBA:
sSQL = "INSERT INTO MainTable ([IndxLvl], [POF_Pluquo], [Actual_Shipped], [Buffer], [Do_Something], [Controlled], [Overstock]) VALUES (9, Forms![Form_helper].LVL9POF.Value, Forms![Form_helper].LVL9SHIPPED.Value, Forms![Form_helper].Buffer9.Value, 14, 36, 50)"
DoCmd.RunSQL sSQL
The name of the List Box is Buffer9, so I am referring to the value in the list box with:
Forms![Form_helper].Buffer9.Value
This line of code works for all of the values I have in other text boxes.
I do realize that this is not a text box, but a list box and I guess there is another way of referring to the value in this object.

Related

Adding an item to the end of a multi-column listbox

Being a beginner in this, I've tried to search for a solution to my issue. I created a form in Access 2013 with a List Box. The List Box is bounded to a table with 10 fields.
For the List Box, in the Property Sheet, I've set the Column Count to 10, but have hidden some columns, i.e. some has a Column Widths of 0" because I'm only showing important data in the List Box.
The Row Source Type of the List Box is set to Table/Query.
When I open the form, it queries the table and populates the List Box with all data.
I have 4 unbounded Text Box that allows the user to enter data, and have a button to allow the input data to be added to the end of the List Box and also be automatically added to the table.
I've searched for how to do this:
Add items to a multi-column List Box
vba listbox multicolumn add
Adding items in a Listbox with multiple columns
Adding values multiple columns to listbox in form access vba
Excel multi-select, multi-column listboxes
I've tried to use the .List property of the List Box, but it does not exist.
I've tried to do something like:
Me.MyList.AddItem "" & ";" & Me.textBoxValueOne & ";" & Me.testBoxValueTwo & ";" & ""
However, VBA complains I need to change the .RowSourceType to Value List, and when I added it, the List Box clears all the data, and adds this data as the first entry, which is not what I want.
I've added the following code, which adds a blank entry to the end of the List Box as well as table, but I don't know where to go further with it to save the input data from the Text Box to the end of the List Box and table:
DoCmd.GoToRecord , , acNewRec
DoCmd.RunCommand acCmdSaveRecord
Me.MyList.Requery
How can I achieve this?
Thanks.
You need to add the record to the table first and then requery your listbox.
After adding the new record try
Me![column name] = textbox1.value
And other textboxes
Followed by your save command.
Once you've added the record. Simply requery the listbox.
Me.listbox.requery

ms access - How to edit dlookup query textbox in vba?

The form retrieves data from a query to a textbox which needs to be editable so that input data can be saved to a table. To do this, I left the textbox control source blank and on form open module wrote me.text1870 = dlookup(...
To edit this textbox I wrote the following code:
DoCmd.runsql ("Update tableName Set qnty = " forms![frmName]!text1870")
But it is not updating the table or it updates the entire table with same value.

Select SQL Query using input from form fields Access 2007

I have a form with two combo boxes and a button that runs a select query with the code below. Currently the issue is it doesn't take the input of the combo boxes as the field input. Is this something that can be solved in SQL or would I have to build a form using VBA?
SELECT Field=[Forms]![TestInteractForm]![Combo18], Field=[Forms]![TestInteractForm]![Combo20]
FROM TestInteract;
Although it may not be apparent, your goal is basically the same as using a query parameter in the SELECT clause to identify a field.
Unfortunately, Access does not support that feature. Your combo box value, which is a field name, will be recognized as a valid text string, but there is no way to inform Access that string should be interpreted as a field name.
The closest you can get would be to use a Switch statement which maps each field name string to the corresponding field value. So, if TestInteract and Combo18 both include 4 fields (fld1, fld2, fld3, and fld4) something like this could actually work ...
SELECT
Switch(
[Forms]![TestInteractForm]![Combo18]='fld1', [fld1],
[Forms]![TestInteractForm]![Combo18]='fld2', [fld2],
[Forms]![TestInteractForm]![Combo18]='fld3', [fld3],
[Forms]![TestInteractForm]![Combo18]='fld4', [fld4]
) AS first_column
FROM TestInteract;
However, I'm not recommending you adopt that method. I think it could be simpler to generate the SELECT statement in your command button's click event.
Dim strSelect As String
strSelect = "SELECT [" & Me.Combo18.Value & "]" & vbCrLf & _
"FROM TestInteract;"
And then use strSelect where you need it ... as a form's RecordSource, as the data source for a recordset, or as the .SQL property of a saved query.

Filter syntax in Access 2010 form macro

I am new to Access programming.
I have a form that I created with Create --> More Forms --> Multiple Items. The form is based on a query that returns values from three fields, two of which are hidden. The three fields are FullName, ID, and LastName. FullName is visible, while ID and LastName are not visible.
I added a text box above the form called txtFilter, and I added a macro to the After Update event of txtFilter, with the intent of filtering out results so that only the FullNames of records whose LastName value is like the value entered in txtFilter.
In the "WHERE=" section of the macro builder, I added the following:
='LastName Like '' & [txtFilter] & '*''
Note the double ==. When I type a value into txtFilter and press return, I get a dialog box that says "Type mismatch."
How can I fix this syntax so that it does what it is supposed to?
You should use double quotes and you need to explcitly identify where txtFilter can be found; that is, that it is a Control on the Form. The following is entered in the Where box (without an equals-sign):
[LastName] Like [Forms]![frmStaff]![txtFilter] & "*"
Replace frmStaff with the name of your form.

Returning multiple records using Recordset MS Access

I have an MS Access form that has a combo box (which acts as a primary key) called cboProjectID. I am trying to create the form in a way so that once a selection is made in the cboProjectID combo box, it triggers another combo box called cboErrCod1 to display error codes based on those available to the project_id selected in the cboProjectID combo box.
Only certain Error Codes are available to certain Project ID's and those are defined in another table. However, i have created a query called HDR_ERRCODES that relates Project_ID, Project_Code and the possible Error_Reason_Code 's available to a specific project_Id.
For example, a project code (which is selected from the cboProjectID combo box) could look something like these: "FI-01-05", "FI-01-01", "SY-02-02" etc). Once selected, the Project_ID is stored in the table Project_DTA_REV_T (this is the table that the form stores its information) and the Project_ID is simply a next up number (1,2,3,4 etc. etc.).
I understand that you need to use a Recordset to return multiple values when doing VBA code. This is the code I have started off with but it doesnt seem to be working in the "on change" command for the cboProjectID combo box field:
Private Sub cboProjectID_Change()
Dim VarComboKey As Integer
Dim dbs As DAO.Database
Dim Err1 As DAO.Recordset
VarComboKey = Me.cboProjectID.Value
Set dbs = CurrentDb
Set Err1 = dbs.OpenRecordset("SELECT DISTINCT [Error_Reason_Code] FROM [HDR_ErrCodes] WHERE [project_ID] = " & VarComboKey)
Do While Not Err1.EOF
Me!cboErrCod1 = Me!cboErrCod1 & Err1!Error_reason_code & " "
Err1.MoveNext
Loop
Err1.Close
Set Err1 = Nothing
End Sub
I am no VBA expert and have been trying to follow the coding methodology i have read on this website for Recordset's. Access is displaying every type of Error_Reason_Code, not the specific Error_Reason_Code 's pertaining to the project ID selected in the cboProjectID combo box.
Can someone point me in the right direction in getting this code to display on the Error_Reason_Code 's in the cboErrCod1 combo box, only pertaining to the Project_ID selected in the cboProjectID combo box??
To change the list of items displayed in a combo box you need to modify its Row Source. There are 3 options for the Row Source Type: Table/Query, Value List and Field List.
Value list will take a semi colon separated list of values and display them for selection. For example if you entered a Row Source of "Cat;Dog;Mouse" you would get the following when selecting your drop down.
cat
dog
mouse
Field List will display the field names of whatever query or table you select as the row source.
Table Query is what you want. This will populate the combo box with the values from the table or query you provide. What you are doing with your code now and the line Me!cboErrCod1 = Me!cboErrCod1 & Err1!Error_reason_code & " " is changing the currently displayed value in the Combo Box but not affecting the values that will be available when you expand it. To do this you have to change the Row Source of your combo box with the query that you have already created.
Me!cboErrCod1.RowSource = "SELECT DISTINCT [Error_Reason_Code] FROM [HDR_ErrCodes] WHERE [project_ID] = " & VarComboKey
So your method would change to be what is below since you do not need to modify a record set.
Private Sub cboProjectID_Change()
Dim VarComboKey As Integer
VarComboKey = Me.cboProjectID.Value
Me!cboErrCod1.RowSource = "SELECT DISTINCT [Error_Reason_Code] FROM [HDR_ErrCodes] WHERE [project_ID] = " & VarComboKey
End Sub
EDIT: I have tried to duplicate your setup to see why the results would return nothing. Please correct this if I have made any errors in my assumptions.
I made a form with a combo called cboProjectID that has a source that gets all of the project IDs from the project table(Select * from Projects;) and displays the Project Code in the drop down. I made another one called cboErrCod1.
I made two tables one called Projects and the other called ErrorCodes with the following set ups based on your description.
Projects HDR_ErrCodes
ProjectID ProjectCode(text) Error_Reason_Code(text) project_ID(integer)
1 FI-01-05 Failure 1
2 FI-01-01 Testing 1
3 SY-02-02 Manual 1
4 SY-01-01 Failure 2
5 SY-01-02
With the above set up and the on change event my combo box cboErrCod1 gets populated with the correct error codes. Is there something wrong with that setup?