MS Access query not receiving parameter from VBA - sql

I have an append query that copies values from one table (tbl_LSI) to another (tbl_LSI_USD):
qry_Append_to_LTI_USD:
INSERT INTO tbl_LTI_USD
SELECT *
FROM tbl_LTI AS lti
WHERE lti.LTI_ID=[Forms]![frm_LTI]![LTI_ID];
I call this query on the AfterUpdate event of the form frm_LTI, where it takes the LTI_ID from the field LTI_ID. The VBA I use is:
db.Execute "qry_Append_to_LTI_USD", dbFailOnError
I have this exact same code working for a different form & table combination, but for some reason when I try and execute this one it fails and asks for a parameter:
Run-time error '3061'
Too few parameters. Expected 1
The only difference between the forms is that this one (frm_LTI) is modal & popup = Yes, whereas the working form modal & popup = No
The VBA code can see and debug.print the value [frm_LTI]![LTI_ID], but it doesn't get passed to teh query. (I have a screenshot showing this, but not enough reputation points to upload it).
Is this anything to do with the form's modal/popup property, or is there something else I'm missing?

When you have such problems with Execute, a specially with pivot queries with sub queries. Try to replace WHERE lti.LTI_ID=[Forms]![frm_LTI]![LTI_ID]; with this WHERE lti.LTI_ID=eval("[Forms]![frm_LTI]![LTI_ID]");. But sometimes type cast also needed(for dates as example): WHERE lti.LTI_ID=cDbl(eval("[Forms]![frm_LTI]![LTI_ID]"));

Related

How do I set parameters for an embedded query on an Access report from an Access form?

I have an existing Access report that runs off a Query. In the existing report I have the parameters set with brackets; [CSRName], [StartDate], and [EndDate]. I have those parameters being filled form a form using the VBA below;
All of this works just fine. My problem is this; I have this query counting the total number of errors a CSR makes. I can easily display in the report the total number of errors. What I'd like to do is have another textbox on the report that shows the total number of completed items by the CSR. In order to do this I need to embed the Control Source for the textbox with another query. I have that query written and it works outside the form. What I need to do though is pass the same parameters that I am using to generate the report into this embedded query on the report. I can't figure out how to do that. I can pass the parameters to the report itself, and it works fine, but trying to do the same won't pass the parameters to the embedded query in the report. It keeps popping up an input box and asking me to give it the variables.
I assumed I could treat the embedded query the same that I could the query the report is based off, so I used the same process to try to pass the parameters to it as well, but it did not work.
Private Sub RunQuery_Click()
DoCmd.SetParameter "CSRName", Me.CSRNameCB
DoCmd.SetParameter "StartDate", "#" & Me.StartDate & "#"
DoCmd.SetParameter "EndDate", "#" & Me.EndDate & "#"
DoCmd.OpenReport "rpt_CSRErrorTracking", acViewPreview
End Sub
What I need is the method to pass the parameters from the form to both the query that the report is based off, and the query embedded in the report itself.
How about having the queries that the report and report field are based on store the "link" to the form's fields something like this (query by form concept)...
In the query's criteria for an appropriate field:
= [Forms]![MyForm]![CSRNameCB]
And:
Between [Forms]![MyForm]![StartDate] And [Forms]![MyForm]![EndDate]
And then on the form that opens the report you don't have to set any parameters at all.

MS Access Database form not updating after SQL requery

