Frustrated with MsgBox

D

Don

When I try to program a message box with multiple buttons, i.e. YES, No or
Retry, Cancel, I can program one button to do what I want, but I can't get
the second button to do anything. I'm going to include the code that I've
tried with a message box I've created; can someone tell me what I've done
wrong?

Private Sub txtPASSWORD4_BeforeUpdate(Cancel As Integer)

If Me.txtPASSWORD4 <> DLookup("[PASSWORD]", "tblPASSWORD") Then
If MsgBox("Incorrect Password, try again!", 5 + 48 + 0, "Password
Confirm") = 4 Then
Cancel = True
Me.txtPASSWORD4.Undo
Else
If MsgBox("Incorrect Password, try again!", 5 + 48 + 0,
"Password Confirm") = 2 Then
Me.Undo
DoCmd.Close , "frmDSR", acSaveNo
End If
End If
End If

End Sub

I'd appreciate any help that I can get.
 
B

Bob Quintal

When I try to program a message box with multiple buttons, i.e.
YES, No or Retry, Cancel, I can program one button to do what I
want, but I can't get the second button to do anything. I'm going
to include the code that I've tried with a message box I've
created; can someone tell me what I've done wrong?

Private Sub txtPASSWORD4_BeforeUpdate(Cancel As Integer)

If Me.txtPASSWORD4 <> DLookup("[PASSWORD]", "tblPASSWORD")
Then
If MsgBox("Incorrect Password, try again!", 5 + 48 + 0,
"Password
Confirm") = 4 Then
Cancel = True
Me.txtPASSWORD4.Undo
Else
If MsgBox("Incorrect Password, try again!", 5 + 48 +
0,
"Password Confirm") = 2 Then
Me.Undo
DoCmd.Close , "frmDSR", acSaveNo
End If
End If
End If

End Sub

I'd appreciate any help that I can get.
You have not A messagebox, but two messageboxes.

try
Dim iResult as integer
iResult = MsgBox("Incorrect Password, try again!", _
vbRetryCancel + vbExclamation, "Password Confirm")
select Case iResult
Case vbCancel
Me.Undo
DoCmd.Close , "frmDSR", acSaveNo
Case vbRetry
Cancel = True
Me.txtPASSWORD4.Undo
Case Else
'Whatever
End select

And do everybody a favor, especially yourself, and use the
intellisense to put the VB constants for those options instead of
the dumb numbers.

and as an added note
If Me.txtPASSWORD4 <> DLookup("[PASSWORD]", "tblPASSWORD")
I think you need to pass criteria to select the user's password,
because this will always return the first entry in tblPASSWORD.
 
L

Linq Adams via AccessMonster.com

First off, when you use DLookup without the third argument (Criteria) as in

If Me.txtPASSWORD4 <> DLookup("[PASSWORD]", "tblPASSWORD")

DLookup pulls a RANDOM VALUE for [PASSWORD]from the table! If you have more
than one record in tblPASSWORD, it's a crapshoot as to whether you'll get a
match, even if the password is valid!

I would use DCount() instead. This works for me:

Private Sub txtPASSWORD4_BeforeUpdate(Cancel As Integer)

If DCount("[PASSWORD]", "tblPASSWORD", "[Password]='" & Me![txtPASSWORD4]
& "'") < 1 Then
If MsgBox("Incorrect Password, try again!", 5 + 48 + 0, "Password
Confirm ") = 4 Then
Cancel = True
Me.txtPASSWORD4.Undo
Else
Me.Undo
DoCmd.Close
End If
End If
End Sub
 
S

Stefan Hoffmann

hi Don,
When I try to program a message box with multiple buttons, i.e. YES, No or
Retry, Cancel, I can program one button to do what I want, but I can't get
the second button to do anything. I'm going to include the code that I've
tried with a message box I've created; can someone tell me what I've done
wrong?
Use the defined constants like, vbYes, vbOkOnly or vbInformation.

If Me.txtPASSWORD4 <> DLookup("[PASSWORD]", "tblPASSWORD") Then
If MsgBox("Incorrect Password, try again!", 5 + 48 + 0, "Password
Confirm") = 4 Then
4 is vbRetry, do you have a button retry?
Cancel = True
Me.txtPASSWORD4.Undo
Else
If MsgBox("Incorrect Password, try again!", 5 + 48 + 0,
"Password Confirm") = 2 Then
2 is vbCancel.

Consider using Select Case.

Dim MsgBoxResult As Long

MsgBoxResult = MsgBox("Try again?", _
vbQuestion + vbYesNo, _
"Password Confirm")

Select Case MsgBoxResult
Case Is = vbYes
'do this
Case Else
End Select


mfG
--> stefan <--
 
D

Don

My Password table can only have one password so I don't need to have a
criteria in my DLookup expression.
 
D

Don

There is only one password because there will only be one person that will
access this info in the form.

I'll take your advice about the "dumb numbers".
--
Don Rountree


Bob Quintal said:
When I try to program a message box with multiple buttons, i.e.
YES, No or Retry, Cancel, I can program one button to do what I
want, but I can't get the second button to do anything. I'm going
to include the code that I've tried with a message box I've
created; can someone tell me what I've done wrong?

Private Sub txtPASSWORD4_BeforeUpdate(Cancel As Integer)

If Me.txtPASSWORD4 <> DLookup("[PASSWORD]", "tblPASSWORD")
Then
If MsgBox("Incorrect Password, try again!", 5 + 48 + 0,
"Password
Confirm") = 4 Then
Cancel = True
Me.txtPASSWORD4.Undo
Else
If MsgBox("Incorrect Password, try again!", 5 + 48 +
0,
"Password Confirm") = 2 Then
Me.Undo
DoCmd.Close , "frmDSR", acSaveNo
End If
End If
End If

End Sub

I'd appreciate any help that I can get.
You have not A messagebox, but two messageboxes.

try
Dim iResult as integer
iResult = MsgBox("Incorrect Password, try again!", _
vbRetryCancel + vbExclamation, "Password Confirm")
select Case iResult
Case vbCancel
Me.Undo
DoCmd.Close , "frmDSR", acSaveNo
Case vbRetry
Cancel = True
Me.txtPASSWORD4.Undo
Case Else
'Whatever
End select

And do everybody a favor, especially yourself, and use the
intellisense to put the VB constants for those options instead of
the dumb numbers.

and as an added note
If Me.txtPASSWORD4 <> DLookup("[PASSWORD]", "tblPASSWORD")
I think you need to pass criteria to select the user's password,
because this will always return the first entry in tblPASSWORD.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top