• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

What is the syntax to reference Access report fields in VBA

I am trying to call an Access report field from a Macro but can't seem to get the syntax correct in my VBA script. I have enclosed a copy of my code. This may look a little muddled as I have tried several attempts to get it to work.

Hopefully from the code it should be quite obvious what I am trying to do, update the rag report output to appropriate colours so that I can then export the file. (which conditional formatting doesn't allow.

DoCmd.openreport "rpt90", acViewPreview
Dim rpt90 As Report_rpt90
If rpt90!RAG_Label = "A" Then
Rrpt90.RAG_Label.Properties("ForeColor") = vbYellow
ElseIf Reports!rpt90.TextBox("R/A/G") = "R" Then
Reports!rpt90.TextBox("R/A/G").ForeColor = vbRed
ElseIf UCase(Reports!rpt90.TextBox("R/A/G")) = "G" Then
Reports!rpt90.TextBox("R/A/G").TextBox.ForeColor = vbGreen
End If
End Sub

Open in new window

  • 2
  • 2
2 Solutions
Here are a few comments:

<If rpt90!RAG_Label = "A" Then>

If RAG_Label is a label (not a textbox) then you need to use the Caption property (otherwise the syntax is fine as-is):

If rpt90!RAG_Label.Caption = "A" Then

<Rrpt90.RAG_Label.Properties("ForeColor") = vbYellow>
Is simply:
Rrpt90.RAG_Label.Properties.ForeColor = vbYellow

<ElseIf Reports!rpt90.TextBox("R/A/G") = "R" Then>
is "R/A/G" the name of your textbox?  If so, consider renaming it using standard naming convetions which avoid spaces and special characters, for example txtRAG.

Otherwise, use Brackets and the following syntax:

ElseIf Reports!rpt90.[R/A/G] = "R" Then

The same thing applies to all your other references to R/A/G.
The Reddick VBA Naming conventions are described here:

Conventions specific to Access are midway down the page.

Most of the regulars here follow these conventions.
JayceWAuthor Commented:
Excellent thanks, Next step I will find out how to loop through the rows on my report to apply the formatting.
Jeffrey CoachmanMIS LiasonCommented:

I think mbizup has most of this covered, but, ...
What is this line supposed to ber doing?:
    Dim rpt90 As Report_rpt90

AFAICT (Unless you have some special Library loaded), your code will error out right away at this line, with a "User defined type, not defined" error.

Perhaps it should be:
    Dim rpt90 As Report


JayceWAuthor Commented:
Thanks Jeff, that line, as you suggest was not needed, so I have already removed it from the code

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now