Problem Passing Parameter With Command Button

S

silva

I have implemented a command button in a continuous form that'll open a
detailed report based on the small amount of overview data on the form. The
problem I'm having is properly limiting the report to the desired, relevant
data. If I use text fields in the conditional portion of the command, it
works, but it displays too many records. As it is the text fields alone do
not have any data that'll single out one record. I tried to use a field with
a date format or an autonumber field named eventID, but all I get when I use
those is an error message that says "Data type mismatch in query expression".
Is there anything I need to do with the query powering the report, or does
the conditional statements in the command button only work with text fields?
I've tried creating a variable as a LONG and storing the autonumber in it and
using it, but that seems to be an incorrect approach.

Here's a code snippet if it'll help identify what I'm doing wrong:

Dim transID As Long
transID = [contact_autonumber]

stDocName = "generic_report"
DoCmd.OpenReport stDocName, acPreview, , ("contact_autonumber = '" &
transID & "'")
 
K

Klatuu

The syntax of your statement is correct provided contact_autonumber is a text
field; however, based on the naming of the field, I think it is probably a
long interger or autonumber field. In addition, contact_autonumber should be
the name of the table field and transID should be the name of the control on
your form and should be qualified with a reference to the form. Assuming
that to be true, you code should be:

DoCmd.OpenReport stDocName, acPreview, , ("contact_autonumber = " &
Me.transID & "")
 
F

fredg

I have implemented a command button in a continuous form that'll open a
detailed report based on the small amount of overview data on the form. The
problem I'm having is properly limiting the report to the desired, relevant
data. If I use text fields in the conditional portion of the command, it
works, but it displays too many records. As it is the text fields alone do
not have any data that'll single out one record. I tried to use a field with
a date format or an autonumber field named eventID, but all I get when I use
those is an error message that says "Data type mismatch in query expression".
Is there anything I need to do with the query powering the report, or does
the conditional statements in the command button only work with text fields?
I've tried creating a variable as a LONG and storing the autonumber in it and
using it, but that seems to be an incorrect approach.

Here's a code snippet if it'll help identify what I'm doing wrong:

Dim transID As Long
transID = [contact_autonumber]

stDocName = "generic_report"
DoCmd.OpenReport stDocName, acPreview, , ("contact_autonumber = '" &
transID & "'")

The correct syntax depends upon the datatype of the criteria field.
Your syntax should work if the [contact_autonumber] field is a text
datatype.
I suspect it's not Text but a Number datatype, so:

DoCmd.OpenReport stDocName, acPreview, , ("[contact_autonumber] = " &
[transID] )

By the way, an autonumber field is a Number datatype, Long field size.
 
S

silva

Thanks guys! The final working command that I ended up using was:

DoCmd.OpenReport stDocName, acPreview, , ("[contact_autonumber] = " &
transID & "")

I guess I didn't realize that I was passing the arguments along as text
strings.That was a whoops. But now it's working perfectly!
 

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