yes no message box, help with code

antonyx

Arsenal Supporter
Local time
Today, 15:00
Joined
Jan 7, 2005
Messages
556
i have some code below.. i have wrote the part in bold that needs to be coded.. its just a small part..

Code:
If me.txtfkAccountTypeID.Value = 1 Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close
End If

If me.txtfkAccountTypeID.Value = 2 And me.txtfkPayByID.Value = 0 Then
Dim stLinkCriteria As String
DoCmd.RunCommand acCmdSaveRecord
stLinkCriteria = "[jobref]=" & "'" & Me![jobref] & "'"
DoCmd.Close
DoCmd.OpenForm "frmAddBookingAcct", , , stLinkCriteria
End If

If me.txtfkAccountTypeID.Value = 2 And me.txtfkPayByID.Value = 2 Then
[b]Need a message box here.. Do you wish to add Credit Card details Now?, it has a yes and no option[/b]
[b]If option is NO then[/b]
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close
[b]If option is YES Then[/b]
Dim stLinkCriteria As String
DoCmd.RunCommand acCmdSaveRecord
stLinkCriteria = "[jobref]=" & "'" & Me![jobref] & "'"
DoCmd.Close
DoCmd.OpenForm "frmAddBookingCard", , , stLinkCriteria
End If
 
I use this code for doing the same kind of thing.

You can drop the word bits out, but it should be clear to see what you need to do.


Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to print the cover?"
Style = vbYesNo + None + vbDefaultButton2
Title = "Service Users Guide"
Ctxt = 1000
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
Set objWord = CreateObject("Word.Application")
objWord.Visible = True 'True is visible
objWord.Documents.Add ("F:\service users guide\cover.doc")
objWord.PrintOut
objWord.ActiveDocument.Close (wdDoNotSaveChanges)
objWord.Quit
Set objWord = Nothing
Else ' User chose No.
End If
 
you legend.. thanks
 
gettin a few probs with the code.. its sayin error 2467.. the object is closed or doesnt exist.. on the bold line..

is there a better way to group this code..

ill also add that one of the following conditions will ALWAYS be true when the button is pressed...

Code:
Me.cbojobtype.Value = 2 And Me.cbopaidby.Value = 2
Me.cbojobtype.Value = 1 And Me.cbopaidby.Value = 1
Me.cbojobtype.Value = 2 And Me.cbopaidby.Value = 0

here is my code at the moment..


Code:
Private Sub btncontinue_Click()
If Me.cbojobtype.Value = 1 And Me.cbopaidby.Value = 1 Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close
End If

[b]If Me.cbojobtype.Value = 2 And Me.cbopaidby.Value = 0 Then[/b]
Dim stLinkCriteria As String
DoCmd.RunCommand acCmdSaveRecord
stLinkCriteria = "[jobref]=" & "'" & Me![jobref] & "'"
DoCmd.Close
DoCmd.OpenForm "frmAddBookingAcct", , , stLinkCriteria
End If

If Me.cbojobtype.Value = 2 And Me.cbopaidby.Value = 2 Then
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Do you want to add Credit Card details now?"
Style = vbYesNo + vbDefaultButton2
Title = "Credit Card Details"
Ctxt = 1000
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
Dim stnewLinkCriteria As String
DoCmd.RunCommand acCmdSaveRecord
stnewLinkCriteria = "[jobref]=" & "'" & Me![jobref] & "'"
DoCmd.Close
DoCmd.OpenForm "frmAddBookingAcct", , , stnewLinkCriteria
Else ' User chose No.
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close
End If
End If
End Sub
 
i fixed it using else if.. here is the code for formalities sake..

Code:
Private Sub btncontinue_Click()
If Me.cbojobtype.Value = 1 And Me.cbopaidby.Value = 1 Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close

ElseIf Me.cbojobtype.Value = 2 And Me.cbopaidby.Value = 0 Then
Dim stLinkCriteria As String
DoCmd.RunCommand acCmdSaveRecord
stLinkCriteria = "[jobref]=" & "'" & Me![JobRef] & "'"
DoCmd.Close
DoCmd.OpenForm "frmAddBookingAcct", , , stLinkCriteria

ElseIf Me.cbojobtype.Value = 2 And Me.cbopaidby.Value = 2 Then
Dim msg, Style, Title, Help, Ctxt, Response, MyString
msg = "Do you want to add Credit Card details now?"
Style = vbYesNo + vbDefaultButton2
Title = "Credit Card Details"
Ctxt = 1000
Response = MsgBox(msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
Dim stnewLinkCriteria As String
DoCmd.RunCommand acCmdSaveRecord
stnewLinkCriteria = "[jobref]=" & "'" & Me![JobRef] & "'"
DoCmd.Close
DoCmd.OpenForm "frmAddBookingAcct", , , stnewLinkCriteria
Else ' User chose No.
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close
End If
End If
End Sub
 
Hi Antony

Glad you got it fixed, but I thought I would add a few thoughts :)

If you indent your code it makes it easier to read and follow the logic of the 'if's'.

If you 'Dim' your variables at the top of the Sub they are easer to find and keeps the code cleaner, again makes it easer to read - especially when you come back to it a year latter and have to try to work out what is going on :)
Re-use variables rather than creating lots, so just 'stLinkCriteria' not 'stLinkCriteria' AND 'stnewLinkCriteria'

in MsgBox(msg, Style, Title, Help, Ctxt) - 'Help, Ctxt' should only be used if you have a custom help to go with the DB

Assuming that your 'If's' covered every combination:-

Every combination included 'DoCmd.RunCommand acCmdSaveRecord' so I took it out of the if's as it will run anyway and stuck it at the start.

every option closes the form so I stuck DoCmd.Close at the end

Which meant the first If was not needed so I took it out.

all this left you with a couple of uneeded variables so I took them out as well :)

I hope this is helpfull

Code:
Private Sub btncontinue_Click()
Dim stLinkCriteria As String
Dim msg, Style, Title, Response

DoCmd.RunCommand acCmdSaveRecord

If Me.cbojobtype.Value = 2 And Me.cbopaidby.Value = 0 Then
    stLinkCriteria = "[jobref]=" & "'" & Me![JobRef] & "'"
    DoCmd.OpenForm "frmAddBookingAcct", , , stLinkCriteria
ElseIf Me.cbojobtype.Value = 2 And Me.cbopaidby.Value = 2 Then
    msg = "Do you want to add Credit Card details now?"
    Style = vbYesNo + vbDefaultButton2
    Title = "Credit Card Details"
    Response = MsgBox(msg, Style, Title)
    If Response = vbYes Then ' User chose Yes.
        stLinkCriteria = "[jobref]=" & "'" & Me![JobRef] & "'"
        DoCmd.OpenForm "frmAddBookingAcct", , , stLinkCriteria
    End If
End If

DoCmd.Close
End Sub

Peter
 

Users who are viewing this thread

Back
Top Bottom