Fill Fields Before Reports (1 Viewer)

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Hello,


On my Clerk form (password "clerk") is there a way to make sure these fields are filled out before the Clerk previews or prints the Tear Down Report?


AFR Number
Received Date

Clerk
Customer
Description
Part Number
Serial Number
Customer Complaint
Preliminary Inspection


Thank you very much,
Bill
 
Last edited:

Cronk

Registered User.
Local time
Today, 18:52
Joined
Jul 4, 2013
Messages
2,771
Create a new function on your form

Code:
function CheckData(ctl as control) as boolean
  
  if len(me.AFRNumber & "")<1 then
     set ctl = me.AFRNumber
     goto MissingData
  endif

  if len(me.ReceivedDate & "")<1 then
     set ctl = me.ReceivedDate
     goto CheckData_Exit
  endif

  <add similar code for all other required fields>

  CheckData= true
  exit function

MissingData:
   CheckData = false

end function


Then add to each of the print/preview buttons as per the following
Code:
Private Sub PreviewTearDown_Click()
  dim ctl as control
  
  if checkData(ctl) = false then
     msgbox "Please add data for " & ctl.name, 64,"Missing Data"
     me.ctl.setfocus
     exit sub
   endif


If there is any required field data is missing, this will advise the user accordingly and put the cursor in the first of any blank field.
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Thanks Cronk I will get to it!
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Cronk,


I'm not sure how to incorporate into existing codes for the print preview and print.


This is what I currently have:


Code:
Private Sub PreviewTearDown_Click()
    If Me.Dirty Then Me.Dirty = False
    Dim strDocName As String
    Dim strWhere As String
    strDocName = "Tear Down"
    strWhere = "AFRsID=" & Me!AFRsID
    DoCmd.OpenReport strDocName, acPreview, , strWhere
End Sub

Private Sub PrintTearDown_Click()
    If Me.Dirty Then Me.Dirty = False
    Dim strDocName As String
    Dim strWhere As String
    strDocName = "Tear Down"
    strWhere = "AFRsID=" & Me!AFRsID
    DoCmd.OpenReport strDocName, acPrint, , strWhere
End Sub
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Never mind I get errors. I will find another way.


Very sorry I know you are trying to help.


:(
 
Last edited:

Cronk

Registered User.
Local time
Today, 18:52
Joined
Jul 4, 2013
Messages
2,771
So you would add the lines in red to each of the On-click events of the print/preview buttons, viz


Code:
Private Sub PreviewTearDown_Click()
   [COLOR=red]dim ctl as control
  
   if checkData(ctl) = false then
       msgbox "Please add data for " & ctl.name, 64,"Missing Data"
       me.ctl.setfocus
       exit sub
   endif[/COLOR]

    If Me.Dirty Then Me.Dirty = False
    Dim strDocName As String
    Dim strWhere As String
    strDocName = "Tear Down"
    strWhere = "AFRsID=" & Me!AFRsID
    DoCmd.OpenReport strDocName, acPrint, , strWhere
End Sub


I can see where you had errors - from my invalid code.
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Cronk,


I tried again and get a compile error when clicking on the Preview button.


Can you take a look please?


Thank you
 
Last edited:

mike60smart

Registered User.
Local time
Today, 09:52
Joined
Aug 6, 2017
Messages
1,908
Hi

In the Before Update of the Clerk Form I placed the following Code:-

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    On Error GoTo Form_BeforeUpdate_Error
Dim ctl As Control
Dim CName As String
For Each ctl In Me.Controls
  If ctl.Tag = "Reqd" Then
     If Nz(ctl, "") = "" Then
       CName = ctl.Controls(0).Caption
       MsgBox "Following field is required: " & vbCrLf & vbCrLf & CName
       Cancel = True
       ctl.SetFocus
       Exit Sub
     End If
   End If
Next ctl

    
    On Error GoTo 0
    Exit Sub

Form_BeforeUpdate_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_BeforeUpdate, line " & Erl & "."

End Sub

Then in the properties of all the Controls listed I set the Tab Property to Reqd

See if this suits
View attachment AFR System 21.1.accdb.zip
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Thanks Mike for the help.


I tried your updated version but when clicking on the Preview button without any entries it goes to debug:


Run-time error '3201':


No current record.
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:52
Joined
Jan 23, 2006
Messages
15,379
I don't think ctl has been identified. It has been DIMmed as Control, but there is no reference to the Form on which this ctl exists.
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
I did see an issue with ctl but not sure how to fix. Hopefully someone with more experience can figure it out thanks.
 

mike60smart

Registered User.
Local time
Today, 09:52
Joined
Aug 6, 2017
Messages
1,908
Hi Billy

I added the following to your On Click Event "Print Tear Down"

Code:
Private Sub PreviewTearDown_Click()
    If Me.Dirty Then Me.Dirty = False
    If IsNull(Me![AFRNumber]) Then
 
       ' Alert the user.
       MsgBox "You must enter an AFR Number."
 
      ' Cancel the update.
      Cancel = True
      Me.AFRNumber.SetFocus
    End If
    
       
    Dim strDocName As String
    Dim strWhere As String
    strDocName = "Tear Down"
    strWhere = "AFRsID=" & Me!AFRsID
    DoCmd.OpenReport strDocName, acPreview, , strWhere
End Sub

You will need to add the same piece of code for all of the remaining Controls
View attachment AFR System 20.9.accdb.zip
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Thanks Mike I now get the message followed by:


Run-time error '3075':
Syntax error(missing operator) in query expression 'AFRsID='.
 

mike60smart

Registered User.
Local time
Today, 09:52
Joined
Aug 6, 2017
Messages
1,908
Hi

Yes you will get this error because there is no data for your Preview of the Tear Down.

Normal Process is fill in all fields and Save the Record.

Then Preview the Report.

If you miss entering data for a control then this will be picked up Validation in the Before Update of the Form.

I believe you need to simplify your process and not try to do too many things with the 1 On Click Event
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Okay then maybe there doesn't need to be code for the missing data but rather a message stating to fill in all fields before printing the report.


Basically instructions is what I mean.
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Mike,


How about keep the code but put another button for saving the record. This could flag any missing fields.


Just an idea maybe.
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
Mike,


That is a good idea but maybe the instructions could stay on the form instead of a popup.


I like the idea of the highlighting.


This seems like a simpler solution and less prone to code issues.
 

billgyrotech

Banned
Local time
Today, 03:52
Joined
Apr 18, 2013
Messages
258
I will likely go with the instructions method. I really appreciate all of the help.
 

Users who are viewing this thread

Top Bottom