Results 1 to 6 of 6
  1. #1
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183

    Message Box help

    My message box is working great. HOWEVER, in the "Else" section, I have an OkCancel button. In testing, both buttons close the message box and move on to send the data to the URL I can't figure out how to just close the box and stop the code if the user selects the "Cancel" button.

    Code:
    Private Sub btn_iplookup_Click()
    'Check to see if IP field contains data
    If IsNull(Me.IP) Then
        'Let user know it will not perform the check without IP.
        MsgBox "No IP Address Found !" & _
               vbCrLf & "Please add IP address and check again.", vbOKOnly Or VbMsgBoxStyle.vbExclamation, _
               "IP Address Error"
    Else
        'Notify user that they will be leaving the application to check IP Geo Info
        MsgBox "NOTE: This will output to your browser", vbOKCancel Or VbMsgBoxStyle.vbExclamation, _
        "Notice"
        
        Me.btn_iplookup.HyperlinkAddress = "http://freegeoip.net/?q=" & Me.IP
        Me.btn_iplookup.Hyperlink.Follow True
        Me.btn_iplookup.HyperlinkAddress = ""
    End If
    
    End Sub


  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    you are using msgbox as a sub, you need to use it as a function

    if MsgBox("NOTE: This will output to your browser", vbOKCancel Or VbMsgBoxStyle.vbExclamation, "Notice")=vbOK then

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    The MsgBox must return a reply so you can decide. Use parens to make it a function,
    Code:
    vReply= Msgbox("message", vbOkCancel+vbExclamation , "header")
    if vReply= vbYes then
        'Run code
    Else
        Exit sub
    endif

  4. #4
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183
    Quote Originally Posted by ranman256 View Post
    The MsgBox must return a reply so you can decide. Use parens to make it a function,
    Code:
    vReply= Msgbox("message", vbOkCancel+vbExclamation , "header")
    if vReply= vbYes then
        'Run code
    Else
        Exit sub
    endif
    I was continuing my research on the issue and found some solutions. Your code made it even better. Thanks.
    Here is how I coded it.

    Code:
    Private Sub btn_iplookup_Click()
    'Check to see if IP field contains data
        If IsNull(Me.IP) Then
            'Let user know it will not perform the check without IP.
            MsgBox "No IP Address Found !" & _
               vbCrLf & "Please add IP address and check again.", vbOKOnly Or VbMsgBoxStyle.vbExclamation, _
               "IP Address Error"
        Else
            'Notify user that they will be leaving the application to check IP Geo Info
                vReply = MsgBox("NOTE: This will output to your browser", vbOKCancel Or VbMsgBoxStyle.vbExclamation, _
                "Notice")
            If vReply = vbCancel Then
                Cancel = True
                Exit Sub
            Else
                Me.btn_iplookup.HyperlinkAddress = "http://freegeoip.net/?q=" & Me.IP
                Me.btn_iplookup.Hyperlink.Follow True
                Me.btn_iplookup.HyperlinkAddress = ""
            End If
    End If
    End Sub
    Now, I discovered a new problem. If I'm NOT connected to the internet, the hyperlink action fails and causes an error. Even pops up the debugger telling me the hyperlink failed.

    Is there a way to capture this error so that it doesn't cause my user a problem?

    I guess I might have to do another message box telling people - "Hey, you're not connected to the Internet."

  5. #5
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Find out what the error number is and use:

    On Error Goto Broke

    :Broke
    If Err.Number = "ERROR NUMBER goes here" Then
    Err.Clear()
    DoSomething()
    Else
    MsgBox Err.Description ' or whatever you find appropriate '
    End If

    Code:
    Private Sub btn_iplookup_Click()
    
    On Error Goto Broke
    
    'Check to see if IP field contains data
        If IsNull(Me.IP) Then
            'Let user know it will not perform the check without IP.
            MsgBox "No IP Address Found !" & _
               vbCrLf & "Please add IP address and check again.", vbOKOnly Or VbMsgBoxStyle.vbExclamation, _
               "IP Address Error"
        Else
            'Notify user that they will be leaving the application to check IP Geo Info
                vReply = MsgBox("NOTE: This will output to your browser", vbOKCancel Or VbMsgBoxStyle.vbExclamation, _
                "Notice")
            If vReply = vbCancel Then
                Cancel = True
                Exit Sub
            Else
                Me.btn_iplookup.HyperlinkAddress = "http://freegeoip.net/?q=" & Me.IP
                Me.btn_iplookup.Hyperlink.Follow True
                Me.btn_iplookup.HyperlinkAddress = ""
            End If
    End If
    
    :Broke 
      If Err.Number = 1234567890 Then
        Err.Clear()
        DoSomething()
      Else
        MsgBox Err.Description ' or whatever you find appropriate '
      End If
    End Sub

  6. #6
    dniezby is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2013
    Posts
    183
    Quote Originally Posted by Thompyt View Post
    Find out what the error number is and use:

    On Error Goto Broke

    :Broke
    If Err.Number = "ERROR NUMBER goes here" Then
    Err.Clear()
    DoSomething()
    Else
    MsgBox Err.Description ' or whatever you find appropriate '
    End If

    Code:
    Private Sub btn_iplookup_Click()
    
    On Error Goto Broke
    
    'Check to see if IP field contains data
        If IsNull(Me.IP) Then
            'Let user know it will not perform the check without IP.
            MsgBox "No IP Address Found !" & _
               vbCrLf & "Please add IP address and check again.", vbOKOnly Or VbMsgBoxStyle.vbExclamation, _
               "IP Address Error"
        Else
            'Notify user that they will be leaving the application to check IP Geo Info
                vReply = MsgBox("NOTE: This will output to your browser", vbOKCancel Or VbMsgBoxStyle.vbExclamation, _
                "Notice")
            If vReply = vbCancel Then
                Cancel = True
                Exit Sub
            Else
                Me.btn_iplookup.HyperlinkAddress = "http://freegeoip.net/?q=" & Me.IP
                Me.btn_iplookup.Hyperlink.Follow True
                Me.btn_iplookup.HyperlinkAddress = ""
            End If
    End If
    
    :Broke 
      If Err.Number = 1234567890 Then
        Err.Clear()
        DoSomething()
      Else
        MsgBox Err.Description ' or whatever you find appropriate '
      End If
    End Sub
    Okay, so this is the error when I try to use the hyperlink without internet connectivity.
    Click image for larger version. 

Name:	error.png 
Views:	15 
Size:	21.2 KB 
ID:	27940

    I put the error code in and your code but it gives me this >> "Compile error. Expected = " and the Err.Clear() is all red.

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

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2016, 05:53 PM
  2. Replies: 2
    Last Post: 01-23-2014, 12:40 PM
  3. Replies: 15
    Last Post: 11-01-2013, 03:24 PM
  4. Replies: 9
    Last Post: 09-26-2012, 12:20 PM
  5. Message Box
    By Alex Motilal in forum Forms
    Replies: 1
    Last Post: 03-27-2012, 08:31 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