I am trying to view the data in sub-form. Below is the code which I am trying.
Dim qdfretriveVal As DAO.QueryDef
Dim rs As Recordset
Dim strQry As String
Set qdfretriveVal = CurrentDb.QueryDef("export_excel")
Set rs = qdfretriveVal(14)
rs.OpenRecordset
DoCmd.OpenQuery "export_excel", acViewNormal, acReadOnly
In Access I have saved "export_excel" query as per following.
PARAMETERS val Long;
SELECT Raw_Data_New.A, Raw_Data_New.B, Raw_Data_New.Val
FROM Raw_Data_New
WHERE (((Raw_Data_New.Val)=[val]));
I want to pass the parameter val and view the data in Subform. can any one assist me, how to achieve this output?
Probably the best way to do this is
Create a form with the query you want to display as the Record Source. Do not put any filter on here. No parameters. Nothing. Let it display all the data.
Put your new form as a subform on your main form.
Add a control like a combo box or a bound control (however you want to get the data) and link it to val (what was your parameter)
in the subform's properties go to Data > Link Master Fields > your textbox/combobox/whatever from previous bullet.
then Data > Link Child Fields > Val
Now your subform will be filtered based on your parent form but with no parameters.
Related
I'm using a sub-form to assign multiple tasks at the same time. This sub-form is placed on a form that has common details applicable to all the tasks there. For instance: Customer, Product Code, Part Code etc., Both the form and the sub-form feed the data to the same table. These forms are linked with one of the keys - Line Item, which is present on both the form and the sub-form. The other two keys are Task Title - placed on the sub-form, and Stage ID placed on the main form. Line Item is configured in a way that it populates the value from another open form for both the main form and the sub-form.
But Access isn't allowing me to add any details whatsoever to the sub-form. It gives me the error "You must enter a value for 'TableName.LineItem' field.
Kindly advise.
Two Approaches:
Select Table A and hit create form on the ribbon. Then do the usual prettification like deleting id columns, setting the forms format to continuous forms, and replacing textboxes with combo- boxes where appropriate.
First Approach is put unbound controls and a button in the header. Then insert the values from the controls in the button click event
Private Sub cmbAddRecord_Click()
Dim db As Database
Set db = CurrentDb
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("TableA")
rs.AddNew
rs!A = Me.txtA
rs!B = Me.txtB
rs!C = Me.txtC
rs.Update
Me.Requery 'show changes
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
You can also insert values while actively entering the data for instance
Private Sub A_AfterUpdate() 'A is the TextBox in the detail section that is bound to A in TableA and so forth
Me.B = B.Parent!txtB
Me.C = C.Parent!txtC 'Tip use Me.MyControlName.SetFocus to select a control
Me.Refresh 'show changes
'tip you can also put this code in hotkeys https://learn.microsoft.com/en-us/office/vba/api/access.form.keydown
End Sub
'
I have a small dataset with a maintable and one table being dependent on the maintable. One record in the main table can have multiple related records in the subtable.
I also created a form (maintable) with a subform (subtable).
Afterwards, I created two recordsetclones based on the form and the subform respectively.
Adding the bookmark method also allows me to use data from the record that is actually active in the form. The problem is: the bookmark only applies to the mainform.
I used GetRows to create an array variable to be filled with the records from the subform. The array is filled, but with all records from the subtable. I only want those records which are currently shown on the subform.
I'm using a DAO recordset.
How can I fix this?
My code so far:
Sub FillMail()
Dim rstMail As DAO.Recordset
Dim rstIsin As DAO.Recordset
Dim db As Database
Dim isinarray As Variant
Set rstMail = Forms!Transfert.RecordsetClone
Set rstIsin = Forms!TFT_ISIN_DatasheetSub2.RecordsetClone
Set db = CurrentDb
rstMail.Bookmark = Forms!Transfert.Bookmark
rstIsin.Bookmark = Forms!TFT_ISIN_DatasheetSub2.Bookmark
isinarray = rstIsin.GetRows
MsgBox isinarray(2, 0)
End Sub
If the subform TFT_ISIN_DatasheetSub2 is in a subform control under the main form Transfert, link the Master and Child fields in the sub form control, and change:
Set rstIsin = Forms!TFT_ISIN_DatasheetSub2.RecordsetClone
to:
Set rstIsin = Forms!Transfert!subTFT_ISIN.RecordsetClone
This assumes your subform control is named subTFT_ISIN.
How I can open diferents reports from one button in access if that form that shows me the information has been filtred. For example, in one form I have 2 elements, "t_element"=3 means is ID and name of report is "DNI_CAB", when "t_element"=54 means is Table and name of report is "Table_CAB". And if I filtred another form maybe shows me 8 elements. Thanks!
If I'm understanding this properly, you need to make this table-driven. By that, I mean you need to set up a table with 2 fields; ID and Report Call it tblReports.
Then you would do something like this on the OnClick event of the button:
Dim db as Database
Dim rec as Recordset
Dim MyReport as String
Set db = CurrentDB
'Find the Report you're going to use by filtering by the ID you want
Set rec = db.OpenRecordset("Select Report from tblReports where ID = " & me.txtID & "")
'Fill a variable with the resulting value
MyReport = rec(0)
'Now that we have the report name, open it
DoCmd.OpenReport MyReport
This is all entirely aircode, written off the top of my head, so it probably needs to be altered a little. But it should give you an idea of how to go about writing the code to get this done.
Hope you're enjoying your Friday afternoon!
I've got a form on MS Access which searches a linked table for a barcode (which is entered by the user)
If the barcode exists in the table I'd like to show the user the record which contains the matched barcode, if not the code moves on elsewhere.
Here is what I have so far:
DoCmd.DeleteObject acQuery, "Query2"
Set qdef = CurrentDb.CreateQueryDef("Query2", s)
Me.sbfMatchedRec.SourceObject = "Query2"
I get an error when trying to change the SourceObject to Query2.
If I open Query2 it has the exact information I need in it. I can also go via the form properties and change the SourceObject to Query2 which then displays what I need, but I just need to know how to change this in VBA as above.
Thanks!
You could requery the subform with the barcode passed in from the form's textbox as the WHERE criteria in the subform's .RecordSource property.
For example, say this table (tblProducts) has your list of barcoded records:
You could then add a subform to your form that will show records from tblProducts. Also add a textbox to the form for the user to supply a barcode and a command button for the user to initiate a search:
Before hooking-up the search button with the VBA, take a look at the .RecordSource property for the subform...
...switch the query builder to SQL View:
Copy the sql as you can use this as the basis to filter the records in the subform using VBA.
Go back to the form in design view and go in to the search button's On Click event:
The SQL you've copied can then be used to create an SQL string that takes the value entered in the form's barcode textbox (I've called the text box on the form txtBarcode) as part of the WHERE clause. This sql string can then be applied to the subform's .RecordSource property and then the subform can be requeried to show the result set of the new SQL definition in the subform:
Private Sub cmdSearch_Click()
Dim strSql As String
strSql = "SELECT tblProducts.Barcode, tblProducts.ProductName" _
& " FROM tblProducts"
If _
Me.txtBarcode > "" _
Then
strSql = strSql & " WHERE tblProducts.Barcode=" & Me.txtBarcode
End If
Me.tblProducts_sub.Form.RecordSource = strSql
Me.tblProducts_sub.Form.Requery
End Sub
So if we put 1001 in the text box on the form, and click the search button, the subfom only shows that matching record:
I have an Access 2003 form with one subform, set to continuous forms, in a subform control. For one record in the main form, 1 to many records will appear in the sub form. The data displays properly.
The main form is named Widgets and the sub form is named Transactions. There are 5 textbox controls that display the data in the subform. The one in question is ReceiptDate.
What I would like to do is look at the values and determine if there was a receipt for the year 2009, and if so then change the background of that row to yellow so it stands out when the user encounters that condition. Maybe even change the date field's font to boldface..
I tried many ways of referencing the subform's controls. When I have tried Me.Transactions.ReceiptDate I have only received the first record in that subform. I'd like to be able to loop through them and see if the condition is met. I tried Me.Transactions.ReceiptDate(1) and Me.Transactions.ReceiptDate(0) and so forth.
I tried the For Each ctl In Form.Controls route as well. It worked for a few iterations and then I received a run-time error 2455 "You entered an expression that has an invalid reference to the property Form/Report".
I had the subform in "datasheet" mode but thought that was causing me not to be able to read through an array of subform controls. So I changed it to "continuous" mode. I get the same errors for either.
Is there a way to reference specific "rows" in the subform and do something based on a value found? Also, I am performing this in the On Current event as I dont know where else to put the code. The subform loads before the parent form so its possible that these controls arent even fully "there" but then I do get the first row's date when I try it in the Immediate Window.
UPDATE 12.23.2010:
With the gracious help of #Remou I am able to debug.print the ReceiptDate fields from the RecordSet. This is great because now I can evaluate the data and do certain things based on the values.. The #Remou's code helped me put this into the OnCurrent event:
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
' Get the form and its recordset.
Set frm = Me.Form
Set rs = frm.RecordsetClone
' Move to the first record in the recordset.
rs.MoveFirst
' Move to the first selected record.
rs.Move frm.SelTop - 1
' Enumerate the list of selected records presenting the ReceiptDate field
For i = 1 To rs.RecordCount
Debug.Print rs![ReceiptDate]
rs.MoveNext
Next i
So now that I am able to know which row in my subform has a receipt from 2009, I need to be able to highlight the entire row or rows as I come across them in that for loop. How can I reference the actual row? Datasheet view or Continuous Forms view - I have tried both.
Conditional Formatting is great but it only allows me to highlight one particular record and I'd much rather be able to do this via VBA because...... from here I will want to give the use the ability to click on any record in the subform and get the receipt details and potentially print them.
Any ideas?
In this situation, it is best to use conditional formatting.
To refer to a control on a subform, refer to the subform control by name, then the form property to get the form contained, then the control by name:
Me.MySubformControlName.Form.MyControl
See: http://www.mvps.org/access/forms/frm0031.htm
I have finally got it. The frm.SelTop = x will set the selected record and from there I can set the background or font style, etc.. Very cool. A simple test for 2009 and setting the selected record:
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
' Get the form and its recordset.
Set frm = Me.Form
Set rs = frm.RecordsetClone
' Move to the first record in the recordset.
rs.MoveFirst
' Move to the first selected record.
rs.Move 0
' Enumerate the list of selected records presenting
' the CompanyName field in a message box.
For i = 1 To rs.RecordCount
If Year(rs![ReceiptDate]) = 2009 Then
frm.SelTop = i '<-----------------------------
End If
rs.MoveNext
Next i
In order for me to get the email off the bottom of my continuous form, I used this much simpler code (as I avoided the RecordsetClone code)
Me.[email subform].Form.SelTop = Me.[email subform].Form.Count 'selects the last row
str = Me.[email subform].Form.Email 'capture the value of the last row
MsgBox str