Set command button backcolor from memory Variable

jamesgarry used Ask the Experts™
I am attempting to set command buttons on a form from memory Variables.  I set up the color I use in a module in the program  

public sub set colors()  
   vb_ltGreen = &HC0FFC0
   vb_Green = &HFF00&
   vb_dkGreen = &H8000&

   vb_ltRed = &HC0C0FF
   vb_Red = &HFF&
   vb_dkRed = &H80&

   vb_ltYellow = &HC0FFFF
   vb_yellow = &HFFFF&
   vb_dkYellow = &H8080&

etc ... for all the colors I want to use.  

This works fine if I set an objects color by using  

Private Sub LoadColorButtons()
               cmd_color(0).BackColor = vb_Green  
               cmd_color(1).backcolor = vb_Red
 End Sub
doing it this way works but I want to incorporate it into an array because the number of commandbuttons can change

what I have attempted to do is use my arrays

I load the color array from an ini file
 For i = 0 To g_ColorCount
        i_field = i
        str_field = "bc_color" & i_field
        str_FieldName = VBGetPrivateProfileString("bc_colorAssignment", str_field, FileName$)
        g_cbo_Color(i) = str_FieldName
    Next i
this makes a nice array of the colors I want.  

Private Sub LoadColorButtons()
       Dim i As Integer
       For i = 0 To g_ColorCount  ' number of color buttons
             cmd_color(i).BackColor =  g_cbo_Color(i)
       Next i
 End Sub

Using this code I get a message Run-time error'13'; type mismatch

the string at this time looks like
 cmd_color(i).BackColor =  g_cbo_Color(i)
using the mouse to view the inforamtion   the g_cbo_color(i) is   "vb_green"  with the quotes
Is there away to get g_cbo_color(i) to become vb_green and not "vb_green" ?
I have attempted to in close it in brackets but that didn't work.  

Any suggestions?  
thanks for your assistance.


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

First, What type of array is g_cbo_Color() defined as? My guess is a string array, but BackColor wants a value not a string either use a long array or convert the string to a long.

cmd_color(i).BackColor =  Clng(g_cbo_Color(i))
Top Expert 2010
GetPrivateProfileInt() returns an integer value so you can probrably use this instead of VBGetPrivateProfileString(). Otherwise you will need to do the conversions like I mentioned in the previous post.

This assumes you have a file setup like this, I named it (setcolors.dat)


' Add the code a form.
Option Explicit
Private Const ERROR_NOT_FOUND As Long = (-1)
Private Declare Function GetPrivateProfileIntW Lib "kernel32" ( _
  ByVal lpAppName As Long, _
  ByVal lpKeyName As Long, _
  ByVal nDefualt As Long, _
  ByVal lpFileName As Long) As Long
Dim g_cbo_Color() As Long
Public Sub FileToArrayColors( _
    ByVal szApp As String, _
    ByVal szKey As String, _
    ByVal szFile As String, _
    ByVal dwCount As Long)
  Dim i As Long
  Dim dwColor As Long
  ReDim g_cbo_Color(dwCount) As Long
  For i = 0 To dwCount
  dwColor = GetPrivateProfileIntW(StrPtr(szApp), StrPtr(szKey & i), ERROR_NOT_FOUND, StrPtr(szFile))
    If dwColor <> ERROR_NOT_FOUND Then
      g_cbo_Color(i) = dwColor
    End If
  Next i
End Sub
Private Sub Command1_Click()
  Command1.BackColor = g_cbo_Color(3)
End Sub
Private Sub Form_Load()
  Call FileToArrayColors("bc_colorAssignment", "bc_color", "d:\setcolors.dat", 8) ' 8 or (g_ColorCount)
End Sub

Open in new window


Thank you,  I used a hybrid verson,  I changed my color assignment to numbers instead of words.  I also put a color picker with sliders on in the form so the user can select the desired color.   storing the results from the slider into the array in a long format.  Thank you for your assistance.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial