How do I use a Checkbox on a form to add a record into a subform in Microsoft Office Access? - ms-access

I have a database for a carpet company. I have a form which enables the user to make a quote, including choosing the customer, etc. There is a also subform dealing with the products and extras, with quantities and unit prices. To state whether the user requires fitting there is a checkbox. When ticked I would like fitting to be added in the subform as one of the products. Products are normally manually chosen using a dropdown.
Important properties are:
Form name: Orders
Subform name: Order Details Subform
Checkbox name: Fitting
Field name for products: Product ID
Subform linked to table: Order Details
Form linked to table: Orders
I'm assuming VBA is needed, or the macro builder.
Anyway thanks in advance!!

I think the easiest way is to use an append query.
If Me.Fitting Then
strSQL="INSERT INTO [Order Details] (ProductID,OtherTextField) Values ("
& Me.ProductID & ",'" & Me.OtherTextField & "')"
CurrentDB.Execute strSQL, dbFailOnError
Me.[Subform control name here].Form.Requery
End If

On the check box control, you are going to add an [Event Procedure] on the After Update event for the checkbox (see the properties window). This will stub out the VBA code, click on the ellipsis ("...") to get to the VBA code.
You can use Remou's code (after a fashion) to insert the new Product:
If Me.Fitting Then
strSQL="INSERT INTO [Order Details] (ProductID,OtherTextField) " & _
"Values (" & _
Me.ProductID & ",'" & Me.OtherTextField & "')"
CurrentDB.Execute strSQL, dbFailOnError
Me.[Subform control name here].Form.Requery
End If
'[code not tested]
However, you probably also want to check that the Product doesn't already exist in for the Order (does your business rules allow for multiple fittings to be scheduled), and you might also want to allow the Fitting product to be removed when it is unchecked - you could do that with an else condition on the if before the "end if":
if 'blah
'blah blah
else
strSQL="Delete [Order Details] " & _
"where ProductID = " & Me.ProductID & " " & _
"and OtherTextField = 'fitting' "
CurrentDB.Execute strSQL, dbFailOnError
Me.[Subform control name here].Form.Requery
end if
'[code not tested]
You will also have to hack your Products sub form so that if you delete the Fitting product from it, you update the check box (or so that you can't delete Fittings that way).
Then again maybe you don't want to use a check box, and just have the fitting as being another drop down option, as the other products are. You could always make sure it's auto-populated into [Order Details] whenever a new Order is created so it can't be forgotten. This would be more consistent with the rest of the product selection user interface.

This is the no code solution:
Get rid of the Fitting check box. Make fitting a product and add it like all the rest. If you want to know if an order requires a fitting (I'm guessing that's why you have the checkbox.), you can create a query to see if fitting is one of the products on your reports/invoices.

Related

Migrating MS Access SQL and VBA functionality into Power App connected to data on Dataverse

After transferring a majority of my MS Access tables into Dataverse, I'm looking to transport this query as a DataSource for a Power Apps search box:
SELECT tblGCPC_ALL.ID, tblGCPC_ALL.DocumentNumber, tblGCPC_ALL.PONum AS [PO#], tblGCPC_ALL.Description, Nz([tbl_Vendors].[Vendor],"Pending") AS Vendor, tblGCPC_ALL.[J-Staff], tblGCPC_ALL.RemoteID, tblGCPC_ALL.Status, Nz([DateReceived],"Pending") AS Received, Nz([tblGCPC_ALL].[Receiver],"Pending") AS Receiver, tblGCPC_ALL.AssignedBuyer, IIf([Discrepancy]=0,"N","Y") AS Discr, Nz([tblGCPC_ALL].[REQ_ID],"N/A") AS REQ_ID, Mid([tblgcpc_all].[DocumentNumber],7,9) AS Expr1
FROM (tblGCPC_ALL LEFT JOIN q_VHUB_ALL ON tblGCPC_ALL.DocumentNumber = q_VHUB_ALL.VendorHolderID) LEFT JOIN tbl_Vendors ON q_VHUB_ALL.VendorID = tbl_Vendors.[Vendor ID]
WHERE (((tblGCPC_ALL.DocumentNumber) Is Not Null))
ORDER BY Mid([tblgcpc_all].[DocumentNumber],7,9) DESC;
The search box would only be about 1.5" long, but the dropdown datagrid that the query produces would produce a grid that is about 7" or more, wide.
Additionally, when the user searches in the box, the grid should grow or shrink depending on any qualified match of each column in the grid. Here is a sample of my KeyUp event in VBA for the search box:
Private Sub cboGCPC_Search_KeyUp(KeyCode As Integer, Shift As Integer)
Dim strSQL As String
strSQL = "SELECT * " _
& "FROM SRCH_GCPC " _
& "WHERE [DocumentNumber] Like '*" & Me.cboGCPC_Search.text & "*' OR [PO#] Like '*" & Me.cboGCPC_Search.text & "*' OR [Description] Like '*" & Me.cboGCPC_Search.text & "*' OR [Vendor] Like '*" & Me.cboGCPC_Search.text & "*' OR [Status] Like '*" & Me.cboGCPC_Search.text & "*' OR [Received] Like '*" & Me.cboGCPC_Search.text & "*' OR [Receiver] Like '*" & Me.cboGCPC_Search.text & "*' OR [J-Staff] Like '*" & Me.cboGCPC_Search.text & "*' OR [AssignedBuyer] Like '*" & Me.cboGCPC_Search.text & "*' OR [REQ_ID] Like '*" & Me.cboGCPC_Search.text & "*';"
Select Case KeyCode
Case 38, 40
KeyCode = 0
Case 1, 9, 13
Exit Sub
Case Else
Me.cboGCPC_Search.RowSource = strSQL
Me.cboGCPC_Search.Dropdown
End Select
End Sub
MS Access also allows either showing or hiding the column headers as well as AutoExpand. I'm sure this should also be available in PowerFX
Because this a typical convention of something complex in MS Access and VBA, I feel that mimicking this exact type of functionality into a Power App would serve as template that covers a lot of questions for my migration.
To replicate the functionality of your MS Access search box and dropdown datagrid in Power Apps, you can use a gallery control to display the search results and a text input control for the user to enter search terms.
Here are the general steps to achieve this:
1.Create a gallery control and set its Items property to a formula that retrieves data from Dataverse using the same SQL query as your MS Access search box. You can use the Filter function to filter the results based on the user's search terms. For example, if the user enters "ABC" in the search box, the formula would look like this:
Filter('GCPC ALL', StartsWith(DocumentNumber, "ABC") Or
StartsWith(PONum, "ABC") Or StartsWith(Description, "ABC") Or
StartsWith(Vendor, "ABC") Or StartsWith(Status, "ABC") Or
StartsWith(Received, "ABC") Or StartsWith(Receiver, "ABC") Or
StartsWith([J-Staff], "ABC") Or StartsWith(AssignedBuyer, "ABC") Or
StartsWith(REQ_ID, "ABC"))
Note that you should replace 'GCPC ALL' with the actual name of your Dataverse table.
2.Add text input control and set its OnChange property to a formula that updates the gallery's Items property based on the user's input. For example, if the text input control is named SearchBox, the formula would look like this:
ClearCollect(SearchResults, Filter('GCPC ALL',
StartsWith(DocumentNumber, SearchBox.Text) Or StartsWith(PONum,
SearchBox.Text) Or StartsWith(Description, SearchBox.Text) Or
StartsWith(Vendor, SearchBox.Text) Or StartsWith(Status,
SearchBox.Text) Or StartsWith(Received, SearchBox.Text) Or
StartsWith(Receiver, SearchBox.Text) Or StartsWith([J-Staff],
SearchBox.Text) Or StartsWith(AssignedBuyer, SearchBox.Text) Or
StartsWith(REQ_ID, SearchBox.Text)))
Note that you should replace SearchResults with the name of a collection that will store the search results.
3.Customize the gallery control's layout to match the appearance of your MS Access dropdown datagrid. You can use the same techniques as in MS Access to show or hide column headers and enable auto-expansion of columns.
4.Add any additional functionality you need, such as sorting the results by a specific column or adding pagination.
By following these steps, you should be able to replicate the functionality of your MS Access search box and dropdown datagrid in Power Apps, while leveraging the power of Dataverse as your data source.
The best approach to migrate this functionality into Power Apps is to utilize the Filter and Search functions. To do this, you will need to create a data source that combines the tables you have previously imported into Dataverse. You can then create a search box and use the Filter function to narrow down the results based on what the user is typing in the search box.
You can also add the ability to show or hide column headers and AutoExpand as you mentioned. To do this, you will use the Visible and Expand/Collapse properties in the Power Apps UI.
Finally, you can use the OnChange event of the search box to set the Filter for the data source of the search results. This way, when a user types something in the search box, the results will be updated accordingly.

Access VBA Keyword Search Function on multiple fields

I currently have a simple Access database with forms for the users to fill out based off a queried table.
My goal is to use a search box that can filter results based off a keyword multiple times. My existing code works great for a single search on 1 field. I want to be able to drill down off the first search by searching off another field. After I select my field from combo box and search keyword, my results are displayed. Once I pick another field from the same box and search, the results do not include my 1st filter.
On the form, I already have a combo box with a list of all the fields to choose from. Then next to that is a text box for the user to search off the chosen field list. I have correct VBA code to search off a single field, but I'd like to drill down from there. Basically, I want the ability to search a keyword on a selected field, and then be able to filter those results further by using the same search box again.
Example: On form, select "borrower" from drop down list and type "Smith" in search box, click search button. THEN I'd like to choose another field such as "Issue Category" from the same drop down list and type "late payment", then click search button. Thus, giving me all records containing the borrower Smith where issues exist of late payments.
I've been spending days on this and finally broke down to come here. I need to know what code I'm needing to add that would accomplish my goal of multiple searches without filter resetting. I am hoping you can help. Here is my code (Text35 is the text box and searchlist is the combobox list of field names):
Private Sub Search_Click()
Dim strSearchValue As String
strSearchValue = Me.Text35.Value
Select Case Me.searchlist.Value
Case "Date"
Me.Filter = "[Date] = #" & strSearchValue & "# "
Case "Account number"
Me.Filter = "[Account number] = #' & strSearchValue & '# "
Case "Borrower"
Me.Filter = "[Borrower] LIKE '*" & (Replace(strSearchValue, "'", "''")) & "*'"
Case "Issue Category"
Me.Filter = "[Issue Category] LIKE '*" & (Replace(strSearchValue, "'", "''")) & "*'"
End Select
Me.FilterOn = True
End Sub
I think you would use the OR keyword instead of &

Access 2002/VBA - how to populate subform from query to allow user to select a row

Consider:
I have a form I need to display payment events for individual clients on. I do not want to display all columns in the payment table, I want user-friendly column names, and I want Access to display a popup form that displays information should the user click on a row of the form (a payment event).
Firstly, I am unsure whether Access 2002 and VBA will allow me to do this. Secondly, I am unsure how to make the individual payment events on the form clickable.
I assume I can do something like:
strSQL = "SELECT payment.payment_id, payment_amount AS Amount, payment_date AS Date" & _
"FROM contact_payment, payment " & _
"WHERE contact_payment.contact_id =" & forms([ContactForm].contact_id & _
"AND payment.payment_id = contact_payment.payment_id"
in order to get the data for the clickable form. Is this the correct way, or is there a better way to achieve this?
You could use the form's DblClick event (or Click event) to open a new form:
Private Sub Form_DblClick(Cancel As Integer)
DoCmd.OpenForm "frmMoreInfo", , , "ID = " & ID
End Sub

Automatically fill fields on an Access form

What is the simplest way for me to get MS Access 2007 to autofill a form based on the entry added in the first field?
I currently have 4 tables.... Customers, Parts, Order Header, and Order Lines. I have created a form for the order header table with a subform for the orderlines.
ideally what i want is that when i add the customer number into the order header form, it autofills the rest of the form with the customer name and details etc etc....
and same principle... when i add the product number to the order lines form, it autofills the Order ID and part description and sales price, taking the info from the parts table and order header table.....
Now i know for the majority of you guys this is bread and butter, but please explain in the simplest form possible... i am by no means 100% computer literate.
Here is an example from a form I use. It uses Dlookup from a table in the same .mdb file. You enter a part number and then everything else populates after you hit tab:
Private Sub Item_Number_AfterUpdate()
PopulateFields
End Sub
Private Sub PopulateFields()
''''PopulateFields takes the Item Number to fill in all the remaining fields with regards to that Item
Me.Full_Desc = RTrim(DLookup("ITEMDESC", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
Me.Item_Type = RTrim(DLookup("ITEMTYPE", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
Me.General_Desc = RTrim(DLookup("ITMGEDSC", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
Me.Current_Cost = RTrim(DLookup("CURRCOST", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
Me.Item_Class_Code = RTrim(DLookup("ITMCLSCD", "GP_Parts_List_Import", "ITEMNMBR = '" & Me.Item_Number & "'"))
End Sub
UPDATE
To get to the VBA part of your application, you press F11 or right click on the control you want triggering the code. After right click, select 'Build Event' and then choose 'Code Builder' to open the VBA Editor window.
The drop down on the left will give you every control you can choose from in that form, and the drop down on the right will give you every event that control has available. So when my text box Item_Number is filled and a user moves on, AfterUpdate is triggered and runs the function PopulateFields.
You'll need to replace the text boxes and table names obviously, and this is only one way to do it. But hope this helps.

Displaying a record in a report when selecting the record ID in a combo box

I have almost finished a project and I would like some user options in the form.
I would like to allow the user to select a product code and when selected, it will find the relative record in the table and then display all the information from that in a report.
Any ideas on the best approach to this would help me out massively.
Use the combo box selection with DoCmd.OpenReport as the WhereCondition option.
So if the report's record source table or query includes a numeric field named product_code and your combo box is named cboProductCode ...
DoCmd.OpenReport "YourReport", _
WhereCondition:="[product_code] = " & Me.cboProductCode
If the field is text rather than numeric, add quotes around the value in the WhereCondition.
WhereCondition:="[product_code] = '" & Me.cboProductCode & "'"