ms access - How to edit dlookup query textbox in vba? - 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.

Related

How do i use Dlookup for Multiple Criteria to create a dynamic Combobox

I have two Comboboxes on a from: txtKategorie and txtTyp.
The values for the frist Combobox (txtKategorie) are fix!
I want the values of the second Combobox (txtTyp) to change according to what the user chooses in the first one.
If the user chooses "Datalogger" the second combobox should only contain "Base Layer Classic" and "Base Layer Plus" as can be seen in the image.
The same idea is true for "Accelerometer".
I've put my code in the AfterUpdate Event of the first Combobox:
If txtKategorie.Value = "Datalogger" And txtTyp.ListCount = 0 Then
i = 1
Do While txtTyp.ListCount < DCount("ID", "tblNomenklatur", "[Kat] = 'K'")
txtTyp.AddItem DLookup("[Typ]", "tblNomenklatur", "[ID] =" & i And "[Kat] = 'K'")
'And "[Kat] = 'K'"
i = i + 1
Loop
When the form opens only the first Combobox "txtKategorie" has Values. When the user chooses Datalogger the code checks how many records in the table have the [Kat] = "K" to define how long the Do While-Statement will run. Then the "txtTyp.AddItem"-Statement should add "Base Layer Classic" and "Base Layer Plus" to the "txtTyp" Combobox. But unfortunately the Code doenst work. There is a problem with the Dlookup-Statement containing tow criterias. If i remove either one of the two criterias the Code works but delivers wrong results for the second Combobox obviously. If i leave it like this the second Combobox stays empty.
Does someone know what im doing wrong?
You can do it easily by below code. Change table name with your table name.
Private Sub txtKategorie_AfterUpdate()
Me.txtTyp.RowSource = "SELECT DISTINCT Table1.Typ FROM Table1 WHERE Table1.Kategorie='" & Me.txtKategorie & "'"
Me.txtTyp.Requery
End Sub
Or you can do it graphically from row source query builder. Below are steps.
Combobox txtKategorie is fix. Fine!
For second combobox txtTyp follow the below steps.
Select combobox txtTyp. From property windows select Row Source then click on query builder ... small three dot. See screenshot.
In query builder window build a query from your data table like screenshot and set criteria for Kategorie column is [Forms]![Form1]![txtKategorie] Save and close the query bulder window.
Now for Combobox txtKategorie in After Update event write below line to requery txtTyp. You are done!
Private Sub txtKategorie_AfterUpdate()
Me.txtTyp.Requery
End Sub

How to use a combo box to fill mutltiple field in a subform in MS-Access?

I have a sub form in Access that allows a user to select records using check boxes. In addition to this sub form there is a combo box with a list of names.
Is it possible to select a few records using the check boxes and assign a name to them with a press of a button?
The button is called [assignButton] , the sub form is called [list subform] and the combo box is called [personCombo] .
This seems like a simple task, but I am unfamiliar with how the VBA for the button triggering this kind of action would be written?
All records are bound to an existing table called "tbl_jobs" this table has the same amount of rows and columns as the subform in the screenshot
Disclaimer: The following code is only intended to illustrate how to update the subform rows, so it includes no error handling or other common process code. Also notice that the SQL statement has no additional criteria in the WHERE clause, primarily because the question lacks the subform's actual RecordSource query and/or information about how the subform table is prepared and/or filtered for each time the form is loaded.
Private Sub AssignPerson_Click()
Dim qry as QueryDef
Set qry = CurrentDb.CreateQueryDef("", _
"UPDATE tbl_jobs SET Person_Name = [ParamName]" & _
" WHERE [Select] = True")
qry.Parameters("ParamName") = PersonCombo.Value
qry.Execute
Me.list_subform.Requery 'Ensure subform displays updated rows
End Sub
Really this question is borderline "Too broad" for Stackoverflow. There are multiple ways to accomplish what you want, and your problem is one of design, coding and understanding. But in the end, this model code is rather simple and satisfies the basic requirements so I feel it not worth nitpicking.

MS Access combo box subform filter afterupdate

I have a combo box, when I select a value, I would like the subform to filter on a particular field with that respective value.
I cannot figure out how to get the Macro Action Builder on After update to function correctly, so I am trying some vba in the afterupdate event field for the combo box.
One line of code in the event field: "SELECT * FROM [fsubtotal] WHERE [Fund Codes] = " & cbo1.Column(0)
fsubtotal is my subform and Fund Codes is the Field I want to apply the filter to. I'm not sure if I should use 0 or 1 for Column(x), as 0 is the key ID in auto number format, but 1 would be the actual fund codes in short text format.
You should be able to link the subform to the combo box in question by selecting the subform container, and then in the property sheet, going to Data -> Link Master Fields. The master field is whatever field you want to connect from the master 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?

How to make subform display data according to combobox selection?

I'm trying to bind multiple tables together and have a subform display data based on my combobox (ID name = CbproductName) selection in a form named Form2. I'm using Ms office Access.
This is the query
SELECT Employee.EmpName, Employee.EmpCode, Employee.CompanyID, Employee.DeptID,
Employee.ComputerID, Software.ProductName
FROM Software
INNER JOIN (
(
Computer INNER JOIN Employee ON Computer.CompID = Employee.ComputerID)
INNER JOIN Application ON Computer.ComputerName=Application.[A-ComputerID]
)
ON Software.ID = Application.SoftwareID
WHERE Application.SoftwareID = Form2.CbProductName;
However it keeps prompting me to enter the parameter value when i run the form, and it doesn't display the result of the entered value. it also doesn't change according to the combobox selection.
Can someone point me at a direction to get it done?
In a query, reference a form by its name as a member of the Forms collection. And then reference the value of a control on that form using the control name. It should look like this pattern:
Forms!FormName!ControlName
In your query, change the Where clause to this:
WHERE Application.SoftwareID = Forms!Form2!CbProductName;
Then make sure to refresh that query in after update event of CbProductName. You indicated the query is used as the record source for a subform, so assuming the subform control is on the same form as the combo box, try this as the after update procedure:
Private Sub CbProductName_AfterUpdate()
Me!SubformControlName.Form.Requery
End Sub
Beware, the subform control name may not be the same as the name of the form it contains. Make sure you use the name of the control.