Making sure all fields have data before user can use a command button

SpiritedAway

Registered User.
Local time
Today, 06:56
Joined
Sep 17, 2009
Messages
97
I have a mail merge button on my form that will merge the data in my fields into specific places bookmarked on my word template.

If the user hits the mail merge button without having filled out all the fields i want to have a message box pop up telling them that they have to fill in the fields before they can use the mail merge button.

I know it must be a simple way of doing this - but I'm getting mind-block here.

Thanks in advance for your help in this.

SpiritedAway.
 
As with most things that you might want to do in Access, there are multiple ways of accomplishing what you want, but most likely the simplest way would be to make each field in your table a "required" field. This will produce a generic message telling the use that they must enter data. (Not very user friendly, but it would work.)

You could also use validation for each field and the have a message tailored specifically for each field where data entry is required. (Much more user friendly, but requires more work on your part.)

Lastly you could have the command button to be disabled when your form opens and then enable this button for use, using VBA code, only when you have verified through VBA code that each field on your form as had data entered to your satisfaction. Using this method you can control evey aspect of the data entry process. (The most user friendly method, but requires the most work on your part.)

Now it is your choice.

Good luck with your project.
 
A code I like to use for data validation is below. This is set for the BeforeUpdate event .... But could be changed to a function and called from the onclick event of your button.... OR just put on the OnClick event of your button.... Depends on how you want it to act...

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)


'Place an asterisk (*) in the Tag Property of the text boxes you wish to validate.
'Then in the BeforeUpdate Event of the form, copy/paste the following:
'modified to add Label text to message box instead of field name
'ctl.Controls(0).Caption instead of ctl.Name
'This will make much more sense to a user


Dim msg As String, Style As Integer, Title As String
Dim nl As String, ctl As Control, Source As String



nl = vbNewLine & vbNewLine


For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Trim(ctl & "") = "" Then
msg = "Data Required for '" & ctl.Controls(0).Caption & "' field" & nl & _
"You can't save this record until this data is provided" & nl & _
"Enter the data and try again . . . "
Style = vbCritical + vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next

End Sub
 
I have had a rethink - what I have is a combo box, if it is empty then user cannot use the Mail Merge Button, no matter how many times they click on the button if the combo box (which is called "selectobject") is empty then the button simply will not do anything. It's inactive. The button can only be active and used if the user selects something from the combo box.

Sorry, I know it feels like I'm treading water here, but is a code that will work.

Thanks

SpiritedAway
 
Last edited:
Guys I came across this bit of code that works a treat!

Private Sub CallObject_BeforeUpdate(Cancel As Integer)
If CallObject.Text = "" Then
MergeBttn.Enabled = False
Else
MergeBttn.Enabled = True
End If
End Sub

Does what exactly it says on the tin.

Thanks everyone for their help - it is greatly appreciated!!!!!
 

Users who are viewing this thread

Back
Top Bottom