Question : Error while trying to update a record on a form.

Hello Experts,

I have a access 2003 database form and am trying to code a button to update a single record based on the recid that is displayed on the form. I have added the code below. When I try and run this code it gives me an error code. The error code is :

Run-time error '3061':
Too few parameters. Expected 1.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("SELECT tbl_credits.recid, tbl_credits.entryby, tbl_credits.entrydt, tbl_credits.credit_type, tbl_credits.mdn, tbl_credits.account_number, tbl_credits.billing_system, tbl_credits.home_sid, tbl_credits.fraud_sids, tbl_credits.mtas_status, tbl_credits.h_to_r_auth, tbl_credits.calls_to, tbl_credits.detect_type, tbl_credits.detection_date, tbl_credits.worked_date, tbl_credits.esn_chg_date, tbl_credits.start_of_calls, tbl_credits.end_of_calls, tbl_credits.cycle_time, tbl_credits.action_taken, tbl_credits.action_taken_date, tbl_credits.credit_category, tbl_credits.credit_amount, tbl_credits.est_credit_amount, tbl_credits.credit_issued, tbl_credits.comments, tbl_credits.lead_approved_by, tbl_credits.lead_approved_date, tbl_credits.credit_issued_by, tbl_credits.credit_issued_date, tbl_credits.ad_approved_by, tbl_credits.ad_approved_date, tbl_credits.newrecord FROM tbl_credits WHERE (((tbl_credits.recid)=[forms]![f_credits]![txtrecid]));")
If Me.cbocreditcategory.Value = "$0 - $499" Then
    RS.Edit
    RS!lead_approved_by = fOSUserName()
    RS!lead_approved_date = Now()
    RS!newrecord = False
    RS.Update
    ' msgbox credit complete
Else
    RS.Edit
    RS!lead_approved_by = fOSUserName()
    RS!lead_approved_date = Now()
    RS.Update
    'Call email function to sups
End If

Answer : Error while trying to update a record on a form.

Courtesy of EE's mbizup:

"References to form controls work fine in stored queries.  If you take that same query and open it through VBA, the same form reference that worked before will cause an error -- unless you evaluate that reference seperately.  This holds true for OpenRecordset statements, and also CurrentDB.Execute and Docmd.RunSQL."


ok ... here is the example.

Setup:
A form with RecordSource of tblEmp  (employees table - two fields EmpID, EmpName)
Textbox on the form bound to EmpID
A saved Query Def (qryParametersTest) in the database window with this SQL:

SELECT tblEmp.EmpID, tblEmp.EmpName
FROM tblEmp
WHERE (((tblEmp.EmpID)=[Forms]![frmTest].[txtEmpID]));

This function:

Public Function mQyrParmTest() As Boolean
   
    Dim rst As DAO.Recordset, qdf As DAO.QueryDef
    Set qdf = CurrentDb.QueryDefs("qryParametersTest")
    With qdf
        .Parameters("[Forms]![frmTest].[txtEmpID]") = ([Forms]![frmTest].[txtEmpID])
        ' or Parameters(0) = ([Forms]![frmTest].[txtEmpID])
        Set rst = .OpenRecordset(dbOpenDynaset)
        MsgBox rst![EmpName]    'this will display the Employee name related to the ID currently on the form
    End With
    Set rst = Nothing
    Set qdf = Nothing
End Function

------
Random Solutions  
 
programming4us programming4us