Results 1 to 11 of 11
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    If Statement with MsgBox Help

    Hi all,


    I am using a button to open a form to a specific set of records using the following code.

    Code:
    Private Sub CmdPrimaryFunction_Click()   
        DoCmd.OpenForm "PrimaryFunctionFrm"
        Forms!CompanyFunctionFrm!CboSelectRole.Column(0) = PrimaryRoleID
    End Sub
    I need to add before this code an If Statment with msgbox VB OK If there is not a selection in the listbox "CboSelectRole.Column(0)
    Msg = "You must select a role from the Selected Role(s) List!"

    So basically if i dont select (Click) on one of the items in the list box, it wont let me open form up! I have never done this with a listbox before!
    Thanks
    Dave

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Try:
    Code:
    Private Sub CmdPrimaryFunction_Click() 
      If Nz(Me.CboSelectRole,0) = 0 Then
        Msgbox "You must select a role from the Selected Role(s) List!"
      Else 
        DoCmd.OpenForm "PrimaryFunctionFrm"
        Forms!CompanyFunctionFrm!CboSelectRole.Column(0) = PrimaryRoleID
      End If
    End Sub
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Sweet, thanks so much...\
    Dave

  4. #4
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I spoke to early,
    This works for the message, however when i do select one of the items in the list, it doesnt recogonize it? I get the message to select....

    Code:
    Private Sub CmdFunction_Click()      
    If Nz(Me.CboSelectRole, 0) = 0 Then
        MsgBox "You must select a role from the Selected Role(s) List!"
      Else
        DoCmd.OpenForm "PrimaryFunctionFrm"
        Forms!PrimaryFunctionFrm!CboPrimaryRole = CboSelectRole.Column(0)
        Forms!PrimaryFunctionFrm.Requery
      End If
        
    End Sub

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Are you using a listbox or combobox?

    You say "selection in the listbox" but control name indicates a combobox.

    If this is an UNBOUND listbox, it will never be Null, it will have a string value and if no item is selected, an empty string.
    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.

  6. #6
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    It is an Unbound List box.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Then check for empty string. Deal with null or empty string:

    If Me.CboSelectRole & "" = "" Then
    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.

  8. #8
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I tried this, same result? I only get the message box if select or not?


    Code:
    Private Sub CmdFunction_Click()
        If Me.CboSelectRole & "" = "" Then
            MsgBox "You must select a role from the Selected Role(s) List!"
      Else
        DoCmd.OpenForm "PrimaryFunctionFrm"
        Forms!PrimaryFunctionFrm!CboPrimaryRole = CboSelectRole.Column(0)
        Forms!PrimaryFunctionFrm.Requery
      End If
        
    End Sub

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Okay, listboxes are kinda weird. I limit my use of them and prefer combobox whenever possible.

    Me.CboSelectionRole.Column(0) & "" = ""

    Now what I find is that once an item is selected, listbox will always have value. Even if I unselect items, it holds value of last item selected.

    So try:

    Me.CboSelectionRole.ItemsSelected.Count = 0

    Might change control name to lbxSelectionRole.
    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.

  10. #10
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    DBMeProfile - Copy22.zip

    Hi,
    That gave errors....
    I have attached db to this. It will open to a form, then click on Manage Industry button and I put a button on that form called Add Functions.

  11. #11
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    I found the problem....Some how it changed from SelectRole to SelectionRole so i did get it working...
    Thank you all for the time and energy in assisting me with this....
    Dave

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

Similar Threads

  1. msgbox not working
    By Fiddler in forum Modules
    Replies: 6
    Last Post: 03-13-2018, 08:46 AM
  2. Msgbox
    By Swh86 in forum Forms
    Replies: 1
    Last Post: 09-20-2017, 03:39 AM
  3. vba MsgBox for some but not all
    By BusDriver3 in forum Macros
    Replies: 18
    Last Post: 12-15-2016, 09:33 PM
  4. Yes No msgbox
    By imintrouble in forum Access
    Replies: 3
    Last Post: 10-14-2011, 02:24 PM
  5. MsgBox
    By Mtyetti in forum Forms
    Replies: 4
    Last Post: 07-27-2011, 01:51 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