Too Few Parameters in Access VBA but works in Query builder - ms-access

I am using the below SQL, it works fine if I run it from query builder but once I have put it in VBA it throws out an error:
Code:
With CurrentDb.CreateQueryDef("", "SELECT [_tbl_Structure].[User Name], tbl_Genesys_Daily.Field32, [_tbl_Structure].[Supervisor Emp Num], [_tbl_Structure].Supervisor FROM _tbl_Structure RIGHT JOIN tbl_Genesys_Daily ON [_tbl_Structure].[User ID] = tbl_Genesys_Daily.Field5 WHERE ((([_tbl_Structure].Supervisor)=?));")
.Parameters(0) = [Forms]![frm_Manager_Stats_NEW]![Text279]
Set lvxObj = AvailabilityCap.Object
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
End With
Error:
Too few parameters. Expected 1. (Runtime 3061)
Any help on understanding why this works for one but not another is appreciated

Form- and report-based parameters are only available in the GUI context (queries run using the GUI, forms, reports, macros and DoCmd.RunSQL). You're probably executing this through CurrentDb, and need to use a querydef instead.
With CurrentDb.CreateQueryDef("", "SELECT [_tbl_Structure].[User Name], tbl_Genesys_Daily.Field32, [_tbl_Structure].[Supervisor Emp Num], [_tbl_Structure].Supervisor FROM _tbl_Structure RIGHT JOIN tbl_Genesys_Daily ON [_tbl_Structure].[User ID] = tbl_Genesys_Daily.Field5 WHERE ((([_tbl_Structure].Supervisor)=?));")
.Parameters(0) = [Forms]![frm_Manager_Stats_NEW]![Text279]
Set rs = .OpenRecordset
End With
You can learn more about the different types of parameters, and when to use which one, in this answer

Related

Formatting a listbox alternative [duplicate]

I am using the below SQL, it works fine if I run it from query builder but once I have put it in VBA it throws out an error:
Code:
With CurrentDb.CreateQueryDef("", "SELECT [_tbl_Structure].[User Name], tbl_Genesys_Daily.Field32, [_tbl_Structure].[Supervisor Emp Num], [_tbl_Structure].Supervisor FROM _tbl_Structure RIGHT JOIN tbl_Genesys_Daily ON [_tbl_Structure].[User ID] = tbl_Genesys_Daily.Field5 WHERE ((([_tbl_Structure].Supervisor)=?));")
.Parameters(0) = [Forms]![frm_Manager_Stats_NEW]![Text279]
Set lvxObj = AvailabilityCap.Object
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
End With
Error:
Too few parameters. Expected 1. (Runtime 3061)
Any help on understanding why this works for one but not another is appreciated
Form- and report-based parameters are only available in the GUI context (queries run using the GUI, forms, reports, macros and DoCmd.RunSQL). You're probably executing this through CurrentDb, and need to use a querydef instead.
With CurrentDb.CreateQueryDef("", "SELECT [_tbl_Structure].[User Name], tbl_Genesys_Daily.Field32, [_tbl_Structure].[Supervisor Emp Num], [_tbl_Structure].Supervisor FROM _tbl_Structure RIGHT JOIN tbl_Genesys_Daily ON [_tbl_Structure].[User ID] = tbl_Genesys_Daily.Field5 WHERE ((([_tbl_Structure].Supervisor)=?));")
.Parameters(0) = [Forms]![frm_Manager_Stats_NEW]![Text279]
Set rs = .OpenRecordset
End With
You can learn more about the different types of parameters, and when to use which one, in this answer

Errors in UPDATE query - VB6 DAO Access

