Results 1 to 10 of 10
  1. #1
    GinaFlan is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2014
    Location
    Texas
    Posts
    68

    Question Form-CmdAdd & If IsNull msg bx (add button still adds a record with an empty fields to my table)

    Help!!!! I have searched and searched. I am building a form for work for our different departments. When testing it. I hit the add button. A message box pops up Please add Fiscal Year! But the record is still added to the table with the Fiscal year missing. The only way the form recognizes the Fiscal year is missing is if I put the code under CmdAdd_Click(). I have read about putting the code in before update and such. Maybe I need to remove all of the if thens out of CmdAdd and then test placing the if then codes somewhere else. I really need help, no one else around here is very familiar to Access I am so close to finishing this form if I could just get this part to work.

    Private Sub CmdAdd_Click()
    'Add data to form
    CurrentDb.Execute "INSERT INTO Scorecards (ForTheMonth,FiscalYear,Groups,Contact,Goals,Bench marks,Results,Comments)" & _
    "VALUES('" & Me.ComboMonth & "','" & Me.ComboFiscal & "','" & Me.ComboDepartment & "','" & Me.txtContact & "','" & Me.ComboGoals & "','" & Me.txtBenchmarks & "','" & Me.TxtActualBenchmark & "','" & Me.txtComments & "');"
    'message box


    If IsNull(Me.ComboFiscal) Then ' No business unit
    MsgBox ("Please add Fiscal Year!"), vbOKCancel ' Tell user
    Me.ComboFiscal.SetFocus ' Focus the control
    Exit Sub ' Exit the method
    End If ' End the IsNull test
    If IsNull(Me.txtContact) Then ' No business unit
    MsgBox ("Please add Contact!"), vbOKCancel ' Tell user
    Me.txtContact.SetFocus ' Focus the control
    Exit Sub ' Exit the method
    End If ' End the IsNull test

    If IsNull(Me.TxtActualBenchmark) Then ' No business unit
    MsgBox ("Please add Results!"), vbOKCancel
    Me.TxtActualBenchmark.Undo ' Undo
    Me.TxtActualBenchmark.SetFocus ' Focus the control
    Exit Sub ' Exit the method
    End If ' End the IsNull test

    If IsNull(Me.ComboGoals) Then ' No business unit
    MsgBox ("Please add Goals!"), vbOKCancel ' Tell user
    Me.ComboGoals.SetFocus ' Focus the control
    Exit Sub ' Exit the method
    End If ' End the IsNull test









    If Me.ComboMonth = "January" And TxtActualBenchmark.Value = "N/A" Then
    MsgBox "Sorry but value cannot be N/A for the selected month"
    Cancel = True
    End If
    If Me.ComboMonth = "February" And TxtActualBenchmark.Value = "N/A" Then
    MsgBox "Sorry but value cannot be N/A for the selected month"
    Cancel = True
    End If
    If Me.ComboMonth = "April" And TxtActualBenchmark.Value = "N/A" Then
    MsgBox "Sorry but value cannot be N/A for the selected month"
    Cancel = True
    End If
    If Me.ComboMonth = "May" And TxtActualBenchmark.Value = "N/A" Then
    MsgBox "Sorry but value cannot be N/A for the selected month"
    Cancel = True
    End If
    If Me.ComboMonth = "July" And TxtActualBenchmark.Value = "N/A" Then
    MsgBox "Sorry but value cannot be N/A for the selected month"
    Cancel = True
    End If
    If Me.ComboMonth = "August" And TxtActualBenchmark.Value = "N/A" Then
    MsgBox "Sorry but value cannot be N/A for the selected month"
    Cancel = True
    End If
    If Me.ComboMonth = "October" And TxtActualBenchmark.Value = "N/A" Then
    MsgBox "Sorry but value cannot be N/A for the selected month"
    Cancel = True
    End If
    If Me.ComboMonth = "November" And TxtActualBenchmark.Value = "N/A" Then
    MsgBox "Sorry but value cannot be N/A for the selected month"
    Cancel = True
    End If


    If IsNumeric(Me.TxtActualBenchmark) = "False" _
    And txtBenchmarks.Value = "No negatives" Then
    Cancel = True
    Me.TxtActualBenchmark.Undo
    MsgBox "Please enter a numeric value ", _
    vbInformation, "Missing Information"
    End If



    'Requery Form
    frmscorecardssub.Form.Requery
    'Clear Form


    CmdClear_Click
    End Sub

    Private Sub CmdClear_Click()

    Me.ComboGoals = ""
    Me.txtBenchmarks = ""
    Me.txtContact = ""
    Me.ComboDepartment = ""

    Me.TxtActualBenchmark = ""
    Me.txtComments = ""
    Me.ComboGoals.SetFocus

    End Sub
    Private Sub CmdClose_Click()
    DoCmd.Close
    End Sub
    Private Sub ComboDepartment_AfterUpdate()
    Me.ComboGoals.Requery
    Me.frmscorecardssub.Form.Requery
    End Sub
    Private Sub ComboDepartment_Change()
    Me.txtContact.Value = Me.ComboDepartment.Column(1)
    End Sub
    Private Sub ComboFiscal_BeforeUpdate(Cancel As Integer)
    End Sub
    Private Sub ComboGoals_Change()
    Me.txtBenchmarks.Value = Me.ComboGoals.Column(1)
    End Sub


    Private Sub Form_BeforeUpdate(Cancel As Integer)
    End Sub
    Private Sub Form_Dirty(Cancel As Integer)
    If IsNull(Me.ComboMonth) And IsNull(Me.ComboFiscal) And IsNull(Me.txtContact) And IsNull(Me.ComboGoals) And IsNull(Me.txtBenchmarks) And IsNull(Me.TxtActualBenchmark) Then
    Me.CmdAdd.Enabled = False
    Else
    Me.CmdAdd.Enabled = True
    End If
    End Sub
    Private Sub Form_Load()
    'Clear Form
    CmdClear_Click
    End Sub

    Private Sub TxtActualBenchmark_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.TxtActualBenchmark) Then ' No business unit
    MsgBox "Please add Results!"
    Me.TxtActualBenchmark.Undo ' Undo
    Me.TxtActualBenchmark.SetFocus ' Focus the control
    Exit Sub ' Exit the method
    End If ' End the IsNull test
    If IsNumeric(Me.TxtActualBenchmark) = "False" _
    And txtBenchmarks.Value = "No negatives" Then
    Cancel = True
    Me.TxtActualBenchmark.Undo
    MsgBox "Please enter a numeric value ", _
    vbInformation, "Missing Information"
    End If
    End Sub

  2. #2
    GinaFlan is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2014
    Location
    Texas
    Posts
    68
    Click image for larger version. 

