Required Data WITHOUT Msgboxes

  • Thread starter Thread starter newbie via AccessMonster.com
  • Start date Start date
N

newbie via AccessMonster.com

Hi

Would anyone know how to do the following -

I have a border box around 4 textboxes (name, Description, StartDate and
EndDate). When a user types in info in any of these textboxes - the border
box around these textboxes should change colour until the user has stored
info in all 4 textboxes. Once all 4 textboxes are Not Null - the border
colour should change back to original colour.

Also, once the user has typed info in any of the 4 textboxes - they MUST
store info in the other 3 textboxes.

At present - i have 2 borderboxes, one black(null info) and one navy(not null)
. i have the following code

If Not (IsNull(me.Name)) then
borderboxNavy.visible = true
borderboxBlack.visible = false
else
if IsNull (me.Name) then
borderboxNavy.visible = false
borderboxBlack.visible = true

however it doesnt
1) work!!
2) state to the user that by entering info in Name txtbox, they must store
info in the other 3 textboxes - without having a textbox appear everytime
they move along textboxes

To all the access god's - can this be done??
 
You will need to use several events to achieve what you want, so it makes
sense to write a function that highlights the boxes, and call it from all
the events where you need it.

A text box named "Name" is going to give you problems: since the form has a
Name property, Access is going to get confused and think you are talking
about the name of the form instead of the value in the control named Name. I
suggest you rename this field, and change the Name and ControlSource of the
text box before you start. The example below assumes the text box is called
ClientName. It assumes the 2nd text box is called ClientDescrip.

The example shows what to put into the module of your form. As always, the
module begins with the Option statements. With them are the definitions of
the colors to use for the highlighted text box or normal text box. Change
those colors to suit. (Note that the code uses the Border of the text boxes
instead of needing extra rectangles.)

Next is the function that does the highlighting. It accepts an argument
telling it whether to check the OldValue of the text boxes, since that's
what you need to do in the form's Undo event to reset them. In all other
events, this argument will be False.

The function then sets a yes/no variable named bNull to True if any of the
text boxes has the Null value (or if the OldValue is null for the undo
case.) It then tests if the BorderColor is set correctly, and if not changes
the BorderColor to the appropriate value.

This function is called by the AfterUpdate event of all 4 text boxes. It is
also called by the Current event of the form so they are set correctly when
you move record, and by the Undo event of the form so it is reset when you
undo a record.

Finally, the BeforeUpdate event of the form tests for nulls to prevent an
unacceptable record being saved. This is necessary since you will have to
remove the Required field from the fields in your table, so as to avoid the
error message that is triggered when you clear a text box bound to a
Required field. (If you want engine-level validation, you could enter a
Validation Rule for the table instead.)

Okay, here's the kind of thing you would end up with in the module of your
form:
*************************code begins***********************
Option Compare Database
Option Explicit
Const mlngcHighlight = vbRed
Const mlngcNormalBorder = vbBlack

Private Function DoHighlight(bUseOldValue As Boolean)
Dim bNull As Boolean
If bUseOldValue Then
bNull = (IsNull(Me.ClientName.OldValue) Or _
IsNull(Me.ClientDescrip.OldValue) Or _
IsNull(Me.StartDate.OldValue) Or _
IsNull(Me.EndDate.OldValue))
Else
bNull = (IsNull(Me.ClientName) Or _
IsNull(Me.ClientDescrip) Or _
IsNull(Me.StartDate) Or _
IsNull(Me.EndDate))
End If
If bNull Then
If Me.ClientName.BorderColor <> mlngcHighlight Then
Me.ClientName.BorderColor = mlngcHighlight
Me.ClientDescrip.BorderColor = mlngcHighlight
Me.StartDate.BorderColor = mlngcHighlight
Me.EndDate.BorderColor = mlngcHighlight
End If
Else
If Me.ClientName.BorderColor <> mlngcNormalBorder Then
Me.ClientName.BorderColor = mlngcNormalBorder
Me.ClientDescrip.BorderColor = mlngcNormalBorder
Me.StartDate.BorderColor = mlngcNormalBorder
Me.EndDate.BorderColor = mlngcNormalBorder
End If
End If
End Function

Private Sub ClientDescrip_AfterUpdate()
Call DoHighlight(False)
End Sub
Private Sub ClientName_AfterUpdate()
Call DoHighlight(False)
End Sub
Private Sub StartDate_AfterUpdate()
Call DoHighlight(False)
End Sub
Private Sub EndDate_AfterUpdate()
Call DoHighlight(False)
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If IsNull(Me.ClientName) Then
Cancel = True
strMsg = strMsg & "Name required." & vbCrLf
End If
If IsNull(Me.ClientDescrip) Then
Cancel = True
strMsg = strMsg & "Description required." & vbCrLf
End If
If IsNull(Me.StartDate) Then
Cancel = True
strMsg = strMsg & "Start Date required." & vbCrLf
End If
If IsNull(Me.EndDate) Then
Cancel = True
strMsg = strMsg & "End Date required." & vbCrLf
End If
If Cancel Then
strMsg = strMsg & vbCrLf & _
"Correct the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, "Invalid data"
End If
End Sub
Private Sub Form_Current()
Call DoHighlight(False)
End Sub
Private Sub Form_Undo(Cancel As Integer)
Call DoHighlight(False)
End Sub
**************************code ends************************
 
Thanks Allen, I've just typed in the code but i think im doing something
wrong -

So far, i've renamed the fields as you suggested and have

1) Created module called DoHighlight?
2) And added the Code in relevant sections (afterUpdate etc) on the form with
the 4 textboxes

It's coming up with an error when i run the form. I'm able to write data into
ClientName (first field on form) but it automatically takes me to the vb
coding, highlighting Call DoHighlight (in ClientName AfterUpdate)

How can i change this?
 
It's coming up with an error when i run the form. I'm able to write data into
ClientName (first field on form) but it automatically takes me to the vb
coding, highlighting Call DoHighlight (in ClientName AfterUpdate)

the Error Msg is - Complie Error

Expected variable or procedure, not module
 
No. don't put this in a standard module.
Put it in the module of the form where you have these 4 text boxes.
 
doh!

Thank you thank you thank you - the code works brilliantly!

Im sorry to be a HUGE pain but would the coding need to be changed alot if
the textboxes change colour once the previous one has been complete? so for
instance, start the form with all black and then once ClientName has been
entered, the next textbox - ClientDescrip changes to red while the other
textboxes remain black?
 
--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.
 
You could modify the routine so that it test each text box individually, and
changes the color accordingly. I might need to let you work out the
specifics there.

You probably understand that our goal is to give you the ability to do
things for yourself rather than to do it for you.
 
Thanks alot for your help - you've guided me down the right direction.

Allen said:
You could modify the routine so that it test each text box individually, and
changes the color accordingly. I might need to let you work out the
specifics there.

You probably understand that our goal is to give you the ability to do
things for yourself rather than to do it for you.
[quoted text clipped - 6 lines]
entered, the next textbox - ClientDescrip changes to red while the other
textboxes remain black?
 
Back
Top