A simple UPDATE query with INNER JOIN is causing me problems, using VB6 and DAO on an Access .MDB database.
I thought it would be simple, but whatever changes I make generate errors.
My query is:
UPDATE work
INNER JOIN emp ON work.ref = emp.ref
SET work.code1 = emp.code1
This generates run-time error 3075 Syntax error in query expression 'work.ref = emp.ref'.
I get a similar error with:
UPDATE work w
INNER JOIN emp ON w.ref = emp.ref
SET w.code1 = emp.code1
and
UPDATE [work] w
INNER JOIN emp ON w.ref = emp.ref
SET w.code1 = emp.code1
and also if I use alias e for table emp.
I cannot use a FROM clause, which is not supported in Access (Thanks though #MarkKram)
I have to use DAO 3.51 (old!) in VB6, which cannot easily be changed.
I have tried square brackets around the first reference to work as in UPDATE [work] (as I need this in a simple SELECT * FROM [work]) as well as various combinations with and without square brackets around table names and column names. But still it fails.
Do you have any suggestions please?
Dim ws As DAO.Workspace
Dim DB As DAO.Database
Dim szSQL As String
Set ws = gWS
Set DB = gWS.OpenDatabase(WorkFile)
szSQL = "UPDATE work INNER JOIN emp ON work.ref = emp.ref SET work.code1 = emp.code1 WHERE work.trancode = 'P'"
DB.Execute szSQL
Set DB = Nothing
Set ws = Nothing
Try this:
UPDATE work, emp
SET work.code1 = emp.code1
WHERE work.ref = emp.ref AND work.transcode ='P'

How to substitute a variable into an OpenRecordSet query

I am almost there:
The variable strCons contains a number and I would like to add this to the SQL. When I do this like below, I get this run-time error:
3061 Too few parameters. Expected 1.
When I replace strCons with a number it works. Tried to find out why with no luck.
Set rs = CurrentDb.OpenRecordset("SELECT Member.HomeEmail FROM Member INNER JOIN MemberRole ON Member.MemberID = MemberRole.MemberID WHERE (((MemberRole.AreaID)= strCons) AND ((MemberRole.RoleID)=1) AND ((MemberRole.FinishDate) Is Null)) ORDER BY Member.Surname")
strEmailTD = rs.Fields(0).Value
rs.Close
You can use:
Set rs = CurrentDb.OpenRecordset("SELECT Member.HomeEmail FROM Member INNER JOIN MemberRole ON Member.MemberID = MemberRole.MemberID WHERE (((MemberRole.AreaID)=" & strCons & ") AND ((MemberRole.RoleID)=1) AND ((MemberRole.FinishDate) Is Null)) ORDER BY Member.Surname")

Access - Report to show subform results (Join table)

I'm a little bit lost with this one. I have a subform, which shows records based on my combobox selection. This is done like this (code is called from Combobox After_Update):
Sub ShowResults1()
With Forms![MYForm].RecordsetClone
.FindFirst "[ID] = " & Forms![MyForm]![CmbSearch]
If Not .NoMatch Then
If Forms![MyForm].Dirty Then Forms![MyForm].Dirty = False
Forms![MyForm].MySubfom.SourceObject = "MySubform"
Forms![MyForm].Bookmark = .Bookmark
End If
End With
End Sub
Main form is bound to a join table, this is It's recordsource (It is linked to 2 tables via ID's in It):
SELECT JoinTable.*, Table1.IDx AS IDx_Table1, Table1.Field1, Table1.Field2, Table1.Field3, Table2.IDy AS IDy_Table2, Table2.Field1, Table2.Field2, Table2.Field3
FROM Table2 INNER JOIN (Table1 INNER JOIN JoinTable ON Table1.IDx = JoinTable.IDx) ON Table2.IDy = JoinTable.IDy;
When upper code is executed, I get a results in subform that matches criteria, so this works fine.
Now, what I want is to have a report, that will show exactly what subform allready shows. Is this too complicated, or is there any simple solution ?
EDIT:
I have created a new query (named "SearchReport"):
SELECT Table2.IDy, Table2.Field1, Table2.Field3
FROM Table2 INNER JOIN (Table1 INNER JOIN Join_Table ON Table1.IDx = JoinTable.IDx) ON Table2.IDy = JoinTable.IDy
WHERE JoinTable.IDy=3;
This query successfully shows records that I want, but only with ID 3. Now I removed "WHERE JoinTable.IDy=3" from Query so that I can show records based on my combobox. This is how I tried opening report:
DoCmd.OpenReport "MyReport", acViewReport, "SearchReport", WhereCondition:="JoinTable.IDy =" & Forms![MyForm]![CmbSearch]
But when Report is opened, Access keeps asking about entering parameter value of IDy. Even If I enter 3, there are all records shown from that query. What Is wrong ??
Both forms and reports have a Record Source property.
In this Record Source property you can specify a table, a query, or a plain SQL statement which is your case.
In VBA, if your Subform is opened and displaying the right information, you can just say MyReport.RecordSource = MySubForm.RecordSource.
But the most proper way to achieve what you want is this :
In the code of your report, add this public sub :
Public Sub Open_This_Report(strSQL)
Me.RecordSource = strSQL
End Sub
In your form, add a button, with this ClickEvent :
Private Sub myButton_Click()
DoCmd.OpenReport "name_of_the_report", acViewNormal
Call Report_name_of_the_report.Open_This_Report(me!SubFormName.Form.RecordSource)
'or
Call Report_name_of_the_report.Open_This_Report("specific SQL query")
End Sub
Solved. I had to select records from Query that I posted in edit (full code for command button_click event - button on form):
Dim sql As String
sql = "SELECT Table2.IDy, Table2.Field1, Table2.Field3" & _
" FROM Table2 INNER JOIN (Table1 INNER JOIN Join_Table ON Table1.IDx =" & _
"JoinTable.IDx) ON Table2.IDy = JoinTable.IDy" & Forms![MyForm]![CmbSearch] DoCmd.OpenReport "APO_IIS", acViewReport
Reports![Myreport].RecordSource = sql

