Problem assigning value in Sub

C

charles.kendricks

I am trying to write write a procedure for a button which will open a
report using the value of a field in the open form as the Where
parameter for DoCmd.Openreport. The value of the field is assigned to
a variable defined as an Integer, and the field to which I am assigning

the variable is an Autonumber field. But when I press the button I get
a message box prompting me for the value of the variable. What am I
doing wrong??? Code included below:

Private Sub Command45_Click()
Dim OrderNum As Integer
OrderNum = Forms!frmClientData!frmOrderSubform!OrderNo.Value


'Debug.Print OrderNum
DoCmd.OpenReport "rptReceipt", acViewPreview, ,
"[OrderNo]=OrderNum"


On Error GoTo Err_Command45_Click


Exit_Command45_Click:
Exit Sub
 
R

Rick Brandt

I am trying to write write a procedure for a button which will open a
report using the value of a field in the open form as the Where
parameter for DoCmd.Openreport. The value of the field is assigned to
a variable defined as an Integer, and the field to which I am
assigning

the variable is an Autonumber field. But when I press the button I get
a message box prompting me for the value of the variable. What am I
doing wrong??? Code included below:

Private Sub Command45_Click()
Dim OrderNum As Integer
OrderNum = Forms!frmClientData!frmOrderSubform!OrderNo.Value


'Debug.Print OrderNum
DoCmd.OpenReport "rptReceipt", acViewPreview, ,
"[OrderNo]=OrderNum"


On Error GoTo Err_Command45_Click


Exit_Command45_Click:
Exit Sub

The variable name needs to be outside the quotes. Otherwise it is not
recognized as a variable name, but rather Access assumes it must be a field in
the Report's RecordSource. When that field name cannot be found it then treats
it as a parameter.

DoCmd.OpenReport "rptReceipt", acViewPreview, ,"[OrderNo]=" & OrderNum
 
R

Rick Brandt

I am trying to write write a procedure for a button which will open a
report using the value of a field in the open form as the Where
parameter for DoCmd.Openreport. The value of the field is assigned to
a variable defined as an Integer, and the field to which I am
assigning

the variable is an Autonumber field. But when I press the button I get
a message box prompting me for the value of the variable. What am I
doing wrong??? Code included below:

Private Sub Command45_Click()
Dim OrderNum As Integer
OrderNum = Forms!frmClientData!frmOrderSubform!OrderNo.Value


'Debug.Print OrderNum
DoCmd.OpenReport "rptReceipt", acViewPreview, ,
"[OrderNo]=OrderNum"


On Error GoTo Err_Command45_Click


Exit_Command45_Click:
Exit Sub

The variable name needs to be outside the quotes. Otherwise it is not
recognized as a variable name, but rather Access assumes it must be a field in
the Report's RecordSource. When that field name cannot be found it then treats
it as a parameter.

DoCmd.OpenReport "rptReceipt", acViewPreview, ,"[OrderNo]=" & OrderNum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top