Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Will someone please Give me a Good Answer!!! I need help with code!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm trying to code my form so that if the Filenumber is empty it will not store the information in its accompanying table. I tried the code in the Filenumber property settings under Before_Update, After_Update, On_Change and (The filenumber field is a text box) it worked with only one BIG problem. It still stored the information in the table. This is what I did, I typed in Data into the textfield "Filenumber" and then I went to the next textbox. After that I closed the form completely. I have the "Add New Record" button on my form so this is what I expect the user to have to push if they want the record to be saved. If the user just exits out of form it should not save the record. I don't want access to save the information unless the "Add New" icon is pushed. I received the code below from someone but it doesn't work. When I test it out, it keeps on outputting the Msgbox when I try to click the Filenumber textbox that is on the main form. I also tried to put the code in the "Add New" On_click property but it still stored the information.



Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Forms!General!Filenumber) Then
MsgBox "Must contain info"
Forms!General!Filenumber.SetFocus
DoCmd.CancelEvent
Exit Sub
End If
End Sub
 
Try

Private Sub Form_BeforeUpdate(Cancel As Integer)

If IsNull(Forms!General!Filenumber) Then
Cancel = True
MsgBox "Must contain info"
Forms!General!Filenumber.SetFocus
DoCmd.CancelEvent
Exit Sub
End If
End Sub
 
I just put the code in and now what it does is it loops the "Must contain info" msgbox. I put the code under the Victim's "Before_Update" property setting. Victim is a subform on my main report known as General. Filenumber is a textbox on the General form. When I put in info for the Victim's Last Name, and then I click the add key, I get the "Must contain info" then when I try and click outside of the subform by going to the "Filenumber" it says "Must contain info" What is not working here?
 
sorry, I'm not awake yet......and I didn't read it correctly the first time. Look into Me.Undo.........and adding a Form Level Boolean Variable that is updated when you click the Add Button, then in the Form's Before_Update Event (and maybe even in the On Close Event just to be sure), check this value and either perform a Me.Undo or DoCmd.RunCommand acCmdSaveRecord

PaulF
 
I need to do something similar and tried the following and it will not work.
I think I might be declaring the variable in the wrong place.

I declared a variable and set it to false
Dim leaseadd as Boolean
leaseadd = false

Then:
Private Sub AddRecord_Click()
leaseadd = True
End Sub

Then:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If leaseadd = False Then
me.undo
Else
DoCmd.RunCommand acCmdSaveRecord
End Sub

I keep getting a variable not defined error either at the before update or when clicking the button so I assume I am declaring my variable incorrectly or in the wrong place. Where and how does this declaration need to be made?

Also, I have a main form with 3 subforms, will this undo the data entered on the mainform when the user clicks on a field in one of the subforms before they click the add record button? This is the part that has been killing me. Getting all the data from the main form and subforms for a single record to be added to the tables only when the user clicks the add button.

Any additional help would be greatly appreciated.

Thanks!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top