Name:	ScorecardForm.jpg 
Views:	10 
Size:	90.4 KB 
ID:	18317 My form is not bound to the table they are adding records to, the table they are adding records to is an empty table.

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -


    Your Add_Click code is adding (inserting) the new record before any of the validity checks are done. Move the INSERT statement to somewhere AFTER all the checks are done, so that the new record is created only if all the data is valid.

    Note too that Cancel = True is meaningless in the Add_Click code - an OnClick event cannot be Canceled. Use Exit Sub as you did with the other validation checks.

  4. #4
    GinaFlan is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2014
    Location
    Texas
    Posts
    68
    Yay it works so far Thank youThank you Thank you

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Another happy user! Glad I could help.

  6. #6
    GinaFlan is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2014
    Location
    Texas
    Posts
    68
    I just need one more piece of code. The underlined part is wrong. I need help correcting the code. If there is a value in Goals then results can't be blank. I need to say

    If IsNull(Me.TxtActualBenchmark) And (Me.ComboGoals) Is not Null Then
    MsgBox ("Please add Results!"), vbOKCancel
    Me.TxtActualBenchmark.SetFocus
    Exit Sub
    End If

  7. #7
    GinaFlan is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2014
    Location
    Texas
    Posts
    68
    Results (TxtAcutalBenchmark) is not bound so I figure I need to base on the value in another field for access to recognize it

  8. #8
    GinaFlan is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2014
    Location
    Texas
    Posts
    68
    I got it to work with ""

    If (Me.TxtActualBenchmark.Value = "") Then ' No business unit
    MsgBox ("Please add Results!"), vbOKCancel ' Tell user
    Me.TxtActualBenchmark.SetFocus ' Focus the control
    Exit Sub ' Exit the method
    End If ' End the IsNull test

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    A couple of things:

    In VBA code

    (Me.ComboGoals) Is not Null

    should be

    Not IsNull(Me.ComboGoals)

    You also need to be aware that

    If (Me.TxtActualBenchmark.Value = "")

    is not checking to see if TxtActualBenchmark is Null, it's checking to see if it is a Zero-Length String, and Null and a ZLS are not the same! In Access, the majority of the time when a Control is 'empty,' it is Null. If you simply leave a Control 'empty' it is Null. If you enter data in a Control, then BackSpace over it, it is Null. If you Delete data from a Control, the Control is Null. As best I've been able to figure out, the only way to get a Control to contain a Zero-Length String is either to populate the Control through code with something like

    Me.ControlName = ""

    or to Import data from a program where Fields can contain ZLSs rather than Nulls.

    Despite the unlikeliness of ZLSs, most experienced Access developers use code that addresses both possibilities. There are several hacks for doing this, with most developers preferring one or the other. I use

    If Nz(Me.ControlName, "") = "" Then

    others like

    If (Me.ControlName & "") = "" Then

    and there are probably others.

    Linq ;0)>

  10. #10
    GinaFlan is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2014
    Location
    Texas
    Posts
    68
    Ok I will try the If Nz(Me.ControlName, "") = "" Then
    Thank you so much

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 05-09-2013, 05:56 AM
  2. Query adds record to table?!?
    By dantejazz in forum Queries
    Replies: 9
    Last Post: 03-11-2012, 08:58 PM
  3. Replies: 6
    Last Post: 02-21-2012, 03:34 PM
  4. Replies: 4
    Last Post: 11-20-2011, 01:08 PM
  5. Clicking on Form Background Adds New Record?
    By swimmermx in forum Forms
    Replies: 3
    Last Post: 08-03-2010, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums