VBA code and command buttons

Aleashia

Registered User.
Local time
Today, 10:46
Joined
Jan 4, 2007
Messages
15
Hey everybody,

I have the working code (well it seems to work, debugged fine etc). However, I have to make a command button on a form with plant details (which I did) and in the event properties I selected 'on click' and went to the code builder and put in the code.

However, when I click the button nothing executes i.e. a message is meant to appear depending on the code I have made:

Function stradvice(strcolour) As String
If IsNull(strcolour) Then
stradvice = ""
Exit Function

End If
Select Case strcolour
Case "white"
stradvice = "Goes well with red"
Case "brown"
stradvice = "Goes well with purple"
Case "green"
stradvice = "Best not to mix colours"
Case "orange"
stradvice = "Put in a dark pot"
Case "red"
stradvice = "Looks better in a dark room"
Case Else 'none of the above
stradvice = "Mix with other species for an exotic look"
End Select
End Function

I am just wondering if anybody knows how to successfully implement this so when the command button is clicked it brings up the correct message.
 
when the button is clicked you need to call the function, passing it the colour, then do something with the reply. Assuming your button is called btnAdvice and you have a field called txtColour that has the colour in it then try this in the button click event
Code:
Private Sub btnAdvice_Click()
If Nz(Me.txtColour, "") = "" Then Exit Sub ' empty box!
MsgBox stradvice(Me.txtColour), vbInformation, "Advice"
End Sub

HTH

Peter
 

Users who are viewing this thread

Back
Top Bottom