VBA Code:
Option Explicit
Public IsCancelled As Boolean
Private Sub UserForm_Initialize()
IsCancelled = True
End Sub
Private Sub btnANAClose_Click()
Me.Hide
End Sub
Private Sub btnANAOk_Click()
If IsInputOk Then
'ANAFormToSheet
IsCancelled = False
'Me.Hide
End If
End Sub
Public Sub SetValues(udtANADataEntry As ANADataEntry)
With udtANADataEntry
SetValue Me.lbxANADate, .Date
SetValue Me.lbxANAShift, .Shift
SetValue Me.lbxANANames, .Name
End With
End Sub
Public Sub GetValues(ByRef udtANADataEntry As ANADataEntry)
With udtANADataEntry
.Date = GetValue(Me.lbxANADate, TypeName(.Date))
.Shift = GetValue(Me.lbxANAShift, TypeName(.Shift))
.Name = GetValue(Me.lbxANANames, TypeName(.Name))
End With
End Sub
Private Function IsInputOk() As Boolean
Dim ctl As MSForms.Control
Dim strMessage As String
IsInputOk = False
For Each ctl In Me.Controls
If IsInputControl(ctl) Then
If IsRequired(ctl) Then
If Not HasValue(ctl) Then
strMessage = ControlName(ctl) & " must have value"
End If
End If
If Not IsCorrectType(ctl) Then
strMessage = ControlName(ctl) & " is not correct"
End If
End If
If Len(strMessage) > 0 Then
ctl.SetFocus
GoTo HandleMessage
End If
Next
IsInputOk = True
HandleExit:
Exit Function
HandleMessage:
MsgBox strMessage
GoTo HandleExit
End Function
Public Sub FillList(ControlName As String, Values As Variant)
With Me.Controls(ControlName)
Dim iArrayForNext As Long
.Clear
For iArrayForNext = LBound(Values) To UBound(Values)
.AddItem Values(iArrayForNext)
Next
End With
End Sub
Private Function IsCorrectType(ctl As MSForms.Control) As Boolean
Dim strControlDataType As String, strMessage As String
Dim dummy As Variant
strControlDataType = ControlDataType(ctl)
On Error GoTo HandleError
Select Case strControlDataType
Case "Boolean"
dummy = CBool(GetValue(ctl, strControlDataType))
Case "Byte"
dummy = CByte(GetValue(ctl, strControlDataType))
Case "Currency"
dummy = CCur(GetValue(ctl, strControlDataType))
Case "Date"
dummy = CDate(GetValue(ctl, strControlDataType))
Case "Double"
dummy = CDbl(GetValue(ctl, strControlDataType))
Case "Decimal"
dummy = CDec(GetValue(ctl, strControlDataType))
Case "Integer"
dummy = CInt(GetValue(ctl, strControlDataType))
Case "Long"
dummy = CLng(GetValue(ctl, strControlDataType))
Case "Single"
dummy = CSng(GetValue(ctl, strControlDataType))
Case "String"
dummy = CStr(GetValue(ctl, strControlDataType))
Case "Variant"
dummy = CVar(GetValue(ctl, strControlDataType))
End Select
IsCorrectType = True
HandleExit:
Exit Function
HandleError:
IsCorrectType = False
Resume HandleExit
End Function
Private Function ControlDataType(ctl As MSForms.Control) As String
Select Case ctl.Name
Case "lbxANADate": ControlDataType = "Date"
Case "lbxANAShift": ControlDataType = "Variant"
Case "lbxANANames": ControlDataType = "Variant"
End Select
End Function
Private Function ControlName(ctl As MSForms.Control) As String
On Error GoTo HandleError
If Not ctl Is Nothing Then
ControlName = ctl.Name
Select Case TypeName(ctl)
Case "TextBox", "ListBox", "ComboBox"
If ctl.TabIndex > 0 Then
Dim c As MSForms.Control
For Each c In Me.Controls
If c.TabIndex = ctl.TabIndex - 1 Then
If TypeOf c Is MSForms.Label Then
ControlName = c.Caption
End If
End If
Next
End If
Case Else
ControlName = ctl.Caption
End Select
End If
HandleExit:
Exit Function
HandleError:
Resume HandleExit
End Function
Private Function IsRequired(ctl As MSForms.Control) As Boolean
Select Case ctl.Name
Case "lbxANADate", "lbxANAShift", "lbxANANames"
IsRequired = True
Case Else
IsRequired = False
End Select
End Function
Private Function IsInputControl(ctl As MSForms.Control) As Boolean
Select Case TypeName(ctl)
Case "TextBox", "ComboBox", "ListBox", "CheckBox", "OptionButton", "ToggleButton"
IsInputControl = True
Case Else
IsInputControl = False
End Select
End Function
Private Function HasValue(ctl As MSForms.Control) As Boolean
Dim var As Variant
var = GetValue(ctl, "Variant")
If IsNull(var) Then
HasValue = False
ElseIf Len(var) = 0 Then
HasValue = False
Else
HasValue = True
End If
End Function
So used code vba add in to generate a userform with 3 listboxs. but I am lost as to why so much code is needed. what does it all do and where do I add the things I need??
Private Function GetValue(ctl As MSForms.Control, strTypeName As String) As Variant
On Error GoTo HandleError
Dim value As Variant
value = ctl.value
If IsNull(value) And strTypeName <> "Variant" Then
Select Case strTypeName
Case "String"
value = ""
Case Else
value = 0
End Select
End If
HandleExit:
GetValue = value
Exit Function
HandleError:
Resume HandleExit
End Function
Private Sub SetValue(ctl As MSForms.Control, value As Variant)
On Error GoTo HandleError
ctl.value = value
HandleExit:
Exit Sub
HandleError:
Resume HandleExit
End Sub
Last edited by a moderator: