Rainbow drawn by Access on an Access report Ms Access Gurus

Wishing you beautiful rainbows! Help support this site, thank you.

Draw a Rainbow in Access

Liven up your Access reports with rainbows! VBA procedure that's easy to call from code behind your reports. Store parameters in a table to make things more flexible. If you can imagine it, Access can do it.

Send the report object, coordinate for the center and radius for the Rainbow. Optionally, you can choose background color, and start and end angles.

The code that does the drawing is all in one module that you can easily import into your projects. The Rainbow uses the Circle method.

Show a Rainbow on an Access report

Quick Jump

Goto Top  


Download

Download zipped BAS file you can import into your Access projects: bas_Draw_Rainbow_s4p.zip

Download zipped ACCDB file with sample data, a module, and 2 sample reports: Draw_Rainbow_s4p__ACCDB.zip

Remember to UNBLOCK files you download to remove the Mark of the Web. Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm

Goto Top  

Try it yourself

There are 2 sample reports you can play around with

Goto Top  

Video

watch on YouTube: Draw a Rainbow on Access Reports (10:14)

Goto Top  

VBA

Standard module

Specify location and size, and optionally background color and start/end angles.

'*************** Code Start ***************************************************
' Purpose  : draw a Rainbow on an Access report
'             specify report object,
'             coordinate of the middle of the Rainbow circle,
'                 and radius of the rainbow.
'             Optionally set background color,
'                 and start and end angles
'           USES the Circle method
' Author   : crystal (strive4peace)
' Code List: www.msaccessgurus.com/code.htm
' This code: https://msaccessgurus.com/VBA/Draw_Rainbow.htm
' Report Draw Reference: https://msaccessgurus.com/VBA/ReportDraw_Reference.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark your changes. Use at your own risk.
' bas_Draw_Rainbow_s4p
'------------------------------------------------------------------------------
'           Global variables
'------------------------------------------------------------------------------
'comment if defined elsewhere
Public Const PI As Double = 3.14159 
Public Const gZero As Single = 0.0000001 

Private ColorRainbow(1 To 9) As Long 

'------------------------------------------------------------------------------
'           Draw_Rainbow_s4p
' send report object, center coordinate of rainbow circle and radius
'------------------------------------------------------------------------------
Public Sub Draw_Rainbow_s4p(poReport As Report _ 
      ,pXCenter As Single _ 
      ,pYCenter As Single _ 
      ,psgRadius As Single _ 
   ,Optional pnColorBackground As Long = vbWhite _ 
   ,Optional psgAngle1 As Single = gZero _ 
   ,Optional psgAngle2 As Single = PI _ 
   ) 
'230116 s4p
'Draw a Rainbow
'   measurements in twips and radians

   'PARAMETERs
   '  poReport is the Report object
   '  pXCenter is x-coordinate of the middle of the Rainbow circle
   '  pYCenter is y-coordinate of the middle of the Rainbow circle
   '  psgRadius is Rainbow radius
   
   '(Optional)
   '  pnColorBackground, default is white
   '  psgAngle1 start angle, default is zero
   '  psgAngle2 end angle, default is PI
   
   On Error GoTo Proc_Err 
   
   'dimension variables
   Dim sgRadius As Single _ 
      ,i As Integer 

   If ColorRainbow(1) = 0 Then 
      Call setColorsRainbow 
   End If 
   
   'background color in the middle
   ColorRainbow(9) = pnColorBackground 
   
   If psgAngle1 = 0 Then 
      If psgAngle2 = 0 Then 
         Exit Sub 
      End If 
      'zero can't be negative -- use small number
      psgAngle1 = gZero 
   End If 
   
    With poReport 
      
      .ScaleMode = 1  'twips
      .DrawWidth = 1  'pixel

      .FillStyle = 0  'Opaque
      
      sgRadius = psgRadius 
      
      For i = 1 To 9 
         .FillColor = ColorRainbow(i) 
         'negative angles mean to close the shape
         'so it can be filled
         poReport.Circle (pXCenter,pYCenter),sgRadius _ 
            ,ColorRainbow(i),-psgAngle1,-psgAngle2 
         If i = 1 Or i = 8 Then 
            sgRadius = sgRadius - psgRadius / 30  'thin border
         Else 
            sgRadius = sgRadius - psgRadius / 15 
         End If 
      Next i 
      
   End With  'poReport
      
Proc_Exit: 
   On Error GoTo 0 
   Exit Sub 
  
Proc_Err: 
   MsgBox Err.Description _ 
       ,, "ERROR " & Err.Number _ 
        &  "   Draw_Rainbow_s4p"

   Resume Proc_Exit 
   Resume 
   
End Sub 
'------------------------------------------------------------------------------
'           setColorsRainbow
'------------------------------------------------------------------------------
Sub setColorsRainbow() 
   ColorRainbow(1) = RGB(208,57,46)      'dark Red
   ColorRainbow(2) = RGB(244,67,54)      'Red
   ColorRainbow(3) = RGB(255,152,0)      'Orange
   ColorRainbow(4) = RGB(255,235,59)     'Yellow
   ColorRainbow(5) = RGB(139,195,74)     'Green
   ColorRainbow(6) = RGB(33,150,243)     'Blue
   ColorRainbow(7) = RGB(153,0,255)      'Violet
   ColorRainbow(8) = RGB(99,50,159)      'dark Violet
