If statement

jackie77

Jackie
Local time
Today, 22:18
Joined
Jul 18, 2007
Messages
85
Hi All

I'm having a problem getting a bit of code to work :banghead: (see below)
I have done if statements loads of times but I am having a real problem getting this to run without errors, I have changed it round and round different ways but can get it done. I need an if and else statement inside another if and else statement

Would really appreciate if someone could tell me where i'm going wrong

Cheers
Jackie

Code:
[SIZE=3][FONT=Calibri] Private Sub CreateOrder_Click()[/FONT][/SIZE]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[SIZE=3][FONT=Calibri]    Dim iline As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Dim strMsg As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Dim iResponse As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]    Dim iQuote As Integer[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]    [/SIZE][/FONT]
[FONT=Calibri][SIZE=3]  [/SIZE][/FONT]
[SIZE=3][FONT=Calibri]    If Me.AccountHold = -1 Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]            strMsg = "This account is on Hold!  Do you wish to continue?" & Chr(10)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                    If iResponse = vbYes Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                         DoCmd.OpenForm "AddJob", acNormal[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                         Forms("AddJob").CustomerID = Me.CustomerID[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                DoCmd.Close acForm, "AddJobForm1"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                Exit Sub[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]             Else[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                If iResponse = vbNo Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                Exit Sub[/FONT][/SIZE]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Else[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[SIZE=3][FONT=Calibri]   If Me.AccountHold = 0 Then[/FONT][/SIZE]
[FONT=Calibri][SIZE=3]DoCmd.OpenForm "AddJob", acNormal[/SIZE][/FONT]
[SIZE=3][FONT=Calibri]                                Forms("AddJob").CustomerID = Me.CustomerID[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                DoCmd.Close acForm, "AddJobForm1"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri]                Exit Sub[/FONT][/SIZE]
[FONT=Calibri][SIZE=3] [/SIZE][/FONT]
[FONT=Calibri][SIZE=3]End If[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]End Sub[/SIZE][/FONT]
 
Not sure about this but perhaps you need:
Code:
 Private Sub CreateOrder_Click()

    Dim iline As String
    Dim strMsg As String
    Dim iResponse As String
    Dim iQuote As Integer


    If Me.AccountHold = -1 Then
      strMsg = "This account is on Hold!  Do you wish to continue?" & Chr(10)
          iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
          If iResponse = vbYes Then
                   DoCmd.OpenForm "AddJob", acNormal
                   Forms("AddJob").CustomerID = Me.CustomerID
            DoCmd.Close acForm, "AddJobForm1"
          'End If
            Exit Sub
          ElseIf iResponse = vbNo Then
            Exit Sub
          End If
    ElseIf Me.AccountHold = 0 Then
      DoCmd.OpenForm "AddJob", acNormal
                                Forms("AddJob").CustomerID = Me.CustomerID
      DoCmd.Close acForm, "AddJobForm1"
    End If
    
End Sub
 
It would be so much easier if information like..

  • What exactly is the Error you are facing?
  • Which If/Which line, is exactly causing the trouble?
  • Has debugging provided any light on the matter?
was provided along with the Code and explanation, it would be more helpful for us to help you.
 

Thanks for the quick replies

Bob, I've tried your suggestion but still getting error
"Else without an if”

Paul my apologies I should have supplied some further info, I have used Bob’s code above and the line the error is coming up on is

ElseIf Me.AccountHold = 0 Then

I just can see what/where I’m doing wrong?

Jackie
 
Maybe:
Code:
 Private Sub CreateOrder_Click()

    Dim iline As String
    Dim strMsg As String
    Dim iResponse As String
    Dim iQuote As Integer


    If Me.AccountHold = -1 Then
      strMsg = "This account is on Hold!  Do you wish to continue?" & Chr(10)
          iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
          If iResponse = vbYes Then
                   DoCmd.OpenForm "AddJob", acNormal
                   Forms("AddJob").CustomerID = Me.CustomerID
            DoCmd.Close acForm, "AddJobForm1"
          'End If
            Exit Sub
          ElseIf iResponse = vbNo Then
            Exit Sub
          End If
    Else
      If Me.AccountHold = 0 Then
        DoCmd.OpenForm "AddJob", acNormal
                                  Forms("AddJob").CustomerID = Me.CustomerID
        DoCmd.Close acForm, "AddJobForm1"
      End If
      
End Sub
 
Jackie, is there a possibility that AcoountHold can have different values other than -1 and 0?

Why have you declared iResponse as String? IMHO it is not even required..

Try..
Code:
Private Sub CreateOrder_Click()
    Dim iline As String
    Dim strMsg As String
    Dim iQuote As Integer
    
    If Me.AccountHold = -1 Then
        strMsg = "This account is on Hold!  Do you wish to continue?" & Chr(10)
        If MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
            DoCmd.OpenForm "AddJob", acNormal
            Forms("AddJob").CustomerID = Me.CustomerID
            DoCmd.Close acForm, "AddJobForm1"
            Exit Sub
        Else
            Exit Sub
        End If
    Else
        DoCmd.OpenForm "AddJob", acNormal
        Forms("AddJob").CustomerID = Me.CustomerID
        DoCmd.Close acForm, "AddJobForm1"
    End If
End Sub
 
Paul Code works great cheers many thanks for that help

Bob thanks for your help too it was most appreciated

Cheers
Jackie
 
...Why have you declared iResponse as String? IMHO it is not even required...

Well, it would be required if the Form has

Option Explicit

set at the top, as it really should have. But the fact of the matter is that

Dim iResponse As String

is incorrect! Clicking on the buttons on a Messagebox returns an Integer, not a String! You can use vbYes, as was done, but that is actually a Constant that evaluates to an Integer, so iResponse would have to be dimmed as an Integer.

Linq ;0)>
 
Linq, I agree it would be required when Option Explicit is turned on. I should have made myself clear, I tend to avoid declaring variables, if they are only used once, as above in the CODE, where it only is used in the IF statement.

So - two lines of code, allocation of memory to a variable could be avoided by incorporating the MsgBox inside the IF, IMO would be efficient.
 

Users who are viewing this thread

Back
Top Bottom