Search code examples
ms-accessvbaoffice365ms-access-2016

Why does DoCmd.SetParameter 'expression' parameter expects the name of a control, not a value?


The documentation for DoCmd.SetParameter says:

expression. SetParameter( _Name_, _Expression_ )
expression A variable that represents a DoCmd object.

Parameters
Name        Required/Optional   Data type   Description
Name        Required            Variant     The name of the parameter. The name must
                                            match the name of the parameter 
                                            expected by the BrowseTo, OpenForm, 
                                            OpenQuery, OpenReport, or RunDataMacro method.
Expression  Required            Variant     An expression that evaluates to a value to 
                                            assign to the parameter.

But when I try this:

DoCmd.SetParameter "sAction", "UPDATE"

I get the error message

Run-time error '2482':

Vendor Compliance System cannot find the name 'UPDATE' you entered in the expression.

So I created a form called "frmTEMP" with a textbox "sAction", and then tried this, which worked:

DoCmd.SetParameter "sAction", "forms!frmTEMP!sAction"

So my question is, Why does DoCmd.SetParameter 'expression' parameter expects the name of a control, not a value? Since I don't have an open form handy, how do I supply SetParameter with a value instead?


Solution

  • June7 provided an answer that solved my problem. Thanks!

    So, here's my code:

    Public Sub sendEvent_ValueChange(RecordID As Long, TableID As String,
    ValueID As String, newVal As Variant, oldVal As Variant)
         DoCmd.SetParameter "sTable", TableID
         DoCmd.SetParameter "sField", ValueID
         DoCmd.SetParameter "sAction", "UPDATE"
         DoCmd.SetParameter "recordID", RecordID
         DoCmd.SetParameter "value_Old", oldVal
         DoCmd.SetParameter "value_New", newVal
         DoCmd.RunDataMacro "ChangeLog.AddLog" 
    End Sub
    

    The idea of this sub is that when a user modifies an unbound control, the afterUpdate event will call it to send the table/source name, field name, old and new values to the "ChangeLog" table. "AddLog" is a named data macro on this table which checks the data types (boolean, long, string, etc.) and puts the data in a field of the right type.

    The problem is it is looking up the second parameter to the "SetParameter" command as the name of a control on an open form, instead of just a value. To interpret it as a value, it must be surrounded by quotes, e.g.

    DoCmd.SetParameter "sTable", "'" & TableID & "'"
    DoCmd.SetParameter "sAction", "'UPDATE'"
    

    etc.