MsgBox if yes then... need a hint! (1 Viewer)

vidus

Confused User
Local time
Today, 06:20
Joined
Jun 21, 2009
Messages
117
Alright, still in the learning curve here, I need a hint to finish this code up. Its not changing the fields as I want it to when ok is clicked... what did I miss?

Code:
Private Sub Command57_Click()
  
On Error GoTo errorHandler

MsgBox "This job will be deactivated from the Job Board, are you sure?", vbYesNo, "Ok?"

    If Answer = vbYes Then
     Me.[Active] = False
     Me.[Invoiced] = True
    End If

    If Answer = vbNo Then
      GoTo ExitHandler
    End If


ExitHandler:
  Exit Sub
errorHandler:
  MsgBox Err.Description

End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 06:20
Joined
Aug 30, 2003
Messages
36,124
You're never setting your variable to the result of the message box:

Dim Answer As Integer
Answer = MsgBox(...)

By the way, your second If/Then block is not necessary. Code execution would go to the exit handler anyway. If there was more code, I'd handle it with an Else in the first If/Then block.
 

vidus

Confused User
Local time
Today, 06:20
Joined
Jun 21, 2009
Messages
117
You're never setting your variable to the result of the message box:

Dim Answer As Integer
Answer = MsgBox(...)

By the way, your second If/Then block is not necessary. Code execution would go to the exit handler anyway. If there was more code, I'd handle it with an Else in the first If/Then block.

Could you break down the Dim Answer as Integer? What does this mean or do?

Also, it doesnt seem to like the Answer = line, syntax error.

Code:
On Error GoTo errorHandler

Dim Answer As Integer
Answer = MsgBox "This job will be deactivated from the Job Board, are you sure?", vbYesNo, "Ok?"

    If Answer = vbYes Then
     Me.[Active] = False
     Me.[Invoiced] = True
    End If

    If Answer = vbNo Then
      GoTo ExitHandler
    End If


ExitHandler:
  Exit Sub
errorHandler:
  MsgBox Err.Description
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 06:20
Joined
Aug 30, 2003
Messages
36,124
Any time you're going to use a variable (which "Answer" is in your context), you should declare it. If you look up "Dim" in VBA help you'll get more info. Most of us put any declarations at the beginning of the sub. Most of us also recommend having Option Explicit at the top of every module, which forces you to declare your variables. More info on that in VBA help as well.

When you're capturing the result, you need the parentheses that I included in my example.
 

vidus

Confused User
Local time
Today, 06:20
Joined
Jun 21, 2009
Messages
117
Any time you're going to use a variable (which "Answer" is in your context), you should declare it. If you look up "Dim" in VBA help you'll get more info. Most of us put any declarations at the beginning of the sub. Most of us also recommend having Option Explicit at the top of every module, which forces you to declare your variables. More info on that in VBA help as well.

When you're capturing the result, you need the parentheses that I included in my example.

Thanks. I think a good book on VBA is in my near future. :D
 

ghudson

Registered User.
Local time
Today, 09:20
Joined
Jun 8, 2002
Messages
6,195
This is a cleaner way to do what you want...

Code:
Private Sub btnDeactivateJob_Click()
On Error GoTo Err_btnDeactivateJob_Click

     If MsgBox("Do you want to deactivate this job from the Job Board?", vbQuestion + vbYesNo, "Deactivea Job") = vbYes Then
          Me.[Active] = False
          Me.[Invoiced] = True
     Else
          Exit Sub
     End If

Exit_btnDeactivateJob_Click:
    Exit Sub

Err_btnDeactivateJob_Click:
    MsgBox Err.Number & " - " & Err.Description, vbCritical, "btnDeactivateJob_Click()"
    Resume Exit_btnDeactivateJob_Click
    
End Sub
 

Users who are viewing this thread

Top Bottom