Ken McLennan wrote :
G'day there Gary,
The user enters their data into the fields. They then click on "Enter Data".
"Validation" is actually poor terminology on my part. The code checks for an
entry to each field & nothing more; except for the combobox which has its
style set to fmStyleDropDownList so it only accepts sets values.
If all fields have entries then the record is saved. If not, then the
offending field has its background colour changed and the record sits there
awaiting correction.
Sorry I wasn't more clear. Normally, I would not allow the "Enter Data"
button to be enabled until all fields were validated as 'good-to-go'.
This precludes, then, that each control would validate user
inputs/selections in an event procedure (Change/Click or ?) and
increment a variable that totals the correct number of required
inputs/selections. Once this is reached the "Enter Data" button gets
enabled.
IOW, there's no way a user can execute processing the data until all
fields are validated. It just makes sense to me to validate user action
at the control level, NOT the button that processes the data.<g> I
guess it's a matter of logistics preference, but the methodology I use
follows what most seasoned VB developers do when working with data.
Also, most data controls follow this same convention.
2. What is the validation doing that it clears the listboxes?
<ergo: post your code>
-----[ SNIP ]-----
Function CompleteRecord() As Boolean
Dim int_X As Integer
Dim str_X As String
' Boolean - False = 0; True = -1
Dim bool_A As Boolean, bool_B As Boolean, bool_C As Boolean, bool_D As
Boolean, bool_E As Boolean
CompleteRecord = True
With frm_Main
If Len(Trim(.ComboBox6.Value)) = 0 Then
bool_A = False
.ComboBox6.BackColor = 52479
Else
bool_A = True
.ComboBox6.BackColor = vbWhite
End If
If Len(Trim(.TextBox6.Value)) = 0 Then
bool_B = False
.TextBox6.BackColor = 52479
Else
bool_B = True
.TextBox6.BackColor = vbWhite
End If
If Len(Trim(.TextBox4.Value)) = 0 Then
bool_C = False
.TextBox4.BackColor = 52479
Else
bool_C = True
.TextBox4.BackColor = vbWhite
End If
str_X = ""
For int_X = 0 To .ListBox9.ListCount - 1
If .ListBox9.Selected(int_X) Then
str_X = str_X & .ListBox9.List(int_X)
End If
Next
If Len(str_X) = 0 Then
bool_D = False
frm_Main.ListBox9.BackColor = 52479
Else
bool_D = True
frm_Main.ListBox9.BackColor = vbWhite
End If
str_X = ""
For int_X = 0 To .ListBox10.ListCount - 1
If .ListBox10.Selected(int_X) Then
str_X = str_X & .ListBox10.List(int_X)
End If
Next
If Len(str_X) = 0 Then
bool_E = False
frm_Main.ListBox10.BackColor = 52479
Else
bool_E = True
frm_Main.ListBox10.BackColor = vbWhite
End If
CompleteRecord = bool_A And bool_B And bool_C And bool_D And bool_E
End With
End Function
-----[ UNSNIP ]-----
Here 'tis as requested. Any advice gladly accepted.
I don't see anything here that would clear the listboxes.
To explain further what I was suggesting regarding the validations
being done by control events:
I would color the background of all controls that require
inputs/selections with a non-offensive color like light green or light
yellow. This provides a visible cue that these fields are mandatory.
I would validate the inputs in the textboxes/combobox in the
AfterUpdate event. Here I'd set the backcolor to white if inputs are
valid.
Private Sub ComboBox6_AfterUpdate()
With Me.ComboBox6
If Len(Trim(.Text)) > 0 Then _
lValidInputs = lValidInputs + 1: .BackColor = vbWhite
End With 'Me.ComboBox6
Me.cmdEnterData.Enabled = (lValidInputs = lREQD_FIELDS)
End Sub 'ComboBox6_AfterUpdate()
Private Sub TextBox6_AfterUpdate()
With Me.TextBox6
If Len(Trim(.Text)) > 0 Then _
lValidInputs = lValidInputs + 1: .BackColor = vbWhite
End With 'Me.TextBox6
Me.cmdEnterData.Enabled = (lValidInputs = lREQD_FIELDS)
End Sub 'TextBox6_AfterUpdate()
ListBoxes:
The way you test the multi-select listboxes is fine but why keep going
once you find a selected item? Using the listbox's Change event tells
you that a selection has happened, but not if an item was unselected,
and this will fire for each selection change. Instead, I'd use the
AfterUpdate event to run your loop, adding to the counter and exiting
the loop when the 1st selected item is found.
Private Sub ListBox9_AfterUpdate()
With Me.ListBox9
For int_X = 0 to .Listcount - 1
If .Selected(int_X) Then _
lValidInputs = lValidInputs + 1: .BackColor = vbWhite: Exit For
Next 'int_X
End With 'Me.ListBox9
Me.cmdEnterData.Enabled = (lValidInputs = lREQD_FIELDS)
End Sub 'ListBox9_AfterUpdate()
Now, your "Enter Data" button is only available AFTER all
inputs/selections are validated, and it only has to process the data.
The CompleteRecord function is no longer needed. The controls validate
themselves as the user interacts with each one.
HTH