Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

turn code on form into function 2

Status
Not open for further replies.

kkson

MIS
Dec 28, 2003
67
US
Hi,

I have several forms that have the same option group on them that do the same thing. I want to create a public function so I would not have to have all the code listed below on all the forms. There are 5 options and if you click one the others would go back to there default settings. The code would have to do this when the buttons are clicked. Also there is other code that can change the options without having the option clicked. ie if a date is older than the current date then change the option to nmcm and then perform all the actions that would happen if you had clicked the option.

thanks for the help.

If Me.STATUS = "FMC" Then
Me.optionStatus.value = 1
Me.optionStatus.BackColor = rgb(0, 250, 12)
Me.togFMC.ForeColor = 16711680
Me.togPMCM.ForeColor = rgb(255, 255, 0)
Me.togPMCS.ForeColor = rgb(255, 255, 0)
Me.togNMCM.ForeColor = rgb(255, 0, 0)
Me.togNMCS.ForeColor = rgb(255, 0, 0)
ElseIf Me.STATUS = "NMCM" Then
Me.optionStatus.value = 4
Me.optionStatus.BackColor = rgb(255, 0, 0)
Me.togFMC.ForeColor = rgb(0, 153, 0)
Me.togPMCM.ForeColor = rgb(255, 255, 0)
Me.togPMCS.ForeColor = rgb(255, 255, 0)
Me.togNMCM.ForeColor = 16711680
Me.togNMCS.ForeColor = rgb(255, 0, 0)
ElseIf Me.STATUS = "NMCS" Then
Me.optionStatus.value = 5
Me.optionStatus.BackColor = rgb(255, 0, 0)
Me.togFMC.ForeColor = rgb(0, 153, 0)
Me.togPMCM.ForeColor = rgb(255, 255, 0)
Me.togPMCS.ForeColor = rgb(255, 255, 0)
Me.togNMCM.ForeColor = rgb(255, 0, 0)
Me.togNMCS.ForeColor = 16711680
ElseIf Me.STATUS = "PMCM" Then
Me.optionStatus.value = 2
Me.optionStatus.BackColor = rgb(255, 255, 0)
Me.togFMC.ForeColor = rgb(0, 153, 0)
Me.togPMCM.ForeColor = 16711680
Me.togPMCS.ForeColor = rgb(255, 255, 0)
Me.togNMCM.ForeColor = rgb(255, 0, 0)
Me.togNMCS.ForeColor = rgb(255, 0, 0)
ElseIf Me.STATUS = "PMCS" Then
Me.optionStatus.value = 3
Me.optionStatus.BackColor = rgb(255, 255, 0)
Me.togFMC.ForeColor = rgb(0, 153, 0)
Me.togPMCM.ForeColor = rgb(255, 255, 0)
Me.togPMCS.ForeColor = 16711680
Me.togNMCM.ForeColor = rgb(255, 0, 0)
Me.togNMCS.ForeColor = rgb(255, 0, 0)
ElseIf Me.STATUS = "n/a" Or IsNull(Me.STATUS) Then
Me.optionStatus.value = 0
Me.optionStatus.BackColor = rgb(0, 0, 0)
Me.togFMC.ForeColor = rgb(0, 153, 0)
Me.togPMCM.ForeColor = rgb(255, 255, 0)
Me.togPMCS.ForeColor = rgb(255, 255, 0)
Me.togNMCM.ForeColor = rgb(255, 0, 0)
Me.togNMCS.ForeColor = rgb(255, 0, 0)
End If
 
Code:
  Private someEventProcedure_Event()
    formatOptionGroup(Me.status,me.OptionGroupName)    
  end sub


Code:
public sub FormatOptionGroup(status as variant, OptGroup as access.optiongroup)

with optGroup
  ' set defaults
  .value = 0
  .BackColor = rgb(255, 255, 0)
  .controls("togFMC").ForeColor = rgb(0, 153, 0)
  .controls("togPMCM").ForeColor = rgb(255, 255, 0)
  .controls("togPMCS").ForeColor = rgb(255, 255, 0)
  .controls("togNMCM").ForeColor = rgb(255, 0, 0)
  .contrls("togNMCS").ForeColor = rgb(255, 0, 0)