I have a report that runs from a query. The query does use a global variable but this is not the problem but needed for the explanation. The function for the variable is:
Function Var1() As String
Var1 = strVar1
End Function
The query where statement is:
WHERE (((IIf([MinOfDueDayMin]<0,0,Int([MinOfDueDayMin]/7)+1))<Var1()+1) AND ((tblEquipment.Retired)=False))
which uses the var1 function
The criteria is on a field that is actually a calculation and that is where I think the problem starts.
The report is run for a command on another form using the following code:
strVar1 = InputBox("Enter Number of Weeks for report")
If strVar1 = "" Then Exit Sub
DoCmd.OpenReport "rptEquipPmSchedule", acViewReport
Everything works just fine
On the report I have a double click event that opens a form. This form uses part of the same query. (not the same one but two levels higher) thiS allow the user to change things so i expect to use requery for the report.
If i double click and then not even change anything and then go back to the report I have #ERROR in the fields that have the calculations
i put a me.requery in the activate event of the report. this did not work.
So I tried a work around.
When I double click the report field, i close the report and send the strVar1 value to the form that is opened. then when I close the form I reasign the strVar1 just in case it is lost be an assignment by another user (currently I am the only one using this but did it just to be sure it had the correct value.) Then I open the report again but still get the errors. I did not expect this at all. thought starting the report from scratch would certainly work. I even closed the form just after assigning strVar1.
then in final effort. When I close the form I run the exact same code:
strVar1 = InputBox("Enter Number of Weeks for report")
If strVar1 = "" Then Exit Sub
DoCmd.OpenReport "rptEquipPmSchedule", acViewReport
Which will force the user to input the value for strVal1. Even though this is not what I want but tried this for troubleshooting and I still get #ERROR.
When I run the report for a form that does not have any of the same field, no issues. When I run the report or keep it open with a requery from the form that has the same data, the report will not give the correct results. Note that if I run the query itself, the data in the query is correct.
i also tried using a number instead of Val1() in the query and got the same results.
i also tried the refresh button in the ribbon and get Unknown Function Name and all the data in the report is lost.
Anyone got any ideas??
While your textual explanation is difficult to understand the entire scope, consider re-assessing your workflow. The entire objective is to allow users to run customized criteria for reporting. And your main issue is the strVal does not persist in memory so all references to it fails.
Consider the following points:
Have users set criteria on a dedicated unbound form with button click for report where that report instance is immutable for viewing/printing only and if needed to be changed must be re-run (i.e., button re-clicked).
Access has no need for VBA's InputBox() as strVal can be an unbound textbox on this unbound form whose value remains intact for all open windows.
Have function and all its references point to form field: Form!myFormName!strValuetextbox
Because reports on pretty much any software/web app system is not used as a GUI interface to run actions, users will know if they intend to change report criteria, close current report or go back to entry point and change strVal then re-click button to re-run report.
Keep data entry/input (primary use of forms) separate from data export/output (like reports). From developer and user standpoint this compartmentalization will save you headaches down the road.

Ms Access and SQL: how to identify where error occurs

I have a form in MS Access where a lot of queries are running to calculate all kinds of numbers using user input and using other queries ouput. All of a sudden I get the error below:
Error text is:
The record source ' ' specified on this form or report does not exist.
The name of the recordsource may misspelled, the recordsource was deleted or renamed, or the recordsource exists in a different database.
In the Form or Report's design view or layout view, display the property sheet by clicking the properties button, and then set the recordsource property to an existing table or query.
I don't remember exactly what I added/removed to have caused this and I can't find where the error occurs. How can I, I guess, debug my Access Form while it's running queries?
I don't see your image as it is blocked at work.
The only one way to debug the application when errors occurs is to break the execution using the keyboard combination :
CTRL + Pause/Break
Do it when the error message appears
This obviously doesnt work if the application is compiled in a mde/accde
When the CTRL-Break doesnt work, you have to add a break point in your code.
Bring the VBA window using
ALT + F11
Open the code of your form, go in the procedure that triggers the queries, select a line of code at the beginning and add a breakpoint using F9
Then run the form, it will stop the execution on the breakpoint and show you the VBA window, continue to execute the code step-by-step using F8 until the error occurs
When ever you change the name of a table in ms access for that has drop down list, go to the design view --> property Sheet--> Row Source and change the names to the corresponding names for the drop down selection for combo selection

Search functionality on form not working properly

I have a form Main with a subform Issue on it. To implement search functionality on Main so that users can search for records in Issue that have a given substring, Main has a text box keyword and a submit button SubmitBtn. Here is the VBA code I am using to try to make this work:
Private Sub SubmitBtn_Click()
Dim keyword As String
Dim recordSourceSql As String
keyword = Nz(Me.keyword.value)
recordSourceSql = "select * from [Issue] where [Details] like " & quoteWrap(keyword)
Me.Issue.Form.RecordSource = recordSourceSql
Me.Issue.Form.Requery
End Sub
Private Function quoteWrap(value As String) As String
quoteWrap = "'*" & value & "*' "
End Function
The problem is that after this line:
Me.Issue.Form.RecordSource = recordSourceSql
there is only one record showing in Issue--it's the first record in the original recordset, when there should be at least 20 records showing with the value of keyword that I tested. Once this occurs, the Me.Issue.Form.Requery call does not change the contents of Issue.
I know that the correct recordSourceSQL is being created, because when I put in, e.g., "data" for keyword, I get this string for recordSourceSQL:
select * from [Issue] where [Details] like '*data*'
and when I create a query in Access and set this as the SQL, I get all the correct results returned.
What's wrong with this code to search the subform according to the given criteria?
UPDATE: I was able to get this to work by setting Me.Issue.Form.Filter to the WHERE clause in recordSourceSql. I don't understand why .Filter works but changing .RecordSource doesn't.
UPDATE 2: The .Filter solution is not working either. I've described this issue in this SO question.
I created a sample, copied your code and was able to replicate your problem and solve it.
If your subform was unbound (i.e., recordsource was empty) and the primary key of your main table and Issue table had the same field name (e.g., both were named "ID", etc.) and you did not link any parent or child fields, then it will only work if you specifically name each field like this:
recordSourceSql = "SELECT Issue.IssueID, Issue.Details FROM Issue WHERE Issue.Details Like '*Data*'"
You are correct! Wildcards in the select don't work in this scenario, including Select Issue.* From Issue.
Alternatively, if you rename the primary key in your Issue table so it is not the same as your main form main table, then the wildcards work as you would expect. When I made this change your exact code worked:
recordSourceSql = "SELECT * FROM Issue WHERE Details Like " & quoteWrap(Keyword)
Note the Me.Issue.Form.Requery is not necessary. Just setting or changing the RecordSource automatically requeries.
It does not seem to matter if the two primary key fields are dragged to the main or subform. It also doesn't help to make the subform databound but empty with an initial recordsource in the property sheet of "Select * from Issue Where 1=2" (one way to create an empty recordset, but keep the form bound).
I don't know if this is an MS-Access quirk or something intentional. It seems to me that I must have come across this scenario many times (primary key was "ID", subform unbound, no child fields linked) but I don't recall coming across this limitation. Maybe I didn't use a wildcard. When I googled, I didn't find this reported by others but no doubt some MS-Access expert out there will know the reason.
Hope this helps.

Access DoCmd.OpenForm Not Working

Looking for a second set of eyes to figure out my problem with an Access form filter. I created a search form, when filled in, appends search criteria to a string variable (strQuery) that is put in place to the [WhereCondition] for opening a form. However, when the script is ran, nothing comes up except for a filtered form with no records.
Here is the line that opens the form:
DoCmd.OpenForm "ADD_NEW_NCMR", , , strQuery
Before the line is ran, strQuery equals:
1=1 AND [NCMR].[NCMR_NUM] = '12-129'
The form name, and table.column combination are all correct. In fact, using the DCount function returns the result of 1, which is correct for this query, and returns the correct number for other queries as well. This makes me think that there is nothing wrong with the where condition.
DCount("[NCMR_NUM]", "NCMR", strQuery)
Check your form's Data Entry property. You can find it on the Data tab of the form's property sheet.
If Data Entry = Yes, the form will not display existing records.
Sounds like you want Data Entry = No, so that existing records which match your OpenForm WhereCondition will be displayed.