Required data in a form

kaisersose

Registered User.
Local time
Yesterday, 23:43
Joined
Sep 25, 2008
Messages
13
I have a form where a user has to fill out a number of fields, and then click the print button. Which will then export the data they just entered into a word file, print the word file and then go on to a new record in the form.

Now I want to make sure users fill out all fields. I want to have it so that when they click the print button, it will check that all fields are filled, and only then will it print out the word document. If they don’t have all fields filled out and they are prompted for each field they fail to fill. At the moment this is what I have:

Private Sub CmdPrint_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn’t open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn’t open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("P:\ Test.doc", , True)
With doc
.FormFields("fldDate").Result = Me!Date
.FormFields("fldInitiator").Result = Me!Initiator
.FormFields("fldDescriptionofItem").Result = Me!Descriptionofitem
.FormFields("fldArea").Result = Me!Area
.Visible = True
.Activate
DoCmd.GoToRecord , , acNewRec
appWord.Documents.Open "P:\ Test.doc"
appWord.PrintOut Background:=False
appWord.Quit
End With
Set doc = Nothing
Set appWord = Nothing
End Sub
Function PrintDoc()
Dim WordObj As Object
Set WordObj = CreateObject("Word.Application")
appWord.Documents.Open "P:\ Test.doc"
appWord.PrintOut Background:=False
appWord.Quit
Set WordObj = Nothing
End Function


Anyone have any pointers on what I need to do? (i'm only learning access atm :o)
 
At the start of the on Click event put something like;
Code:
If Isnull(Me.yourControlname) then
     MsgBox "Tell the user he must put data in a field"
     Me.yourControlname.Setfocus
     Exit Sub
End If

Put an if statement for each filed that you want to check for data.

Note: I have assumed that your default value for the filed you are testing is Null, if this is not the case you will need to change the logical test to reflect the empty state of each control.
 
cheers

I've added this

In my case initiator is a drop down list with names. So this filed would be blank until the user selects a name from a drop down list.

In this case what would I need to do here if the value isnt Null?

If IsNull(Me.Initiator) Then
MsgBox "Tell the user he must put data in a field"
Me.Initiator.SetFocus
Exit Sub
End If


When he set the default values to 0 with the code above, once i select a value in the first drop down list and still leave the other fields blank (0), it will go ahead and print the file and move to a new record (I want it to keep prompting me till all fields are filled)
 
Last edited:
Try;

Code:
If Me.Initiator=0 Then
MsgBox "Tell the user he must put data in a field"
Me.Initiator.SetFocus
Exit Sub
End If
 
thanks again, nearly there now

It works fine with the code at the bottom when the defualt value is 0. However I want the default value to be blank, and so if the field is blank the user will be prompted. I've tried

If Me.Date = "" Then
MsgBox "Date is a required field"
Me.Combo35.SetFocus
Exit Sub
End If

But it just goes skips this and goes on to the second field (I still have the second field having a default value of 0 and using the If Me.Area = 0 Then). Any idea what I should be doing so that it will prompt me when fields are blank?





Private Sub CmdPrint_Click()
If Me.Date = 0 Then
MsgBox "Date is a required field"
Me.Combo35.SetFocus
Exit Sub
End If
If Me.Descriptionofitem = 0 Then
MsgBox "Description of item is a required field"
Me.Text58.SetFocus
Exit Sub
End If
If Me.Area = 0 Then
MsgBox "Area is a required field"
Me.Combo39.SetFocus
Exit Sub
End If
If Me.Initiator = 0 Then
MsgBox "Initiator is a required field"
Me.Combo33.SetFocus
Exit Sub
End If
'Print customer slip for current customer.
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn’t open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn’t open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("P:\Test.doc", , True)
With doc
.FormFields("fldDate").Result = Me!Date
.FormFields("fldInitiator").Result = Me!Initiator
.FormFields("fldDescriptionofItem").Result = Me!Descriptionofitem
.FormFields("fldArea").Result = Me!Area
.Visible = True
.Activate
DoCmd.GoToRecord , , acNewRec
appWord.Documents.Open "P:\Test.doc"
appWord.PrintOut Background:=False
appWord.Quit
End With
Set doc = Nothing
Set appWord = Nothing
End Sub
Function PrintDoc()
Dim WordObj As Object
Set WordObj = CreateObject("Word.Application")
appWord.Documents.Open "P:\Test.doc"
appWord.PrintOut Background:=False
appWord.Quit
Set WordObj = Nothing
End Function
 
Put a break point in your code at If Me.Date = "" Then then run the code. When the code stops hold your cursor over the Me.Date a small window should pop up (similar to a tool tips window) this should tell you the value that Me.Date is holding. You will then know what to test for. I suspect you will need to test for Isnull(Me.Date). But do the test and see what the result is.
 
it says Me.Date = Null

but if i try this

If IsNull(Me.Date) Then
MsgBox "Tell the user he must put data in a field"
Me.Initiator.SetFocus
Exit Sub
End If

and have the default value blank then it just ignores this field and doesnt prompt me to enter data.
 
I wonder if this might be one of those cases where using a reserved word (Date) as an object name has undesired consequences... Is it too late to change the field/control named 'Date' to something else?
 
I've just tried the code in a quick DB and it seemed to work OK. However you are correct Mike it's not good practice to use reserved words as object names.
 
I removed the date field altogether, in the design view of the table I set the default value of each item to "", this seems to work fine for all the items apart from the last one, which it just ignores.

Theres something strange with this last field, I have it called descriptionofitem, it's no different to the way the other fields are set up in the table.

But when I'm entering the code for the print button, theres no such thing as me.descriptionofitem, for whatever reason it's not showing up. me.area etc show up fine. Spelling is fine. What would cause something like this to happen?
 
Once again I'd suggest putting a break point in the code at that point and checking what value the control is actually holding.

It might also be worth checking in the Other tab of the properties box that you do have the correct Name for that control.
 

Users who are viewing this thread

Back
Top Bottom