end with

if isNull(status) then status = "n/a"

select case Status
case "FMC"
  with optGroup
     .value = 1
     .BackColor = rgb(0, 250, 12)
     .controls("togFMC").ForeColor = 16711680
   end with
Case "NMCM" Then
  with optGroup
    .value = 4
    .BackColor = rgb(255, 0, 0)
    .controls("togNMCM").ForeColor = 16711680
  end with  
case "NMCS" 
   with optGroup
      .value = 5
      .BackColor = rgb(255, 0, 0)
      .controls("togNMCS").ForeColor = 16711680
   end with
case  "PMCM" 
   wit optGroup
     .value = 2
     .controls("togPMCM").ForeColor = 16711680
   end with
case "PMCS"
   with optGroup
     .value = 3
     .controls("togPMCS").ForeColor = 16711680
   end with
case  "n/a" 
  optgroup.BackColor = rgb(0, 0, 0)
end select

end sub
 
Private someEventProcedure_Event()
formatOptionGroup(Me.status,me.OptionGroupName)
end sub

i am getting a compile error expecting =. formatOptionGroup(me.status,me.optiongroupname) am i replacing the me.status to the status i want "nmcm" and the me.optiongroupname with the name of the optiongroup "optionstatus"

thanks,
 

I would assume MajP wants you to place the code for
[tt]Public Sub FormatOptionGroup[/tt]
in a standatd Module to be available from any Form in your application.

Where did you place this code?

Have fun.

---- Andy
 
the formatOptionGroup i put in a separate module. Would it need to be a function then? when i typed the formatOptionGroup and hit space it pulled up the help which shows what options should be placed in there. It does it like other functions i have made and used.
 
yes the module is named something else. in the form when i use the formatoptiongroup( do i put the actual status i want "nmcm" or does it reference the text box on the form which contains the status. the text box is called status. Then is the optgroup the name of option group? which in this case is optionstatus. That is the name on the optiongroup, i deleted the label to the option group that gets created. Did I need that?

Thanks for the help!!
 
It would be really nice if you copied and pasted your actual code. I doubt this is real:
Code:
Private someEventProcedure_Event()
formatOptionGroup(Me.status,me.OptionGroupName) 
end sub

I would call this without any ()s. Make sure this has your event procedure and control names.
Code:
Private someEventProcedure_Event()
    formatOptionGroup Me.status, me.OptionGroupName
End Sub



Duane
Hook'D on Access
MS Access MVP
 
that was it dhookom!! once i removed the () it worked!!!

Thanks everyone for the help [2thumbsup]
 
I thought this would work on the gotfocus of the options in the group. it will not let me change the status by clicking on the status. here is the code i had on a gotfocus on one of the options:

Private Sub togNMCM_GotFocus()
On Error GoTo Err_togNMCM_GotFocus

Me.chkUpdate = True
Me.STATUS = "NMCM"
Me.optionStatus.BackColor = rgb(255, 0, 0)
Me.togFMC.ForeColor = rgb(0, 153, 0)
Me.togPMCM.ForeColor = rgb(255, 255, 0)
Me.togPMCS.ForeColor = rgb(255, 255, 0)
Me.togNMCM.ForeColor = 16711680
Me.togNMCS.ForeColor = rgb(255, 0, 0)
If Me.chkPause = True Then
Me.chkPause = False
End If

Exit_togNMCM_GotFocus:
Exit Sub

Err_togNMCM_GotFocus:
MsgBox Err.Description
Resume Exit_togNMCM_GotFocus

End Sub

I changed it to:

Me.chkUpdate = True
Me.STATUS = "NMCM"
FormatOptionGroup Me.STATUS, Me.optionStatus
If Me.chkPause = True Then
Me.chkPause = False
End If

on all the buttons in the option group but when i try to click on another button nothing happens.

Thanks
 
I can get the optiongroup status to change to nmcm with the code that checks a date box. But if I then try to click on a different status button it defaults to the FMC status. Do i have to run code to reset something. with the old code i could click right down the list and change the status. But trying to save some coding. Should i have an on current event for the form and then make the change then?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top