View Full Version : Required fields / Enable Button failed


china99boy
10-07-2007, 07:52 AM
Me again, I am exporting data on my form to bookmarks in Ms Word, which works great. On my form I have a print command button which prints the word document. What I have done is set some fields to required fields and would like to enable the print button only when all required fields are filled. I have this code in the on current event of the form. Right now it disables the button when the form opens, but when the required fields are populated, the print button does not become enabled. Did I miss a step somewhere?

Private Sub Form_Current()

If Me.NewRecord = True Then
Me.cmdPrintCheck.Enabled = False
Exit Sub
End If
If IsNull(Me!txtFirstName) Then
ElseIf IsNull(Me!txtLastName) Then
ElseIf IsNull(Me!cmbInstitution) Then
ElseIf IsNull(Me!txtReference) Then
ElseIf IsNull(Me!txtExceedAmt) Then
ElseIf IsNull(Me!txtVoidDate) Then
ElseIf IsNull(Me!CheckNo) Then
Me.cmdPrintCheck.Enabled = False
Else
Me.cmdPrintCheck.Enabled = True
End If

End Sub

boblarson
10-07-2007, 08:13 AM
Did I miss a step somewhere?
Yes, the On Current event ONLY occurs when moving from record to record. So, making changes to a text box after the On Current has occured will not fire the event again.

You need to use the form's BEFORE UPDATE event for validation and then use Cancel = True if the validation fails, otherwise just let it go through.

china99boy
10-07-2007, 11:13 AM
Ok, thanks for the help, I made the recommended changes and that seem to do the trick, but it scrolls to a new record before the user gets a chance to click on the now enable print button. Is there a better way to handle my situation. Once all the required fields are filled in and the print button is enable,the user should be able to click that print button to both print and save the record. I don't need for access to move to the next blank record unless I that print button is pressed. Hope this makes sense. Thanks in advance.

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Err


If IsNull(Me!txtFirstName) Then
ElseIf IsNull(Me!txtLastName) Then
ElseIf IsNull(Me!cmbInstitution) Then
ElseIf IsNull(Me!txtReference) Then
ElseIf IsNull(Me!txtExceedAmt) Then
ElseIf IsNull(Me!txtVoidDate) Then
ElseIf IsNull(Me!CheckNo) Then
Me.cmdPrintCheck.Enabled = False
Cancel = True
Else
Me.cmdPrintCheck.Enabled = True
End If

WayneRyan
10-07-2007, 11:47 AM
CB,

Assuming, you want to be able to print existing records, then use
the OnCurrent event.

If you want to print modified records, then use the BeforeUpdate.

You can use both!

1) Your current If ... ElseIF only checks on the field CheckNo,
I changed the ElseIfs to Ors

2) The fields might just be "zero-length", not null, I switched to
the Len function.


If Len(Me!txtFirstName) = 0 Or _
Len(Me!txtLastName) = 0 Or _
Len(Me!cmbInstitution) = 0 Or _
Len(Me!txtReference) = 0 Or _
Len(Me!txtExceedAmt) = 0 Or _
Len(Me!txtVoidDate) = 0 Or _
Len(Me!CheckNo) = 0 Then
Me.cmdPrintCheck.Enabled = False
Cancel = True
Else
Me.cmdPrintCheck.Enabled = True
End If


hth,
Wayne

china99boy
10-07-2007, 12:57 PM
I made the switch to the Len function, but that still does not allow the user to click the print button, because the record is saved automatically and moves to a blank record. I need the print button to be enabled before it saves or goes to a new record. Can you shed some light on this matter?

WayneRyan
10-07-2007, 01:07 PM
CB,

Where's your code to go to the next record?

I'm assuming that you're OK when viewing (but not changing) an existing
record.

Wayne

china99boy
10-07-2007, 01:21 PM
Currently when the my form opens up, the users has a blank record in which they enter the data. I am not currently using in any save commands. Access does this automatically when the user tabs thru the fields. Currently if I click on my cmdPrintCheck button, it prints the current information on the form that the user is currently filling out, but I need to make sure that all fields are filled in before the print button is allowed. Once the print button is pressed, I need to the record to be saved at that point. Don't want it to be saved any other time.

Private Sub cmdPrintCheck_Click()


DoCmd.RunCommand (acCmdSelectRecord)
DoCmd.RunCommand (acCmdSaveRecord)

'Declare the follwing
Dim objWord As Word.Application

'Set word as an application and make it invisible
Set objWord = CreateObject("Word.Application")
objWord.Visible = False 'True is visible

'path and name of the template your are using.
objWord.Documents.Add ("R:\Call Center\Call Center Departments\Mortgage Dept\Mortgage Statistics & Tracking\AutoPower\Temp\AutoPowerTemplate.dot")

'This is for the bookmark that you created in the template

objWord.ActiveDocument.Bookmarks("MemberName").Select
objWord.Selection.Text = (CStr(Me!FIRST_NAME & Me!LAST_NAME))

objWord.ActiveDocument.Bookmarks("ExceedAmt").Select
objWord.Selection.Text = Format(CCur(CStr(Me!ExceedAmt)), "Currency")



'Print the document in the foreground so Microsoft Word will not close
'until the document finishes printing.


objWord.ActiveDocument.PrintOut Background:=False

'Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

'Quit Microsoft Word and release the object variable.
objWord.Quit
Set objWord = Nothing
Exit Sub

End Sub

WayneRyan
10-07-2007, 08:48 PM
CB,

I think I understand what you are trying to do here.

1) When the user starts a new record do nothing.
2) ONly move to a new record after successfully filling out ALL fields.
3) When successful, then PRINT and move to the new record.

I'd do this by.


1) In table design set all appropriate fields --> Required = Yes
2) Your form should be --> DataEntry = Yes
AllowEdits = No
3) Use the BeforeUpdate of each individual control to SetFocus to the "next" control.
4) Use the form's OnError event to trap for DataErr = 3314 (<-- I think) and
give the user whatever nice message conveys "Enter all of the fields".

Now, you've got them trapped, once they enter ALL fields, use the form's AfterInsert
event to Print the record. You don't even need a PRINT button, but maybe a nice
message box would help.


Is that what we're doing?
Wayne

china99boy
10-08-2007, 01:10 AM
Yes, this is exactly what should happen except that I would rather have a msgbox asking the user yes/no are they sure they want to save and print record and have option to cancel.

I totally understand how your method will work if I set the required fields to yes at the table level, but this may sound silly, but I have several other forms that feed data into some of these fields and are not required depending on which form the user is using. Currently I have other required fields, but those are setup at the form level using vba coding. Although your method would work, is there a workaround to avoid setting the table fields to required?