Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

vba MsgBox for some but not all

  1. #16
    Micron is online now VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    2,810
    Hmmm, lots of beer and confusion (on my part). In your case, I don't think the combo can be null after the update.


    Anyway, I thought there was only one report requiring start and end dates. FWIW, here's another take on how it might go (warning: air code to follow).
    Code:
    Private Sub cboReports_AfterUpdate()
    
    If Me.cboReports = "Personnel Arrived (Date Range)" Then
      If IsNull([StartDate]) OR IsNull([EndDate)]) Then
        Msgbox  "A start and end date is required for the selected report."
        Exit Sub
      End if
      If Not IsDate(Me.txtStartDate) OR Not IsDate(Me.txtEndDate) Then
        Msgbox "Only valid dates can be entered into the Begin and Start date fields."
        Exit Sub
      End If
    End If
    
    End Sub
    What my air code would not do (assuming it's written correctly) is ensure the end date is not before the start date. You might be surprised that I would consider that, but hey, there's always someone who will surprise you. Nor will it validate that there are valid dates or no Nulls for the other reports, but I provide it as an example of how one might want to validate date parameters.
    - "doesn't work" is no help.Error #s/text do. State what's happening.
    - Use code tags for code/sql; show where errors occur
    Make all suggested changes in copies of your database or to its objects

  2. #17
    BusDriver3 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    46
    This is what seems to work for me... Still not sure if it is clean enough...

    Code:
    Private Sub cboReports_AfterUpdate()
    If Me.cboReports Like "*(Date Range)*" And IsNull(StartDate) Then
           MsgBox ("Selecting both a Start Date and End Date is required to view this report."), vbCritical
         cboReports.SetFocus
     cboReports = ""
       Exit Sub
       End If
    If Not IsNull(cboReports) And cboReports <> "" Then
          DoCmd.OpenReport cboReports, acViewReport  ' use acViewPreview for print preview
     cboReports = ""
    Exit Sub
    End If
    End Sub

  3. #18
    ssanfu is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    6,754
    Looks pretty clean in my view.
    Minor changes in blue....

    Just one thing that was mentioned earlier:
    Code:
    Private Sub cboReports_AfterUpdate()
        If Me.cboReports Like "*(Date Range)*" And (IsNull(StartDate) Or IsNull(EndDate)) Then  '<<- need to also check end date???
            MsgBox ("Selecting both a Start Date and End Date is required to view this report."), vbCritical
            Me.cboReports.SetFocus
            Me.cboReports = ""
            Exit Sub
        End If
        '
        ' maybe check to see if end date >= start date   
        ' (ex. start date = 12/14/2016 and end date = 11/25/2016 would be bad)
        '
        If Not IsNull(cboReports) And cboReports <> "" Then
            DoCmd.OpenReport cboReports, acViewReport  ' use acViewPreview for print preview
            Me.cboReports = ""
            Exit Sub
        End If
    End Sub
    HTH
    -----
    Steve
    --------------------------------
    "Veni, Vidi, Velcro"
    (I came; I saw; I stuck around.)

  4. #19
    BusDriver3 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    46
    This works great! Thank You so much!!!!

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Msgbox-Combo
    By wickedjc in forum Modules
    Replies: 2
    Last Post: 02-16-2016, 11:02 AM
  2. VBA code for MsgBox
    By mladen273 in forum Programming
    Replies: 8
    Last Post: 01-17-2013, 09:05 PM
  3. Yes No msgbox
    By imintrouble in forum Access
    Replies: 3
    Last Post: 10-14-2011, 01:24 PM
  4. MsgBox
    By Mtyetti in forum Forms
    Replies: 4
    Last Post: 07-27-2011, 12:51 PM
  5. Msgbox with data in
    By AndycompanyZ in forum Programming
    Replies: 9
    Last Post: 06-27-2011, 06:52 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
  •  
Tech Forums: Microsoft Office Forums