End Sub 

'*************** Code End *****************************************************

Goto Top  

Access report with a Rainbow drawn with specified colors

rpt_Rainbow_Detail

Code behind report to draw portions of a Rainbow based on status.

'*************** Code Start CBR ***********************************************
' Purpose  : code behind rpt_Rainbow_Detail
'              calls Draw_Rainbow_s4p
'              draw portions of a rainbow
'                 fractions of PI defined in a table for stand and end angles
' Author   : crystal (strive4peace)
' Code List: www.msaccessgurus.com/code.htm
' This code: https://msaccessgurus.com/VBA/Draw_Rainbow.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark your changes. Use at your own risk.
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
'           Detail_Format
'------------------------------------------------------------------------------
Private Sub Detail_Format(Cancel As Integer,FormatCount As Integer) 
'230116 crystal
'draw portions of rainbows in the detail section of a report
   'CALLs
   '  Draw_Rainbow_s4p
      
   Dim X As Single,Y As Single _ 
      ,dx As Single,dy As Single _ 
      ,sgRadius As Single _ 
      ,sgAngle1 As Single _ 
      ,sgAngle2 As Single 
      
   With Me 
      
      .ScaleMode = 1  'twips
      
      'width and height for drawing
      dx = 2 * 1440  '1440 twips/inch
      dy = .ScaleHeight 
      
      sgRadius = 1440  '1 inch
      
      'center of rainbow circle
      X = .ScaleLeft + dx / 2 
      Y = .ScaleTop + sgRadius 
   
      sgAngle1 = .StartPI * PI 
      sgAngle2 = .EndPI * PI 
   End With 
   
   'Call Draw_Rainbow_s4p
   Call Draw_Rainbow_s4p(Me,X,Y,sgRadius,,sgAngle1,sgAngle2) 
   
End Sub 
'*************** Code End *****************************************************

Goto Top  

Access report with a Rainbow on the page

rpt_Rainbow_Page

Code behind report to draw a rainbow at the top of a page.

'*************** Code Start CBR ***********************************************
' Purpose  : code behind rpt_rainbows_Page
'            calls Draw_Rainbow_s4p
'              draw a rainbow on a page
' Author   : crystal (strive4peace)
' Code List: www.msaccessgurus.com/code.htm
' This code: https://msaccessgurus.com/VBA/Draw_Rainbows.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark your changes. Use at your own risk.
'------------------------------------------------------------------------------
'           Report_Page
'------------------------------------------------------------------------------
Private Sub Report_Page() 
'230115 crystal
'draw a rainbow at the top of the page
   'CALLs
   '  Draw_Rainbow_s4p
      
   Dim X As Single,Y As Single _ 
      ,dx As Single,dy As Single _ 
      ,sgRadius As Single 
      
   With Me 
      
      .ScaleMode = 1  'twips
      
      'width and height for drawing
      dx = .ScaleWidth 
      dy = .ScaleHeight _ 
         - .PageFooterSection.Height _ 
         - .PageHeaderSection.Height 
      
      If dx > dy Then 
         sgRadius = dy / 2 
      Else 
         sgRadius = dx / 2 
      End If 
      
      'center of rainbow
      X = .ScaleLeft + dx / 2 
      Y = .ScaleTop + .PageHeaderSection.Height _ 
         + sgRadius 
   
   End With 
   
   'Call Draw_Rainbow_s4p
   Call Draw_Rainbow_s4p(Me,X,Y,sgRadius) 
      
End Sub 
'*************** Code End *****************************************************
' Code was generated with colors using the free Color Code add-in for Access.

Goto Top  

Reference

Drawing Reference on MsAccessGurus

Report Draw Reference for VBA syntax and help for drawing on Access reports.

Circle method

DrawWidth property

FillColor property

FillStyle property

ScaleMode property

ScaleWidth property

ScaleHeight property

ScaleLeft property

ScaleTop property

Goto Top  

Backstory

This is for all the dreamers that love rainbows and beautiful things.

VBA drawing code with links to more pages with code to draw on your Access reports. If you can imagine it, Access can do it!

If you like this page, please let me know. Donations are needed and much appreciated. Even a little means a lot, thank you

Share with others

Here's the link for this page in case you want to copy it and share it with someone:

https://msaccessgurus.com/VBA/Draw_Rainbow.htm

or in old browsers:
http://www.msaccessgurus.com/VBA/Draw_Rainbow.htm

Get Help with Access and drawing

Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.

While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. I'll give you lots of links to good resources.

Do you want your reports to be more creative and visual? I'd love to help you. Email me at training@msAccessGurus.com

~ crystal

the simplest way is best, but usually the hardest to see

Goto Top