vba code to print a report using a current access variable.

  • Thread starter Thread starter capinvest
  • Start date Start date
C

capinvest

I have a database of clients set up. Each client has a
unique id number. I am trying to print a report using
only the current clients information, which is on the form
that I am placing the button. This is what I have so far:

Private Sub Print_SituationF_Button_Click()
On Error GoTo Err_Print_SituationF_Button_Click

Dim stDocName As String
Dim stWhere As String

stDocName = "Situation"
stWhere = "id=" & Me![*ID]
DoCmd.OpenReport stDocName, acPreview, , stWhere

Exit_Print_SituationF_Button_Click:
Exit Sub

I think that I use the openreport where condition, but I
am not sure how to retrieve the correct client ID.

If anyone can help, I would be very grateful.

Thanks
 
Why the asterisk in [*ID]?

If the Field name is [ID] and it is a number datatype,
and the name of the control on the form is [ID] then:

stWhere = "id=" & Me![ID]

Change [ID] to what ever the actual name is.
Note: You have 5 lines of code written where only one
is needed to do the job:

DoCmd.OpenReport "Situation", acViewPreview, , "id=" & Me![ID]
 
Thanks for your help, I am a lot further than I was before.
But I still get an error. It says that I have an extra )
in the query expression '([main].[ID] = )' but the () are
not there. Is it becuase this is a subform? "Situation"
is a subform of "Main". ID is the relationship between
them.
This will be a big weight off my shoulders if you can help
me. Thanks again.
-----Original Message-----
You evidently are using a query as a record source, and have the same field
name used in several tables.

Try:
stWhere = "[TableName].[ID] = " & Me![ID]

--
Fred

Please reply only to this newsgroup.
I do not reply to personal e-mail.


capinvest said:
I attemped to do what the last msg said and I still get an
error. The error is as follows:

The specified field "ID" could refer to more than one
table listed in the from clause of your SQL statement.

It is a number datatype.

Any further help would be great.

Thanks.
-----Original Message-----
Why the asterisk in [*ID]?

If the Field name is [ID] and it is a number datatype,
and the name of the control on the form is [ID] then:

stWhere = "id=" & Me![ID]

Change [ID] to what ever the actual name is.
Note: You have 5 lines of code written where only one
is needed to do the job:

DoCmd.OpenReport "Situation", acViewPreview, , "id=" &
Me!
[ID]
--
Fred

Please reply only to this newsgroup.
I do not reply to personal e-mail.


I have a database of clients set up. Each client has a
unique id number. I am trying to print a report using
only the current clients information, which is on the form
that I am placing the button. This is what I have so far:

Private Sub Print_SituationF_Button_Click()
On Error GoTo Err_Print_SituationF_Button_Click

Dim stDocName As String
Dim stWhere As String

stDocName = "Situation"
stWhere = "id=" & Me![*ID]
DoCmd.OpenReport stDocName, acPreview, , stWhere

Exit_Print_SituationF_Button_Click:
Exit Sub

I think that I use the openreport where condition, but I
am not sure how to retrieve the correct client ID.

If anyone can help, I would be very grateful.

Thanks



.


.
 
Back
Top