How to Call A Query Into A Form In Access? (ms-access) - forms

I have a query that you enter a number, and you get a result (it calculates something).
Now, I have a form with two inputs. In one input you type a number. After you type the number, I want my query to be called with the input's value as an argument, and then generate the number from query's result to the second input.

Here's an example VBA:
Private Sub Refresh_Button_Click()
Dim strSQL as String
Dim inputbox1 as String
Dim myR As Recordset
'set the input1 as a string
inputbox1 = Me.input_box_1
'this select statement creates a SQL string
strSQL = "Select whatever from table_name where field = '" & inputbox1 & "'"
'this recordset pulls your SQL statement so you can get your fields
Set myR = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
'this gives the value of your second input box
Me.input_box_2 = myR!field_you_want_to_appear
Me.Refresh
Set myR = Nothing
End Sub

Related

Access pass-through query with parameters not updating with prompted parameters

An Access pass-through query works when using the default parameters. When used in an Access report, the prompts that are used returns records based on the default parameters in the ptq and not the answered prompts. Default data is being returned.
I have a SQL Server based stored procedure that works, uspWorkCentreReport, that uses #TheDate DATE, #WC VARCHAR(15), #Shift INT for parameters and returns, through a SELECT statement, these columns:
[JOB NUMBER], [REL #], [JOB NAME], QTY.
Here's the ALTER line of the stored procedure code:
ALTER PROCEDURE [dbo].[uspWorkCentreReport]
#TheDate DATE,
#WC VARCHAR(15),
#Shift INT
The Access pass-through query, ptq_uspWorkCentreReport, passes these default parameters '2019-05-30','PCOT',1 and uses a DSN-less ODBC connection that works to return default data. I forgot to try but I think it will return correct data with whatever default parameters I use to replace '2019-05-30','PCOT',1. EDIT - I tried it this morning and indeed any appropriate replacement parameters return the appropriate associated records. Here's the ptq's one line:
exec uspWorkCentreReport '2019-05-30','PCOT',1
I provide the ptq with default parameters based on Albert D. Kallal's SO reply.
I use an Access select query, qry_ptq_uspWorkCentreReport, to receive [JOB NUMBER],[REL #],[JOB NAME],QTY and pass the parameters TheDate, set to Date With Time, WC, set to Short Text, and Shift, set to Integer.
qry_ptq_uspWorkCentreReport uses the pass-through query. The parameters are set using Access' Parameters applet and not within the query fields. Running this select query prompts for the 3 parameters but only returns data based on the default parameters set in the ptq's one line. I did not think to look at the Access SQL statement but will do so when I get to work tomorrow morning. EDIT - Here's the SQL statement for qry_ptq_uspWorkCentreReport:
PARAMETERS TheDate DateTime, WC Text ( 255 ), Shift Short;
SELECT ptq_uspWorkCentreReport.[JOB NUMBER], ptq_uspWorkCentreReport.[REL #], ptq_uspWorkCentreReport.[JOB NAME], ptq_uspWorkCentreReport.QTY
FROM ptq_uspWorkCentreReport;
Of course the above three functions culminate in an Access report, rpt_qry_ptq_WorkCentreReport to make the records human readable.
I have used the same scenario for another report the takes From and To dates as parameters. When that report runs, the prompts take the dates and return records based on those dates and not the dates in the ptq. Here's that ptq:
exec uspMergeAandPJobs '2018-01-01','2019-01-01'
Indeed, I tried using
exec uspMergeAandPJobs '',''
And the report returns 0 records!
Not sure what I am missing and would appreciate any feedback. TIA.
I tried the following with the help of a tutor:
Sub Report_Load()
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
Dim qdf As DAO.QueryDef, rst As DAO.Recordset
Set qdf = CurrentDb.CreateQueryDef("")
qdf.SQL = "exec dbo.uspWorkCentreReport " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
qdf.Connect = "ODBC;DRIVER=ODBC Driver 13 for SQL Server;SERVER=OURS\NTSQL;Trusted_Connection=Yes;DATABASE=TablesCoE;ApplicationIntent=READONLY;"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset
rst.Close
Set rst = Nothing
Set qdf = Nothing
End Sub
After the prompts VBA spits up a Run-Time error 3129 - Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'. Neither of us were able to determine what was causing the error. In VBA the "qdf.SQL..." line is highlighted in yellow.
EDIT - Adding stored proc's SQL code:
ALTER PROCEDURE [dbo].[uspWorkCentreReport_TEST] #FromDate DATETIME,#ToDate DATETIME,#WC VARCHAR(15),#Shift INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Build table variable SumTable structure
DECLARE #SumTable TABLE(matl_nbr VARCHAR(60),QTY DECIMAL(4,0),matl_dsc VARCHAR(50))
-- P jobs and their summed WorkCentre traversals using crosstab - each traversal is added up
INSERT INTO #SumTable(matl_nbr,matl_dsc,QTY)
SELECT SRC1.matl_nbr,SRC1.matl_dsc,
SUM(CASE WHEN SRC1.locn_to = #WC THEN 1 ELSE 0 END) AS QTY
FROM
(
SELECT matl_nbr,matl_dsc,locn_to
FROM mtrk_CompanyE.dbo.trxn_hstd th
WHERE (last_upd >= #FromDate AND last_upd <= #ToDate) AND
locn_to = #WC
)SRC1
GROUP BY matl_nbr,matl_dsc
-- These updates take all the summed WorkCentre (locn_to) columns and turn each into "1" for later summing
UPDATE #SumTable
SET QTY = 1
WHERE QTY >1
-- Shortening the material number from 123456_00_00_R1_00 to 1234560
UPDATE #SumTable
SET matl_nbr = LEFT(matl_nbr,6) + right(LEFT(matl_nbr,9),1)
SELECT LEFT(A.matl_nbr,6)[JOB NUMBER],SUBSTRING(A.matl_nbr,7,1)[REL #],matl_dsc AS [JOB NAME],QTY
FROM (SELECT matl_nbr,matl_dsc,
SUM(CASE WHEN QTY = 1 THEN 1 ELSE NULL END) AS QTY
FROM #SumTable
GROUP BY matl_nbr,matl_dsc)A
ORDER BY QTY DESC;
END
EDIT - Finished sub:
Private Sub Report_Open(Cancel As Integer)
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("Enter From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
strSQL = "exec dbo.uspWorkCentreReport_TEST " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQL
DoCmd.OpenReport "rpt_qry_ptq_uspWorkCentreReport", acViewReport
Me.lblFromDate.Caption = strFromDate
Me.lblToDate.Caption = strToDate
Me.lblWC.Caption = strWC
Me.lblShift.Caption = intShift
End Sub
Your Access query has parameters:
PARAMETERS TheDate DateTime, WC Text ( 255 ), Shift Short;
and since they are defined in the query definition, Access asks for them when opening/running the query.
But these parameters are never used!
There is no way for Access to pass these parameters into the pass-through query that is the basis of the Access query. Again, a PT query is nothing more than a Connect string and a constant SQL string.
So when you run the Access query, it will always run the saved contents of the PT query, i.e.
exec uspWorkCentreReport '2019-05-30','PCOT',1
The parameters you entered are ignored.
What you need to do (as outlined in the answer you refer to):
create a form to collect the parameter values
dynamically create the SQL string for the PT query with VBA
assign that SQL to the PT query:
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSql
(it is automatically saved)
and then you can run the report based on the Access query - or better: directly use the PT query as record source for the report.
Remove the parameters from the Access query, they are of no use for your situation. Or remove the query entirely, unless you need it to join the PT query with something else.
Edit for above edit:
If you get a runtime error, there is probably a syntax error in your .Sql. Build the SQL string in a variable, do Debug.Print strSql, and run that string in SSMS. You may need to change date formatting (depending on your locale settings).
Also: See my 3rd bullet. Defining a temporary querydef and opening a recordset doesn't work for a report. You must assign the .Sql of the existing query that is the record source of the report.
Addendum: if you need to create a new query, first set .Connect, and then .Sql, so Access knows it's a Pass-Through query.
Access SQL doesn't know exec.
Edit 2
You have an existing, working PT query ptq_uspWorkCentreReport, which returns records for one set of parameters, e.g.
exec uspWorkCentreReport '2019-05-30','PCOT',1
Use this query as record source for your report.
To run the report with different parameters, you must modify the query's SQL. You can do this manually in query design view, or with VBA.
I think Report_Load() is too late for modifying its record source (the PT query). Run the following sub, then open the Report.
Sub SetUspParameters()
Dim strFromDate As String
Dim strToDate As String
Dim strWC As String
Dim intShift As Integer
Dim strSQL As String
strFromDate = InputBox("From Date and Time: ")
strToDate = InputBox("Enter To Date and Time: ")
strWC = InputBox("Enter Work Center: ")
intShift = InputBox("Enter Shift: ")
strSQL = "exec dbo.uspWorkCentreReport " & "'" & strFromDate & "', " & "'" & strToDate & "', " & "'" & strWC & "', " & intShift & ";"
Debug.Print strSQL
' This line is all that's needed to modify the PT query
CurrentDb.QueryDefs("ptq_uspWorkCentreReport").SQL = strSQL
End Sub
In practice, you don't want to use 4 x InputBox, but a form.

how to use a form to navigate to specific query based on what is entered on the msgbox?

So I have a Access database with a table transaction.
On the table there is a column called profit filtered by month.
I have already made a few queries to calculate the total profit on monthly basis (eg. Jun,July,Aug)
So is it possible to create a form with a pop-up message box, and by enter a month number on the box and click, it will lead to a specific query?
I think it is a good idea but I know little about ACCESS programing so any comment are much appriciated!
You can use VBA's InputBox function to create the pop-up. If your queries already exist in the database, you could then use the results of that function to determine which query to open. For example:
Public Sub OpenExistingQuery()
Dim intMonth As Integer
intMonth = InputBox("Please enter a month number:", "Enter Month Number")
DoCmd.OpenQuery "qryMonth" & intMonth, acViewNormal
End Sub
Alternatively, you could use the results of the InputBox function to dynamically build a query, and then open it:
Public Sub OpenDynamicQuery()
Const strQueryName As String = "qryDynamicMonth"
Dim db As DAO.Database: Set db = CurrentDb
Dim qdf As DAO.QueryDef
Dim intMonth As Integer
intMonth = InputBox("Please enter a month number:", "Enter Month Number")
On Error Resume Next
DoCmd.Close acQuery, strQueryName, acSaveNo
DoCmd.DeleteObject acQuery, strQueryName
On Error GoTo 0
Set qdf = db.CreateQueryDef(strQueryName)
qdf.SQL = "SELECT * FROM your_table_name WHERE your_monthnumber_column = " & intMonth
qdf.Close
DoCmd.OpenQuery strQueryName, acViewNormal
Set qdf = Nothing
Set db = Nothing
End Sub
Please note that the above functions are of the "quick-and-dirty" variety. They really need better error handling, sanitation of user input, etc. But hopefully you get the idea, and can take care of that on your own.

Is it possible to pass a "Enter Parameter Value" to form if query fails?

I was working with an Access 2013 database and had a question about the "Enter Parameter Value" box. I am using a Form whose record source is tied to a "Select" query.
If the query finds the result I'm looking for, it populates the form with its values. If the query fails, it keeps the form blank for a new entry to be made by users.
Some of my users have been complaining that they'd like to pass the value they initially put into "Enter Parameter Value" to the form if the query fails so they don't have to enter data twice into the form.
Is it possible to pass a value from "Enter Parameter Value" to the form box instead of too a query?
One way would be enter the value in a form box and then programmatically pass the value as parameter to your query.
some pseudo:
Open the querydef
Set the parameter value
Set the query as form's rowSource
Some code:
check here:
http://bytes.com/topic/access/answers/887449-how-pass-parameter-value-query-via-vba
in Access 2010 and 2013
This uses DAO and might be of interest
DIM MyQryDef as querydef
Dim a as string
a = ""
a = a & "PARAMETERS Parameter1 INT, Parameter2 INT; "
a = a & "SELECT f1, f2 FROM atable WHERE "
a = a & "f3 = [Parameter1] AND f4 = [Parameter2] "
a = a & ";"
Set MyQryDef = currentdb().CreateQueryDef("MyQueryName", a)
MyQryDef.Parameters("Parameter1").Value = 33
MyQryDef.Parameters("Parameter2").Value = 2
' You could now use MyQryDef with DAO recordsets
' to use it with any of OpenQuery, BrowseTo , OpenForm, OpenQuery, OpenReport, or RunDataMacro
DoCmd.SetParameter "Parameter1", 33
DoCmd.SetParameter "Parameter2", 2
DoCmd.Form YourFormName
' or
DoCmd.SetParameter "Parameter1", 33
DoCmd.SetParameter "Parameter2", 2
DoCmd.OpenQuery MyQryDef.Name
See here:
https://msdn.microsoft.com/en-us/library/office/ff194182(v=office.14).aspx
Harvey

Access Form / VBA: Concatenated Field Name

I created a Form in Access, and within this form there is a button which will allow the user to insert a record into a table ("Tbl")
The format of this table is:
Report1_Field1
Report1_Field2
Report2_Field1
Report2_Field2 (and so on)
The Form will ask the user to:
- Select the report name ("ReportName"); which could be "Report1" or "Report2"
- Input a value for Field1 and Field2
The VBA code behind the button is as follows:
Private Sub ButtonUpdate
Dim NameReport as String
Dim FirstField as String
Dim SecondField as String
NameReport = ReportName
FirstField = ReportName & "_Field1"
SecondField = ReportName & "_Field2"
DoCmd.RunSQL "INSERT INTO tbl (FirstField, SecondField) VALUES (Field1, Field2)"
End Sub
I am however getting the Run-Time error 3127: The INSERT INTO statement contains the following unknown field name: 'FirstField'. Make sure you have typed the name correctly, and try the operations again.
Thoughts?
The SQL string is reading "FirstField" and "SecondField" as literal text instead of using your variables. Try this:
DoCmd.RunSQL "INSERT INTO tbl (" & FirstField & ", " & SecondField & ") VALUES (Field1, Field2)"

Auto Populate fields in MS Access Form

Is there a way to automatically populate fields in an MS Access form? Lets say the user makes a selection from a specific combo box on the form, is there something that can be done to automatically select the other fields on the form based on the PK?
Id like to add that the fields to auto populate would come from various tables..
***ammendment
I need to return multiple values once i select a specific record in the combo box. Can someone help? The multiple values will come from a query that returns values like this:
ID Code Count
24 TST 4
24 BPB 7
24 SSS 10
In the form, the combo box would chose the ID number. Once I choose an ID number of 24, i want to return all 3 records above that come from a query called Project_Error_Final (in this example there are 3 values to return, but i want the query to return any records with ID = 24). The VBA code i have so far is:
Private Sub cboProjectID_Change()
Dim VarComboKey As Integer
Dim VarObjective As Variant
Dim VarStartDate As Variant
Dim VarEndDate As Variant
Dim VarRiskCategory As Variant
Dim VarTarDatSet As Variant
Dim VarErrorCount As Variant
Dim VarErrorCode As Variant
VarComboKey = Me.cboProjectID.Value
VarObjective = DLookup("[Objective]", "[Project_HDR_T]", "[Project_ID] = " & VarComboKey)
Me.txtObjective = VarObjective
VarStartDate = DLookup("[Start_Date]", "[Project_HDR_T]", "[Project_ID] = " & VarComboKey)
Me.txtStartDate = VarStartDate
VarEndDate = DLookup("[End_Date]", "[Project_HDR_T]", "[Project_ID] = " & VarComboKey)
Me.txtEndDate = VarEndDate
VarRiskCategory = DLookup("[Risk_Category]", "[Project_HDR_T]", "[Project_ID] = " & VarComboKey)
Me.txtRiskCategory = VarRiskCategory
VartxtTarDatSet = DLookup("[Targeted_Dataset]", "[Project_Targeted_Dataset]", "[Project_ID] = " & VarComboKey)
Me.txtTarDatSet = VartxtTarDatSet
VarErrorCount = DLookup("[Count_Error_Codes]", "[Project_Error_Final]", "[project_ID] = " & VarComboKey)
Me.txtErrorCount = VarErrorCount
VarErrorCode = DLookup("[ErrorCode]", "[Project_Error_Final]", "[project_ID] = " & VarComboKey)
Me.txtErrorCode = VarErrorCode
End Sub
The value in question is the VarErrorCount and VarErrorCode. In the VBA code above, only a single value is returned. But, I am looking for multiple VarErrorCount and VarErrorCode values to be returned in my form once the ID combo box field is selected. In this particular example VarErrorCode should return "TST", "BPB" and "SSS." The VarErrorCount should return the corresponding VarErrorCode values: "4","7","10"
With regards to your multiple returns, you can't use a DLookup, but I will show you how you can achieve the result you want, as per your description.
In this particular example VarErrorCode should return "TST", "BPB" and "SSS." The VarErrorCount should return the corresponding VarErrorCode values: "4","7","10"
Change your last 4 lines above the End Sub to the following:
Dim dbs as DAO.Database
Dim rst1 as DAO.Recordset
Dim rst2 as DAO.Recordset
Set dbs = CurrentDb
Set rst1 = dbs.OpenRecordset("SELECT [Count_Error_Codes] FROM [Project_Error_Final] WHERE [project_ID] = " & VarComboKey)
If rst1.RecordCount > 0 Then
rst1.MoveFirst
Do Until rst1.EOF
VarErrorCount = VarErrorCount & rst1!Count_Error_Codes & ","
rst1.MoveNext
Loop
' Remove the last comma
VarErrorCount = Mid(VarErrorCount, 1, Len(VarErrorCount) - 1)
End If
Set rst2 = dbs.OpenRecordset("SELECT [ErrorCode] FROM [Project_Error_Final] WHERE [project_ID] = " & VarComboKey)
If rst2.RecordCount > 0 Then
rst2.MoveFirst
Do Until rst2.EOF
VarErrorCode = VarErrorCode & rst2!ErrorCode & ","
rst2.MoveNext
Loop
' Remove the last comma
VarErrorCode = Mid(VarErrorCode, 1, Len(VarErrorCode) - 1)
End If
rst1.Close
Set rst1 = Nothing
rst2.Close
Set rst2 = Nothing
dbs.Close
Set dbs = Nothing
Me.txtErrorCount = VarErrorCount
Me.txtErrorCode = VarErrorCode
Yes there is!
Obviously, you need to be able to relate the combo box selection to the value you wish to be populated into the other field(s). Assuming that you have a 1:1 relationship with the PK (since you want to display only one value in your form), you can use the AfterUpdate event plus the DLookup() function to retrieve a related value using the PK.
As a simple example, I set up a table named Foods as follows:
FoodID, FoodName, FoodCategory
1, Orange, Fruits
2, Chicken, Poultry
3, Almond, Nuts
4, Lettuce, Vegetables
In the form, I have a control that selects the FoodID as the PK bound value named ComboFoods, and an unbound text box control named TextFoodCategory that we will populate with the FoodCategory from the Foods table.
I've assigned the following code to the AfterUpdate event of the Combo Box so that when the value of the combo box changes, the text box will be populated:
Private Sub ComboFoods_AfterUpdate()
'Create a variable to store the combo box primary key selection
Dim VarComboKey As Integer
'Create a variable to store the DLookup results
Dim VarFoodCat As Variant
'Capture the primary key of the combo box
VarComboKey = Me.ComboFoods.Value
'Retrieve the related field value
VarFoodCat = DLookup("[FoodCategory]", "[Foods]", "[FoodID] = " &
VarComboKey)
'Set the value of the text box to the variable
Me.TextFoodCategory.Value = VarFoodCat
This will return the FoodCategory that is related to PK. This is all using one table, but the DLookup statement can be modified to reference any query or table that contains the PK.
Please note that DLookup will only work properly when the PK is unique in the data you are referencing. It will not work in a one to many relationship unless you specify other criteria that restrict the results to one record. There are other ways to use SQL queries and recordsets within VBA if you need to return multiple records, but that this out of scope for this question.
This worked when tested - best of luck!