Results 1 to 2 of 2
  1. #1
    Swh86 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    1

    Msgbox

    I am trying to make a code, where a Msgbox shoud appear if a certain statement is true (to values in a variable is not the same). And afterwards the replay to yes and no should also have a function. I can´t get the code to work. I does show the msgbox when I write this code, but it also shows it when the first statement is not true (when the two values are equal to each other), where I just want it to move on to the next cell (called p_navn). The code is as follows:

    Private Sub p_center_LostFocus()
    Dim Msg, style, title, Response
    style = vbYesNo + vbInformation + vbDefaultButton1
    title = "Advarsel"


    Msg = "Værdien stemmer ikke overens med første indtastning" & vbNewLine & vbNewLine & _
    "Gammel indtastning = " & Me.center.Value & vbNewLine & "Ny indtastning = " & Me.p_center.Value & vbNewLine & vbNewLine & _
    "Vil du beholde den nye indtastede værdi?" & vbNewLine & vbNewLine & _
    "(Hvis svaret er nej, vil værdien blive ændret til den oprindelige indtastede værdi)"
    Response = MsgBox(Msg, style, title)
    If Me.p_center.Value = Me.center.Value Then
    Exit Sub
    ElseIf Me.p_center.Value <> Me.center.Value Then
    MsgBox Msg, style, title
    If Response = vbYes Then
    Me.p_center = Me.p_center.Value
    Else
    If Response = vbNo Then
    Me.p_center = Me.center.Value

    End If
    End If
    End If
    End Sub

    Hope somebody can help me with this.

    Sincerely, swh86

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Please post code between CODE tags to retain indentation and readability.

    You need to move the 'Response = MsgBox()' line where you have the 'MsgBox Msg, style, title' line.

    MsgBox has two structures - one with parens and one without. Use of parens makes it a function that returns a value. Without the parens it's just a popup message and the only response by user is to close it regardless of which buttons are displayed.

    Consider this:
    Code:
     If Me.p_center <> Me.center Then
        If MsgBox(Msg, style, title) = vbNo Then
            Me.p_center = Me.center
        End If
     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.

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

Similar Threads

  1. vba MsgBox for some but not all
    By BusDriver3 in forum Macros
    Replies: 18
    Last Post: 12-15-2016, 09:33 PM
  2. SetFocus after MsgBox
    By NISMOJim in forum Programming
    Replies: 18
    Last Post: 12-12-2012, 08:44 PM
  3. Yes No msgbox
    By imintrouble in forum Access
    Replies: 3
    Last Post: 10-14-2011, 02:24 PM
  4. Help with vbYesNo msgbox
    By focosi in forum Access
    Replies: 10
    Last Post: 08-13-2011, 01:48 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