The recordsource [query here] specified on this form or report does not exist

I got error in populating the result of my query to access form but it gives me "The recordsource [query here] specified on this form or report does not exist
Here is my code:
Me.RecordSource = "SELECT Division, Station, EmployeeNumber, BranchCode, AccountNumber, LedgerSetNumber, EmployeeName, EffectDate, TermDate, Amount, BillingType, DateBilled, PolicyNumber, BillingStatus, UpdateStatus, DateUpdated, SequenceNumber FROM DepEdTempBilling WHERE (UpdateStatus<>'Billed') ORDER BY Division, Station, EmployeeNumber, BranchCode, AccountNumber, LedgerSetNumber, EmployeeName"
screenshot:
Your logic is correct. For some reason that doesn't work. I did the same thing with my code. Here it is, hopefully this helps.
Me.RecordSource = "SELECT tblQuoteMaster.[Quote Number], tblQuoteMaster.Status, tblQuoteMaster.Prefix, tblQuoteMaster.[Rev Number], tblQuoteMaster.[AWC Rep], tblQuoteMaster.Estimator, tblQuoteMaster.Customer, CUSLST.Name1 AS Company, tblQuoteMaster.CustomerID, tblQuoteMaster.[Approx Value], tblQuoteMaster.Currency, tblQuoteMaster.Decription, tblQuoteMaster.[Date Received], tblQuoteMaster.[Date Sent], tblQuoteMaster.[Follow Up Date], tblQuoteMaster.[Contact for Quote], tblQuoteMaster.ProductCode FROM (tblQuoteMaster INNER JOIN CUSLST ON tblQuoteMaster.CustomerID = CUSLST.[Account Code]) INNER JOIN tblContact ON CUSLST.[Account Code] = tblContact.[Customer Number] WHERE (" & strSQL & ");"
I have seen some people assign their recordset to the RecordSource. That would always give me your error. For example:
Dim rcd As RecordSet
Set rcd = db.OpenRecordset("SELECT.....")
Me.RecordSource = rcd.Name
Have re-made you table and form and tested, This error occurs when the Property Sheet (Access window not VBA editor) Form/Record Source: has “[query here]” in it clear this line out and try again, if that does not work try a clean new form with this statement. The command itself and SQL appear to be ok.