vbaprogammer
Programmer
I am creating a project with many, many panels using multipage. I have lots and lots of controls -- text, combo, etc.
I have written the following code to change the background color on entry and exit.
I have also written the following code to format the text boxes for date and amount BeforeUpdate.
Is this a good way to code these modules? I believe I have managed to cut down on some repetition, but can I do better, or do I have to put similar code in EVERY enter, exit event?
I am coding in Word 97 and Word 2000, so I cannot use WithEvents (darn). (All of these controls are inside frames ['fra...'].)
'SET THE BACK COLOR OF CONTROL
Private Sub txtLDAmt_Enter()
SetBackColor Me.fraLDInfo.ActiveControl, 1, PrevCtrl
End Sub
Private Sub txtLDAmt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
SetBackColor Me.fraLDInfo.ActiveControl, 0, PrevCtrl
End Sub
Public Sub SetBackColor(Ctr As Control, State As Integer, PrevControl As String)
If Ctr Is Nothing Then
Else
If TypeOf Ctr Is TextBox Or TypeOf Ctr Is ListBox Or TypeOf Ctr Is ComboBox Then
If State = 1 Then
Ctr.BackColor = &HC0FFFF ' Yellow
Else
Ctr.BackColor = &HFFFFFF ' White
End If
Else
Ctr.BackColor = &HC0FFFF ' Yellow
Ctr = PrevControl
Ctr.BackColor = &HFFFFFF 'White
End If
End If
End Sub
'SET FORMAT OF CONTROL
Private Sub txtLDAmt_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Me.txtLDAmt.Text = "" Then
Else
PrevCtrl = Me.fraLDInfo.ActiveControl.Name
InputFormat Me(PrevCtrl).Name, "txtNumber", False, 2
Cancel = vReturn
If vReturn = True Then
Me(PrevCtrl).SetFocus
End If
End If
End Sub
Function InputFormat(Ctrl, InputType, myReturn, NumDecimals)
Select Case InputType
Case "txtDate" '
If IsDate(Me(Ctrl).Text) Then
Me(Ctrl).Text = Format(Me(Ctrl).Text, "mmmm d, yyyy"
Else
MsgBox "Date values only in this field!", vbOKOnly + vbCritical, "Date Field"
myReturn = True
End If
Case "txtNumber"
If IsNumeric(Me(Ctrl).Text) Then
If NumDecimals = 0 Then
Me(Ctrl).Value = Format(Me(Ctrl).Text, "##0"
ElseIf NumDecimals = 2 Then
Me(Ctrl).Value = Format(Me(Ctrl).Text, "##,##0.00"
End If
Else
MsgBox "Numbers only in this field!", vbOKOnly + vbCritical, "Numeric Field"
myReturn = True
End If
Case Else
End Select
vReturn = myReturn
End Function
============
Thanks,
Dan
I have written the following code to change the background color on entry and exit.
I have also written the following code to format the text boxes for date and amount BeforeUpdate.
Is this a good way to code these modules? I believe I have managed to cut down on some repetition, but can I do better, or do I have to put similar code in EVERY enter, exit event?
I am coding in Word 97 and Word 2000, so I cannot use WithEvents (darn). (All of these controls are inside frames ['fra...'].)
'SET THE BACK COLOR OF CONTROL
Private Sub txtLDAmt_Enter()
SetBackColor Me.fraLDInfo.ActiveControl, 1, PrevCtrl
End Sub
Private Sub txtLDAmt_Exit(ByVal Cancel As MSForms.ReturnBoolean)
SetBackColor Me.fraLDInfo.ActiveControl, 0, PrevCtrl
End Sub
Public Sub SetBackColor(Ctr As Control, State As Integer, PrevControl As String)
If Ctr Is Nothing Then
Else
If TypeOf Ctr Is TextBox Or TypeOf Ctr Is ListBox Or TypeOf Ctr Is ComboBox Then
If State = 1 Then
Ctr.BackColor = &HC0FFFF ' Yellow
Else
Ctr.BackColor = &HFFFFFF ' White
End If
Else
Ctr.BackColor = &HC0FFFF ' Yellow
Ctr = PrevControl
Ctr.BackColor = &HFFFFFF 'White
End If
End If
End Sub
'SET FORMAT OF CONTROL
Private Sub txtLDAmt_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Me.txtLDAmt.Text = "" Then
Else
PrevCtrl = Me.fraLDInfo.ActiveControl.Name
InputFormat Me(PrevCtrl).Name, "txtNumber", False, 2
Cancel = vReturn
If vReturn = True Then
Me(PrevCtrl).SetFocus
End If
End If
End Sub
Function InputFormat(Ctrl, InputType, myReturn, NumDecimals)
Select Case InputType
Case "txtDate" '
If IsDate(Me(Ctrl).Text) Then
Me(Ctrl).Text = Format(Me(Ctrl).Text, "mmmm d, yyyy"
Else
MsgBox "Date values only in this field!", vbOKOnly + vbCritical, "Date Field"
myReturn = True
End If
Case "txtNumber"
If IsNumeric(Me(Ctrl).Text) Then
If NumDecimals = 0 Then
Me(Ctrl).Value = Format(Me(Ctrl).Text, "##0"
ElseIf NumDecimals = 2 Then
Me(Ctrl).Value = Format(Me(Ctrl).Text, "##,##0.00"
End If
Else
MsgBox "Numbers only in this field!", vbOKOnly + vbCritical, "Numeric Field"
myReturn = True
End If
Case Else
End Select
vReturn = myReturn
End Function
============
Thanks,
Dan