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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enabling a button depending.. 2

Status
Not open for further replies.

homesick

Programmer
Dec 5, 2001
55
0
0
CA
I have a combobox with a list of codes(product codes) and I have a text box that represents the dollar amount....I want to enable the button for the codes that do not require a dollar amount and only enable the button if a dollar amount is entered for the codes that require a dollar amount....Can anyone suggest anything? This is what I have.....eg. one of my codes that require a dollar amount is "MTG"

Private Sub Combo1_Change()
Command1.Enabled = Combo1.Text <> &quot;&quot;
End Sub

 
If Combo1.Text = &quot;MTG&quot; Then
Command1.Enabled = True
Else
Command1.Enabled = False
End If

Give that a try. Should do it. Rob
Just my $.02.
 
Write a function that will determine if the value in the combo box is a value that requires a dollar amount entered. Use that function to determine wheather the textbox should be populated with a valid dollar amount. Almost the same technique I showed you in the other thread, just another function added:

--------------------

'Check Value against a list of values requiring a dollar amt
'Return True if dollar amt is required, false if not

Function DollarAmtRqd(ByVal Value As String) As Boolean
If Value = &quot;MTG&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;TPS&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;MFC&quot; Then
DollarAmtRqd = True
Exit Function

'Add the rest of the values that determine wheather a dollar amount is required
End If
End Function

'Verify the dollar amount is valid

Private Function IsValidDollarAmt(ByVal Amt As Variant) As Boolean
Dim TmpVal As Currency

On Error Resume Next
TmpVal = Amt

IsValidDollarAmt = Err.Number = 0
On Error GoTo 0

End Function

'txtDollarAmt is the textbox where the user enters the dollar amount

Private Sub Combo1_Change()
If DollarAmtRqd(Combo1.Text) = True Then
Command1.Enabled = IsValidDollarAmt(txtDollarAmt.Text)
End If
End Sub
------------------------

Now you just need to finish adding the rest of the values to the DollarAmtRqd() function. Does that work for you?

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Mike:

Nothing seems to happening..this is what i have:

&quot;comb1 is where the prod codes are and text8 is where the dollar amount should be entered
'check value against a list of values requiring a dollar amount
'return True if dollar amnt is required, false if not

Function DollarAmtRqd(ByVal Value As String) As Boolean
If Value = &quot;MTG&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;RGIC&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;NGIC&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;RMF&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;NMF&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;PAC&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;BT&quot; Then
DollarAmtRqd = True
Exit Function

End If


End Function
'verify dollar amount is valid
Private Function IsValidDollarAmt(ByVal Amt As Variant) As Boolean
Dim TmpVal As Currency

On Error Resume Next
TmpVal = Amt

IsValidDollarAmt = Err.Number = 0
On Error GoTo 0

End Function

Private Sub Combo1_Change()
If DollarAmtRqd(Combo1.Text) = True Then
Command1.Enabled = IsValidDollarAmt(Text8.Text)
End If
End Sub
 
Don't use the combos change event to check if a dollar amount is required. Use the click event.
 
' Save some keystrokes
Function DollarAmtRqd(ByVal Value As String) As Boolean
Select Case Value ' Consider UCase$(Value)
Case &quot;MTG&quot;, &quot;RGIC&quot;, &quot;NGIC&quot;, &quot;RMF&quot;, &quot;NMF&quot;, _
&quot;PAC&quot;, &quot;BT&quot;
DollarAmtRqd = True
End Select
End Function Compare Code (Text)
Generate Sort in VB or VBScript
 
MrEGuest is right, I moved the code to the click event and the code below seems to test ok.

----------------------------------

Option Explicit

Function DollarAmtRqd(ByVal Value As String) As Boolean
If Value = &quot;MTG&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;RGIC&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;NGIC&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;RMF&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;NMF&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;PAC&quot; Then
DollarAmtRqd = True
Exit Function

ElseIf Value = &quot;BT&quot; Then
DollarAmtRqd = True
Exit Function

End If

End Function
'verify dollar amount is valid
Private Function IsValidDollarAmt(ByVal Amt As Variant) As Boolean
Dim TmpVal As Currency

On Error Resume Next
TmpVal = Amt

IsValidDollarAmt = Err.Number = 0
On Error GoTo 0

End Function

Private Sub Combo1_Click()
If DollarAmtRqd(Combo1.Text) = True Then
Command1.Enabled = IsValidDollarAmt(Text8.Text)
Else
Command1.Enabled = False 'Added this line
End If

End Sub

Private Sub Form_Load()
'For testing only
With Combo1
.AddItem &quot;MTG&quot;
.AddItem &quot;RGIC&quot;
.AddItem &quot;NGIC&quot;
.AddItem &quot;RMF&quot;
.AddItem &quot;NMF&quot;
.AddItem &quot;PAC&quot;
.AddItem &quot;BT&quot;
.AddItem &quot;Different String&quot; 'Dollar amt not rq'd
.AddItem &quot;Another Different String&quot; 'with these
End With


End Sub

Private Sub Text8_Change()
If DollarAmtRqd(Combo1.Text) = True Then
Command1.Enabled = IsValidDollarAmt(Text8.Text)
End If

End Sub

-----------------------------

The command button needs to be updated in both the combo_click() and the Text8_Change() events for it to work right. Lemme know if this doesn't do it, we're getting close :)

~Mike

Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top