Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code problem 1

Status
Not open for further replies.

MA04

Technical User
Dec 21, 2004
162
GB
Hi all,

i have a command button that appends a record on a form when pressed. I have a condition before it which prompts a user if compulsory fields are not filled in, to fill them in or append is not carried out. The problem with my code is i cannot seem to display all the text and comboboxes that have not been filled in, on one messagebox. The code i have displays them in seperate message boxes.

e.g. if 3 compulsory text or comboboxes are null:
"The following text or comboboxes have to be filled in:
textbox1
textbox2
combobox3

if 2 then:
textbox1
textbox2

I want to display on a message box all compulsory boxes that have not yet been filled in. This is my code so far:

Code:
dim ctl AS control
 For Each ctl In Me.Controls
     If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
         If Me(ctl.NAME) = "" Or IsNull(Me(ctl.NAME)) Then
             If ctl.Tag = "Required" Then
                 MsgBox "You have a Text box with no value, " & _
                             "The Text Box name is:" & vbCrLf & ctl.NAME, _
                  vbExclamation, Me.Caption
                  ctl.SetFocus
                  Me.ActiveControl.BackColor = vbYellow
                  Exit Sub
              End If
          End If
     End If
 Next

The above code displays text or comboboxes that i have placed Required under in their tag property. I want to display all the control names that have required on their tag property and are not filled in (i.e. null) on one messagebox.

Any help appreciated, thanks in advance,
M-.
 
Code:
dim ctl AS control
Dim strMsg as String [green]'String to hold missing field names[/green]

 For Each ctl In Me.Controls
     If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
         [green]' Check if its required before checking if its "" or Null[/green]
         If ctl.Tag = "Required" Then
             If ctl.Name.Value = "" Or IsNull(ctl.Name.Value)) Then
                  [green]' Missing so append its name then change its backcolor[/green]
                  strMsg = strMsg & vbTab & ctl.Name & vbcrlf
                  ctl.SetFocus
                  ctl.BackColor = vbYellow
              End If
          End If
     End If
 Next

[green]' Show the message if there are fields missing[/green]
if len(strMsg) > 0 then
   msgbox "Please fill in a vlaue for the following fields:" & vbcrlf & strMsg, _
          vbExclamation, Me.Caption
end if

This was all just typed and not tested but should work. You can add an 'Exit Sub' in the last if, if you want it to exit and not append, but I would just put the append code in an else on the last if.

Hope this helps,
Tom
 
Whoops,
you should change both instances of 'ctl.Name.Value' to just 'ctl.Value'



Sorry about that,
Tom
 
Thanks for that Tom just what i needed,

p.s. ctl.value gives me the error not ctl.name.

M-. [thumbsup2]
 
The 'default' property for most Ms. A. Controls is the ".Value", so it is generally acceptable to JUST use ctl. Some feel that the 'documentation' effect of including the ".Value" is worth the effort, but few would say it is NECESSARY.





MichaelRed


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top