field must be filled in first

maxmangion

AWF VIP
Local time
Today, 15:35
Joined
Feb 26, 2003
Messages
2,805
i have a form bounded to a query and the fields are placed on a tab control with 4 pages. Now when adding a new record i have a particular field (textbox), which i would like it to be filled in first. Therefore, if it is null i want that any input on the other controls would result in a msgbox saying that the textbox must be filled in first.

What it the best approach so that i would avoid to make a piece of code on each and every control to check if the first textbox is null ?

Thanks!
 
Hi,

You need some code like this:

IF ISNULL(textbox1) then
msgbox "Please enter a value"


you will need to change textbox1 to the name of your text box on the tabbed form.
 
I made a macro code for empty fields that need to be filled in first before going to new record.

Or

You can use a this code below for module:

Option Compare Database
Option Explicit

Public Function CheckForEntries() As Boolean
'On Error GoTo Err_CheckEntries

Dim strMessage As String
Dim ctl As Control
Dim strControl As String

'set all controls to normal white background
Call SetBackColorNormal

'if not a new record check each controls tag property for Req
If Not Screen.ActiveForm.NewRecord Then
For Each ctl In Screen.ActiveForm.Controls
If (ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox) And ctl.Tag = "Req" Then
'only build a message for the first required control without an entry
If Nz(Len(ctl.Value), 0) = 0 And Nz(Len(strMessage), 0) = 0 Then
'this function requires that the label for the control have the same name as the control
'followed by _Label
strMessage = Screen.ActiveForm(ctl.ControlName & "_Label").Caption & " is a required field."
'set the strControl variable to the name of the required control that is not filled in
strControl = ctl.ControlName
End If
End If
Next

'if there is no message, then all required controls have entries
If Nz(Len(strMessage), 0) > 0 Then
MsgBox strMessage, vbOKOnly, "Required field left blank!"
'set the back color of the required control in question to yellow
Screen.ActiveForm(strControl).BackColor = 65535 'Bright Yellow
'and set the focus to the control
Screen.ActiveForm(strControl).SetFocus
'and set the checkforentries to True so that any code in the form can be handled
CheckForEntries = True
Else
'if all required controls have entries set checkforentries to false
CheckForEntries = False
End If
End If

Exit_CheckEntries:
Exit Function

Err_CheckEntries:
MsgBox Err.Description
GoTo Exit_CheckEntries

End Function

Public Sub SetBackColorNormal()
On Error Resume Next
Dim ctl As Control

For Each ctl In Screen.ActiveForm.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
ctl.BackColor = -2147483643 'normal white back color
End If
Next

End Sub


And Place the code below in a form property

Private Sub Form_Current()
Call SetBackColorNormal
End Sub


Set the tag in property to Req

If you want I can attach a sample for you

Michael
 
SQL_Hell,

yes i have used that piece of code on other occasions, but i would like to avoid placing that piece of code on each and every control.

Maybe there is a particular event, which i can use on the form ?
 
I would go for the simple way out and make all other fields invisible until the field you want has been updated
On open set every other field to invisible and after update to visible
You would also need to use this each time you went to a new record

Hope this makes sense
 
i wouldn't like to make all the other fields invisible until i enter something in the first textbox.

i thought of using the OnDirty event of the form and place the following:

if isnull(me.textbox) then
docmd.cancelevent
msgbox "message goes here"
end if

The problem with the above is that when i am adding a new record, when i try to type something in the first field which i would like to fill, i am getting the msgbox (which makes sense). Therefore, i would like to make something similar but which does not concern that particular field.

Any other suggestions pls ?
 

Users who are viewing this thread

Back
Top Bottom