[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1137
  • Last Modified:

Displaying asterisk "*" text in required form fields

How do I display to the user which fields are the "required" fields when they click the update or save button.  Then, the form displays the required fields, with the asterik "*" symbol next to the label or the input field to show the user that the field is required. I know there is a "Required Field Text:" option, but that is for the error message that is display next to the input field.

In other words,
(1). Use an asterisk "*" to highlight information that has been filled out incorrectly or which is compulsory.
(2). Ensure that the "*" is grouped with the text label.
(3). The "*" can be colored, in red for example, to draw attention to it as,and
(4). Making sure tat the asterik " * " only appear at the missing form fields and then disappear after
       all missing fields are completed.
Suggestions as to the design and code implementation will be appreciated.
0
billcute
Asked:
billcute
  • 9
  • 3
  • 2
  • +2
3 Solutions
 
antontolentinoCommented:
change the Caption property of the Label

let say for example a Name field having label "NAME:" is required, you can put this code

Label1.Caption = "NAME:*"

to make it red

Label1.Forecolor = #ED1C24

you can put these code in your after_update event of the required field.

provided that the name of your text field is txtName, you code will look something like this

if isnull(txtName.value) then
   Label1.Caption = "NAME:*"
   Label1.Forecolor = #ED1C24
end if

0
 
billcuteAuthor Commented:
antontolentino:
I currently have a working code that checks for "required field" and then will list all required fields in a message box.  If you could modify my current code to work your suggested code above, it will be great.
I'll post the code if you want.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
An alternative thing you can do is put something like this in the Format property of a text box:

@;"<required>"   or   ;;;[Red]"<required>"

If you have a date/time field and you already have a Format, you can do this:

ddd mm-dd-yyyy;;;[Blue]"<required>"

So, whenever the control is blank, you will see the word <required> or whatever word you like.

mx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
billcuteAuthor Commented:
MX,
I am hoping for asterik that would only display when user clicks the "save" button and some fields are incorrectly filled or required field are missing. As I have said in my previous post to "antontolentino", I do have a working function that I would wish to convert from "msgbox" type flag  to asterik type design.

If you are wishing to help on this, it will be great.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Bill, I would love to help you, but there is a fair amount of complexity to get the working 'just right' ... cover all cases such that asterisks are displayed (or not) when they should be.  Unfortunately, I am really short on time today. Sorry ...

mx
0
 
billcuteAuthor Commented:
mx,
Not to worry, I can understand.

Regards
Bill
0
 
billcuteAuthor Commented:

I tried your suggested code dated 10/07/07. I recieved a compile error:
"variable not defined"

..on
Label1

.....from:

Private Sub txtName_AfterUpdate()
If IsNull(txtName.Value) Then
   Label1.Caption = "Name:*"
   Label1.ForeColor = "#ED1C24"   ' <<<--- I also corrected this line as shown
End If
End Sub
0
 
rockiroadsCommented:
Hello Bill, nice to see you still around.
This is a quick post as Im extremely busy at work

Suggestion,  say had 5 textboxes and 5 labels (these are labelled with * and perhaps colour red - all done in design time)

all textboxes are prefixed with "txt"
all labels are prefixed with "lblReq"
all textboxes and labels end with a numeric
eg
txtPrompt1, lblReqPrompt1
txtPrompt2, lblReqPrompt2
txtPrompt3, lblReqPrompt3
etc
"Prompt" can be any word you want


Now in form_load, u want to ensure labels are hidden

for i = 1 to 3   '3 is max number you have
    me("lblReq" & i).visible = false
next i


Now in your validation routine, you can loop through and check the values.
In this example, I am using the tag value to determine if its mandatory or not


    Dim i As Integer
   
    For i = 1 To 3   '3 controls
   
        'Hide existing asterix
        Me.Controls("lblReq" & i).Visible = False

        'If tag contains the word req then its required field
        If Me.Controls("txt" & i).Tag = "req" Then
            If IsNull(Me.Controls("txt" & i).Value) Then
                Me.Controls("lblReq" & i).Visible = True
            End If
        End If
    Next i



You can then use this to determine whether to complete your operation or not.

But its a start no less.

if you try this then apologies if I dont respond soon. Work is keeping me extra busy
0
 
rockiroadsCommented:
urm, typo, I didnt cater for "Prompt"
so its

    Dim i As Integer
    Dim ctl As Control
   
    'max 3 textbox checks
    For i = 1 To 3
        Me.Controls("lblReq" & i).Visible = False
    Next i
   
    'For each control
    For Each ctl In Me.Controls
   
        'If its a textbox (identified by prefix)
        'And its required
        If Left$(ctl.Name, 3) = "txt" And ctl.Tag = "req" Then
           
            'If no value specified
            If IsNull(ctl.Value) = True Then
           
                'Assumes you have max 9 textboxes,
                'Need alternative way to get number otherwise
                'Number to get is the number on that textbox name
                i = Right$(ctl.Name, 1)
               
                'Show req label (asterix)
                Me("lblReq" & i).Visible = True
            End If
        End If
    Next ctl

0
 
rockiroadsCommented:
'Dead Simple version of extracting numbers
'Assume max 99 fields, any more is just way to silly
'Get last 2 characters, if first is not digit then assume its single
Public Function GetNumberFromText(ByVal sWord As String) As Integer

    Dim sLastTwo As String
   
    sLastTwo = Right$(sWord, 2)
    If IsNumeric(sLastTwo) Then
        GetNumberFromText = Val(sLastTwo)
    Else
        GetNumberFromText = Val(Right$(sLastTwo, 1))
    End If
End Function


probably better way to do it, I do have a function that does it somewhere but cant spend time.

Im done with EE for a bit now
0
 
billcuteAuthor Commented:
rockiroads,
Thank you for taken time to response to my post out of your busy schedule.

Yes, I noticed that you have not appeared on EE for awhile now but I have been posting new questions for once awhile to polish my current working code - nothing big really.

I homestly appreciate your response on this post. I read through the code - seems a little confused about the implementation - i.e the steps of implementing both "numbers" and "text" code you posted above..

Your first post was well understood - the other two followup posts seesmsomewhat confusing - may require furher explanation for me to grasp it. However, based what I have seen it's definitely waht I am looking for.
(1). I curently have: textboxes and checkboxes (radio and box type checkboxes).
(2). Some of the texboxes are numeric while others are text type boxes.
(3). I have criteria expression for some of the "required" tag fields as well such as (Case 3"ne") below.
I have decided to post my current working "Tag" functions, If you could modify these functions to work with the ones you already posted above - it will be great.
' *****************
Public Function ChkCtl(ByRef mCtl As Control) As Boolean
  ChkCtl = True
    If mCtl.Properties("TabIndex") <= Screen.ActiveControl.Properties("TabIndex") Then  ' <<--additional
       Select Case ExtractTag(mCtl.Tag, 2)   '<<-- Another function for extracting "tag"
                                                                     ***Tags are set in controls like: [ne] [1ne] or [2ne]
         Case "ne"
                If Len(Nz(mCtl.Value)) = 0 Then ChkCtl = False
         Case "1ne"
                If Len(Screen.ActiveForm.txtPermitNo) > 0 And Len(Nz(mCtl.Value)) = 0 Then ChkCtl = False
        Case "2ne"
                If Len(Screen.ActiveForm.cboSConnType) > 0 And Len(Nz(mCtl.Value)) = 0 Then ChkCtl = False
        Case "3ne"                                      '<<--- Case '3ne' is based on criteria expression
                If Len(Screen.ActiveForm.txtPermitNo) > 0 And _
                    Screen.ActiveForm.cboSConnType = "NC" And _
                    (Screen.ActiveForm.cboSewerType = "SA" Or _
                    Screen.ActiveForm.cboSewerType = "CM") And _
                    Len(Nz(mCtl.Value)) = 0 Then ChkCtl = False
        Case ">0"
                If Not Nz(mCtl.Value) > 0 Then ChkCtl = False
       Case "tf"
                If Nz(mCtl.Value, 99) <> 0 And Nz(mCtl.Value, 99) <> -1 Then ChkCtl = False
      End Select
  End If                            '<<--Additional
  Exit Function
End Function
' *************
Function MandatoryFields() As Boolean      
  Dim myCtl As Control
  Dim myCtlList As String
    MandatoryFields = True
        ' First the basic controls
            For Each myCtl In Me.Controls
                If Len(myCtl.Tag) > 1 Then
                    If ChkCtl(myCtl) = False Then
                        myCtlList = myCtlList & Me.Controls("l_" & myCtl.Name).Caption & ", "
                    On Error Resume Next
                        myCtl.Properties("BackColor") = 8454143
                    On Error GoTo 0
                Else
                    On Error Resume Next
                        myCtl.Properties("BackColor") = 16777215
                    On Error GoTo 0
                End If
            End If  
        Next
                If Len(myCtlList) > 0 Then MsgBox "Please fill the following field(s) before continuing:" & vbCrLf & myCtlList
            Exit Function
    End Function        
' ***********
Note:
You may post your amended code at your convenience. You can even combine my two functions into one - whatever you feel is more efficient will be appreciated.

Regards
Bill
0
 
billcuteAuthor Commented:
rockiroads:
Please disregard my last post. I have been able to figure out your code. I put the code together per your last three posts. It works great. I really appreciate your taken time to assist with the code despite your busy schedule.

Regards
Bill
0
 
billcuteAuthor Commented:
rockiroads:
I'll split your code among all your three posts.
Regards
Bill
0
 
billcuteAuthor Commented:
rockiroads:
Sorry.... I meant to say that I'll split your points on your three assisted code.
Regards
Bill
0
 
dlottsCommented:
There is a simple way to modify the label associated with a text box in Access 2007 (unsure if this is true in earlier versions)

dim oLabel as Label
Set oLabel = oTextBox.Controls(0)
oLabel.caption = "New caption" <- add or remove * as required.
0
 
billcuteAuthor Commented:
dlotts:
Happy New Year.
I didnt quite understand your suggestion but I can open a new post on the subject and upload a sample db to explore your method.

Regards
Bill
0
 
dlottsCommented:
In your code, you get the associated label by using a naming convension.  That will work fine but requires some maintenance to keep everything named correctly.

By changing
myCtlList = myCtlList & Me.Controls("l_" & myCtl.Name).Caption & ", "
to something like
dim oLabel as Label
Set oLabel = myCtl.Controls(0)
myCtlList = oLabel.Caption & ", "

you no longer need to enforce the naming convention.  (This assumes you always have an associated label)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now