Results 1 to 7 of 7
  1. #1
    Only4Access is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    38

    Arrow Nested IF with INSERT INTO

    Hi Forum,



    I have an unbound form with multiple textbox. I am trying to force users to fill all required field, then INSERT to a table.

    Conditions are,


    Dim arrAcct As Variant
    Dim i As Integer

    arrAcct = Split(Me.TxtAccounts, vbCrLf)

    If IsNull (Me.Text1) Then
    msgbox "Text1 is empty"
    Me.Text1.SetFocus

    ElseIf IsNull (Me.Text2) Then
    msgbox "Text2 is empty"
    Me.Text2.SetFocus

    ElseIf IsNull (Me.Text3) Then
    msgbox "Text3 is empty"
    Me.Text3.SetFocus

    For i = 0 To UBound(arrAcct)

    CurrentDb.Execute "INSERT INTO tblAccounts (TransAccount) VALUES ('" & arrAcct & "')"

    Next

    End If

    But this is giving an error. If all fields are NOT filled out do not Execute INSERT. How can i use IF here properly.

    Thank you for your valuable time.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What's the error? Does this help?

    http://www.baldyweb.com/ImmediateWindow.htm

    for one thing, you're not referring to the array value, like arrAcct(i)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Oh, and the loop should be in the Else clause.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    First, your End If should be after the last ElseIf.... not at the end of the entire code.

    Second, you should Exit Sub in the conditions where things are not as you wish them to be.

    Third, you shouldn't test for IsNull, it doesn't properly cover the possibilities.
    There are several ways to solve this, I prefer something along the lines of
    If Len("" & Me.ControlName.Value)=0 Then

  5. #5
    Only4Access is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    38
    Thank you Paul for your reply. In my real code, yes there is (i). That was typo.

    Isaac Pisors,

    First, your End If should be after the last ElseIf.... not at the end of the entire code.

    Second, you should Exit Sub in the conditions where things are not as you wish them to be.
    Could you please help me to revise the code?

  6. #6
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    Quote Originally Posted by Only4Access View Post
    Isaac Pisors,

    Could you please help me to revise the code?
    I was kind of hoping you'd give it a shot yourself ... taking my very specific suggestions and implementing them. Try it out, let us know how it goes!

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    You can just add Else before the For... line. ipisors suggestion is simply another way to go.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  2. Having problems with nested iif
    By LSHULSTER in forum Access
    Replies: 7
    Last Post: 03-23-2012, 02:27 PM
  3. Nested IIF
    By Oldie in forum Queries
    Replies: 1
    Last Post: 02-17-2012, 06:04 AM
  4. Nested Join Help?
    By sab in forum Access
    Replies: 1
    Last Post: 12-16-2011, 05:08 PM
  5. Nested IIf Statement
    By traquino98 in forum Queries
    Replies: 5
    Last Post: 06-11-2011, 10:56 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