S
sarndt
I created code in vba that will handle increasing/decreasing time values
using textboxes and spinbutton controls. The code works great if I reference
the textbox and spinbutton control exactly (for instance textbox1 and
spinbutton1). I then tried converting the code to a class to handle multiple
combinations of textboxes and spinbutton controls. But it doesn't work. Any
suggestions appreciated. Also how would I make sure the right textbox and
spinbutton controls are always used together?
Thanks
Steve
Private WithEvents TB As MSForms.TextBox
Private WithEvents SB As MSForms.SpinButton
Public iCur As Integer
Public strControl As String
Public strTimeChange
Public i As Integer
Public Property Set TBControl(obtNewTB As MSForms.TextBox)
Set TB = obtNewTB
End Property
Public Property Set SBControl(obtNewSB As MSForms.SpinButton)
Set SB = obtNewSB
End Property
Private Sub SB_Change()
Dim dtTime As Date
Dim Y As Integer
'If strTimeChange = "" Then
' MsgBox "Please click on a time to modify it"
' Exit Sub
'End If
dtTime = Format(TB.Value, "hh:mm AM/PM")
Y = SB.Value
If Y > i Then
TB.Value = Format(dtTime + TimeValue(strTimeChange), "hh:mm AM/PM")
Else
TB = Format(dtTime + 1 - TimeValue(strTimeChange), "hh:mm AM/PM")
End If
i = SB.Value
HighlightTime (iCur)
End Sub
Sub HighlightTime(iCur As Integer)
Dim iPos1 As Integer
Dim iPos2 As Integer
iPos1 = InStr(1, TB.Value, ":")
iPos2 = InStr(1, TB.Value, " ")
If iCur >= iPos2 Then
strTimeChange = "12:00:00"
TB.SelStart = 6
TB.SelLength = 2
ElseIf iCur >= iPos1 Then
strTimeChange = "00:01:00"
TB.SelStart = 3
TB.SelLength = 2
Else
strTimeChange = "01:00:00"
TB.SelStart = 0
TB.SelLength = 2
End If
TB.HideSelection = False
End Sub
Private Sub TB_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift
As Integer)
KeyCode.Value = vbKeyReturn
strControl = TB.Name
HighlightTime (iCur)
End Sub
Private Sub TB_MouseDown(ByVal Button As Integer, ByVal Shift As Integer,
ByVal X As Single, ByVal Y As Single)
strControl = TB.Name
iCur = TB.SelStart
HighlightTime (iCur)
End Sub
Private Sub Class_Terminate()
Set TB = Nothing
Set SB = Nothing
End Sub
using textboxes and spinbutton controls. The code works great if I reference
the textbox and spinbutton control exactly (for instance textbox1 and
spinbutton1). I then tried converting the code to a class to handle multiple
combinations of textboxes and spinbutton controls. But it doesn't work. Any
suggestions appreciated. Also how would I make sure the right textbox and
spinbutton controls are always used together?
Thanks
Steve
Private WithEvents TB As MSForms.TextBox
Private WithEvents SB As MSForms.SpinButton
Public iCur As Integer
Public strControl As String
Public strTimeChange
Public i As Integer
Public Property Set TBControl(obtNewTB As MSForms.TextBox)
Set TB = obtNewTB
End Property
Public Property Set SBControl(obtNewSB As MSForms.SpinButton)
Set SB = obtNewSB
End Property
Private Sub SB_Change()
Dim dtTime As Date
Dim Y As Integer
'If strTimeChange = "" Then
' MsgBox "Please click on a time to modify it"
' Exit Sub
'End If
dtTime = Format(TB.Value, "hh:mm AM/PM")
Y = SB.Value
If Y > i Then
TB.Value = Format(dtTime + TimeValue(strTimeChange), "hh:mm AM/PM")
Else
TB = Format(dtTime + 1 - TimeValue(strTimeChange), "hh:mm AM/PM")
End If
i = SB.Value
HighlightTime (iCur)
End Sub
Sub HighlightTime(iCur As Integer)
Dim iPos1 As Integer
Dim iPos2 As Integer
iPos1 = InStr(1, TB.Value, ":")
iPos2 = InStr(1, TB.Value, " ")
If iCur >= iPos2 Then
strTimeChange = "12:00:00"
TB.SelStart = 6
TB.SelLength = 2
ElseIf iCur >= iPos1 Then
strTimeChange = "00:01:00"
TB.SelStart = 3
TB.SelLength = 2
Else
strTimeChange = "01:00:00"
TB.SelStart = 0
TB.SelLength = 2
End If
TB.HideSelection = False
End Sub
Private Sub TB_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift
As Integer)
KeyCode.Value = vbKeyReturn
strControl = TB.Name
HighlightTime (iCur)
End Sub
Private Sub TB_MouseDown(ByVal Button As Integer, ByVal Shift As Integer,
ByVal X As Single, ByVal Y As Single)
strControl = TB.Name
iCur = TB.SelStart
HighlightTime (iCur)
End Sub
Private Sub Class_Terminate()
Set TB = Nothing
Set SB = Nothing
End Sub