Results 1 to 14 of 14
  1. #1
    MFS is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2009
    Location
    Ohio
    Posts
    235

    If Statement


    Hi everyone,
    I have a checkbox and a textbox on a form.
    I'm looking to have a message appear, when closing the form, to request the user to add data to the textbox if the checkbox has been checked, and vise versa.
    Any help would be appreciated,

    mikespeck

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Are you allowing users to close form with the X button or have you disabled it?

    When I want to control how user leaves a form I disable the X and create a button that when clicked executes the form close command but only after conditions are met. If conditions not met the user is returned to form.

    I only use VBA not macros.

    Are you saying that if textbox has entry, the checkbox must be checked? Sounding like the checkbox is not needed.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    MFS is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2009
    Location
    Ohio
    Posts
    235
    Quote Originally Posted by June7 View Post
    Are you allowing users to close form with the X button or have you disabled it?


    Are you saying that if textbox has entry, the checkbox must be checked? Sounding like the checkbox is not needed.
    The X button is disabled, yes. And the checkbox is tied to other vba in the DB.
    I am wanting to use a button to close the form but not sure how to write the VBA.
    Mike

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    In button Click event, something like:

    If (IsNull(Me.textboxname) And Me.checkboxname = vbYes) Or (Not IsNull(Me.texboxname) And Me.checkboxname = vbNo) Then
    MsgBox "something"
    Else
    DoCmd.CloseForm acForm, Me.Name
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    MFS is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2009
    Location
    Ohio
    Posts
    235
    My form name is "Passdown Entry"
    Where would I enter that into your code: DoCmd.CloseForm acForm, Me.Name
    I've tried entering it like, DoCmd.CloseForm acForm, Me.Passdown_Entry
    Also, after reviewing the vba if the checkbox is checked and there is no data in the textbox I'm looking to have a message appear requesting: "Enter data into textbox".
    If there is data in the textbox but the checkbox is not checked then I need a message saying: "Check Checkbox"
    Just a little new to VBA

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Try something like this:
    If IsNull(Me.textboxname) And Me.checkboxname = vbYes Then
    MsgBox "Enter data into textbox"
    ElseIf Not IsNull(Me.texboxname) And Me.checkboxname = vbNo Then
    MsgBox "Check checkbox"
    Else
    DoCmd.Close acForm, Me.Name
    End If

    Do not change anything on the CloseForm line.
    Name is a property of the form.
    Me is shorthand for Form_formname or Forms!formname when code behind a form needs to reference that same form.
    Me.Name is generic, don't have to be concerned with possibly misspelling name and could copy/paste this line to another form and the behavior would work for that form.
    Otherwise would have to put the form's name within quote marks, like "Passdown Entry". Does the form name have an underscore or not? See, now have to be aware of spelling, Me alias gets around that and saves keystrokes.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    MFS is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2009
    Location
    Ohio
    Posts
    235
    Here's what I get: (Attached)

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Whoops (I fixed my previous post)!
    DoCmd.Close acForm, Me.Name

    The intellisense popup should have presented the correct syntax. Just have to get in the habit of paying attention to the popups as you type.

    Or this:
    If IsNull(Me.textboxname) And Me.checkboxname = vbYes Then
    MsgBox "Enter data into textbox"
    Exit Sub
    ElseIf Not IsNull(Me.textboxname) And Me.checkboxname = vbNo Then
    Me.checkboxname = vbYes
    ElseIf IsNull(Me.textboxname) And Me.checkboxname = vbYes Then
    Me.checkboxname = vbNo
    End If
    DoCmd.Close acForm, Me.Name

    You might want to indent your code so it is easier to read. I didn't try to because am typing on the fly, not in VBA editor.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    MFS is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2009
    Location
    Ohio
    Posts
    235
    That fixed that problem but now when I click on the button nothing happens, using your original code:

  10. #10
    MFS is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2009
    Location
    Ohio
    Posts
    235
    Except closing the form

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    You are missing the Else before DoCmd

    Did the msgbox popup?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    MFS is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2009
    Location
    Ohio
    Posts
    235
    Attached is my VBA,
    And there are no message boxes.
    I do apologize for being such a pain.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    No, not a pain. I just need to get it right the first time.

    I don't use checkboxes much and always have to relearn how to use them. Instead of vbYes or vbNo, simply True or False.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    MFS is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2009
    Location
    Ohio
    Posts
    235
    That was it, I want to thank-you for all of your help. Works great

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

Similar Threads

  1. BETWEEN in a FROM Statement
    By Cyborg in forum Queries
    Replies: 6
    Last Post: 05-12-2011, 01:54 PM
  2. If Statement
    By ajolson1964 in forum Access
    Replies: 1
    Last Post: 05-11-2011, 07:51 AM
  3. need a If /then statement
    By kmiszczak in forum Access
    Replies: 1
    Last Post: 04-12-2011, 01:51 PM
  4. Help with an Iif statement please
    By 10 Gauge in forum Programming
    Replies: 4
    Last Post: 04-05-2011, 06:02 AM
  5. If Then Statement Help
    By Kapelluschsa in forum Programming
    Replies: 5
    Last Post: 08-11-2010, 09:24 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