Results 1 to 4 of 4
  1. #1
    ismith is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Posts
    39

    If Statement in form

    Hi there.

    Im trying to use some if statements to validate my text boxes.

    This is what ive got so far.
    What it does atm is just looks at the quantity field and just checks to see if it has numbers in, if i put a number in it adds to database with all the fields blank when its not meant to happen..

    Can any1 tell me how to solve this please...


    Set database = CurrentDb

    If txtDescription.Value = "" Then
    MsgBox "Please select a Description", vbExclamation, "Cannot Save"
    ElseIf cbCategory.Value = "" Then
    MsgBox "Please select a Category", vbExclamation, "Cannot Save"
    ElseIf txtSize.Value = "" Then
    MsgBox "Please enter a Size", vbExclamation, "Cannot Save"
    ElseIf txtQuantity.Value = "" Then
    MsgBox "Please enter a Quantity", vbExclamation, "Cannot Save"
    ElseIf Not IsNumeric(txtQuantity.Value) Then
    MsgBox "Quantity can only contain numbers", vbExclamation, "Cannot Save"
    ElseIf txtPrice.Value = "" Then
    MsgBox "Please enter a Price", vbExclamation, "Cannot Save"
    ElseIf MsgBox("Save Product Details?", vbOKCancel + vbQuestion, "Confirmation") = vbOK Then
    'get a reference to the record set in the main form
    Set addprod = database.OpenRecordset("productQuery")



    addprod.AddNew
    addprod!Description = txtDescription.Value
    addprod!Category = cbCategory.Value
    addprod!Size = txtSize.Value
    addprod!Quantity = txtQuantity.Value
    addprod!Price = "£" + txtPrice.Value
    addprod.Update


    End If

  2. #2
    pdebaets is offline Competent Performer
    Windows Vista Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Los Angeles
    Posts
    235
    It looks like you might be using an unbound form. This would be a lot easier with a bound form. You wouldn't need to write the update code. A lot less headaches.

    It appears that you are not checking for null values. Try changing

    If txtDescription.Value = "" Then

    ... to:

    If trim("" & txtDescription.Value) = "" Then

    ... and make similar changes to the rest of your validation code.

  3. #3
    nicknameoscar is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Apr 2011
    Location
    Earlysville, VA
    Posts
    91
    This is what I do in my code. I have no idea if it is the "correct" way or not but it works very well for me.

    If txtDescription.Value = "" Or IsNull(txtDescription) Then

  4. #4
    pdebaets is offline Competent Performer
    Windows Vista Access 2010 (version 14.0)
    Join Date
    Jul 2010
    Location
    Los Angeles
    Posts
    235
    That works too.

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

Similar Threads

  1. Update Statement in form code
    By ksmith in forum Programming
    Replies: 9
    Last Post: 11-07-2011, 12:04 PM
  2. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  3. If Statement to validate Field on a Form
    By ETCallHome in forum Forms
    Replies: 4
    Last Post: 04-05-2011, 05:22 PM
  4. Check my Select Statement in Form
    By OMGsh Y did I say Yes in forum Forms
    Replies: 12
    Last Post: 12-07-2010, 02:13 PM
  5. Passing Criteria to IN statement from Form
    By stephenaa5 in forum Queries
    Replies: 1
    Last Post: 10-09-2009, 04:02 AM

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