Record created from form problem?

hardrock

Registered User.
Local time
Today, 17:57
Joined
Apr 5, 2007
Messages
166
I'm learning it slowly, but getting there!
When a user clicks the save button on my form the bit of code below will check for empty fields and show a msgbox if the textboxes are empty. That all works fine, but, it appears that a record is being created in my table if i say only one field is completed. I need some help to stop this happening.
All fields must be completed before a record is created. ideas please? Cheers


Private Sub save_rec_Click()

PART.SetFocus

If IsNull(PART) Then
MsgBox " Please enter a Part"
PARTNUMBER.SetFocus

elseIf IsNull(DESCRIPTION) Then
MsgBox "Please enter a Description"
DESCRIPTION.SetFocus

ElseIf [QUANTITY] < 1 Then
MsgBox "Please enter a Quantity"
QUANTITY.SetFocus

End If

'DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

End Sub
 
Its saving because u have the save command outside of your if statments. so it is running through all the criteria. getting to the end and saving anyways.
 
Tried it

Even with the following, a new record is being created. Note the acSaveRecord is now before the endif ?

Private Sub save_rec_Click()

PART.SetFocus

If IsNull(PART) Then
MsgBox " Please enter a Part"
PARTNUMBER.SetFocus

elseIf IsNull(DESCRIPTION) Then
MsgBox "Please enter a Description"
DESCRIPTION.SetFocus

ElseIf [QUANTITY] < 1 Then
MsgBox "Please enter a Quantity"
QUANTITY.SetFocus

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

End If


End Sub
 
Code:
Private Sub save_rec_Click()
PART.SetFocus
If IsNull(PART) Then
MsgBox " Please enter a Part"
docmd.CancelEvent
PARTNUMBER.SetFocus


elseIf IsNull(DESCRIPTION) Then
MsgBox "Please enter a Description"
docmd.CancelEvent
DESCRIPTION.SetFocus

ElseIf [QUANTITY] < 1 Then
MsgBox "Please enter a Quantity"
docmd.CancelEvent
QUANTITY.SetFocus
End If

DoCmd.RunCommand acCmdSaveRecord

End Sub
 
Tried it again.. no luck

yup just tried your suggestion rainman89, exactly as you suggested. Still, it continues to write a record in the table! So i don't know whats going on
 
put this code in the beforeupdate of your form
Code:
PART.SetFocus
If IsNull(PART) Then
MsgBox " Please enter a Part"
docmd.CancelEvent
PARTNUMBER.SetFocus


elseIf IsNull(DESCRIPTION) Then
MsgBox "Please enter a Description"
docmd.CancelEvent
DESCRIPTION.SetFocus

ElseIf [QUANTITY] < 1 Then
MsgBox "Please enter a Quantity"
docmd.CancelEvent
QUANTITY.SetFocus
End If

and leave the rest in the button click
 
re:

tried that mate and still no joy, i have now singled stepped through the code to see whats going on. it appears that the problem is with the elseif isnull() statements. if it detects something in the textbox, then its ok, and goes to the next elseif, however, if nothing is in the textbox then it shows the msgbox message and then goes straight to the End if ! thus bypassing all other elseif cases!! this is why records are being created..

maybe its just me? but try it and see if u get the same thing?
 
Right. the cancelevent should make it so the record is not saving. can u post a sample?
 
re:

its weird, the cancel event gives this error: run-time error 2001 - you cancelled the previous operation, so it dont like that. The database im working on is a big beast, so cant really email it, but the form is simple. 3 text boxes, a comand button save, and the code, and a table.
 
Lets go back to the button!!

Code:
If IsNull(Part) Then
MsgBox " Please enter a Part"
DoCmd.CancelEvent
Part.SetFocus


ElseIf IsNull(Description) Then
MsgBox "Please enter a Description"
DoCmd.CancelEvent
Description.SetFocus

ElseIf [Quantity] < 1 Then
MsgBox "Please enter a Quantity"
DoCmd.CancelEvent
Quantity.SetFocus

Else

    DoCmd.RunCommand acCmdSave
   End If
Im sorry i forgot the ELSE
Forgive me its friday
 

Attachments

Still no joy!! its this blooming "Description",with no text entered in , it does the docmd.cancelevent and then straight to end if, bypassing quantity and else save record etc! - i give up !
 

Users who are viewing this thread

Back
Top Bottom