Results 1 to 11 of 11
  1. #1
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53

    Question If IfElse Else - Dont understand why this isnt working

    He guys,

    I have a simple ifelse branch (see attached). It works as follows when a user sets a value through a control form and one or another or both field(s) is/are not set (in my case two fields either the first or second or both) a error message occurs (for that I use a other function) which notice that the user should add a value (field1) or activate the checkbox (field 2). So the code is working for field1, but not for field 2, but why isnt that working? The code seems to be very clear as well as clean.

    Sorry comments are in German, if one needs English I could translate it. Would be very cool if somebody could help me out.

    ---
    Private Sub Combo471_AfterUpdate()

    'Variablendeklarationen
    Dim strTitle As String
    Dim strPormpt As String
    strTitle = "Achtung!"
    strPrompt = "Sie haben blablabla angegeben. Jedoch liegt ihr angegebenes blabla (" & Me![subformB].Form![Field1] & ") in der Vergangenheit." _
    & vbCrLf & "Bitte prüfen Sie, ob dies so korrekt ist. Andernfalls Bitte ändern Sie das blabla entsprechend."


    'Prufen ob Feld blabla leer, bei gleichzeitigen blablabla 3 - 5
    If IsNull(Me![subformB].Form![Field1]) And (Me!Combo471 = "3" Or Me!Combo471 = "4" Or Me!Combo471 = "5") Then
    'Wenn dem so ist, Aufruf einer Msg.Box, extern
    Select Case MsgBox2(Title:="Achtung", _
    Prompt:="Sie haben blablabla " & Me.Combo471 & " angegeben aber kein blabla." & vbCrLf & "Bitte geben Sie ein entsprechendes blabla an", _
    Buttons:=vbButton2 + vbInformation, _
    UserButton1:="Jahr manuell eintragen", _
    UserButton2:="Aktuelles Jahr eintragen")
    Case vbButton1
    MsgBox "Ok, blabla kann manuell eingetragen werden!", vbOKOnly + vbInformation, "blabla wird so belassen"
    Case Else
    MsgBox "Aktuelles Jahr wird eingetragen!", vbOKOnly + vbInformation, "Aktuelles Jahr"
    Me![subformB].Form![Field1] = Year(Now())
    End Select
    'Falls nicht zutrifft dann pruefen,ob Hersteller ab blablabla 3 als guck gekennzeichnet ist oder nicht ob
    ElseIf IsNull(Me![subformA].Form![marr]) And (Me!Combo471 = "3" Or Me!Combo471 = "4" Or Me!Combo471 = "5") Then
    'Aufruf der selben Messagebox, wie bei anderen Routinen
    Select Case MsgBox2(Title:="Achtung", _
    Prompt:="Sie haben den blablabla" & Me.Combo471 & " angegeben aber denhossa nicht als guck gekennzeichnet" & vbCrLf & "Bitte kennzeichnen Sie den als guck.", _
    Buttons:=vbButton2 + vbInformation, _
    UserButton1:="hossa als guck kennzeichnen", _
    UserButton2:="marr manuell kennzeichnen")
    Case vbButton1
    MsgBox "hossa wird als guck gekennzeichnet!", vbOKOnly + vbInformation, "hossa guck"
    Me![subformA].Form![marr].Value = True
    Case Else
    MsgBox "Ok,hossa wird manuell als guck gekennzeichnet!", vbOKOnly + vbInformation, "hossa nicht guck"
    End Select
    'Als dritte Alternative pruefen, bei blablabla 3 als Fall, ob blabla in der Verangenheit
    ElseIf Me!Combo471 = "3" And Me![subformB].Form![Field1] < Year(Now()) Then
    Select Case MsgBox2(Title:=strTitle, _
    Prompt:=strPrompt, _
    Buttons:=vbButton3 + vbInformation, _
    UserButton1:="blabla so belassen", _
    UserButton2:="Aktuelles Jahr eintragen", _
    UserButton3:="Derzeitiges blabla löschen und neues manuell eintragen")
    Case vbButton1
    MsgBox "Ok, blabla wird so belassen", vbOKOnly + vbInformation, "blabla wird so belassen"


    Case vbButton2
    MsgBox "Aktuelles Jahr wird eingetragen!", vbOKOnly + vbInformation, "Aktuelles Jahr"
    Me![subformB].Form![Field1] = Year(Now())
    Case Else
    MsgBox "blabla wird gelöscht! Neues kann manuell eingetragen werden", vbOKOnly + vbInformation, "Löschen"
    Me![subformB].Form![Field1] = ""
    End Select
    'Trifft keiner der Situation zu, dann Sub verlassen
    Else
    Exit Sub
    End If
    End Sub

    ---

  2. #2
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53
    Does somebody knows a way to add attachments after posting? Would like to attach the code, because I just recognised that the code is not indented.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    2nd question: Click on the "Go Advanced" button. It allows you to add attachments (below the text entry area) and has more options in the tool bar. The "#" is the option that allows you to add formatted code. (It doesn't do the formatting). Or you can type "[ code ]" at the start and "[ /code ]" (without the spaces) at the end of the code.



    1st question:
    Code:
    If IsNull(Me![subformB].Form![Field1]) And (Me!Combo471 = "3" Or Me!Combo471 = "4" Or Me!Combo471 = "5") Then
    This is a text string:"3"
    This is a number: 3

    A text 3 <> a numeric 3 --> ie "3" <> 3

    If the PK for the combo box is a Long, try removing the quotes.

    Also,
    Code:
    ElseIf Me!Combo471 = "3" And Me![subformB].Form![Field1] < Year(Now()) Then
    Try removing the quotes here also.
    And "Now()" will work... since you are getting the date, I would use "Date()"; "Now()" includes the time component.

  4. #4
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53
    Quote Originally Posted by ssanfu View Post
    2nd question: Click on the "Go Advanced" button. It allows you to add attachments (below the text entry area) and has more options in the tool bar. The "#" is the option that allows you to add formatted code. (It doesn't do the formatting). Or you can type "[ code ]" at the start and "[ /code ]" (without the spaces) at the end of the code.
    Thanks, the code is attached as a script


    Quote Originally Posted by ssanfu View Post
    1st question:
    Code:
    If IsNull(Me![subformB].Form![Field1]) And (Me!Combo471 = "3" Or Me!Combo471 = "4" Or Me!Combo471 = "5") Then
    This is a text string:"3"
    This is a number: 3

    A text 3 <> a numeric 3 --> ie "3" <> 3

    If the PK for the combo box is a Long, try removing the quotes.
    Its a text field and its working with the first and thirth if statement, which checks the same field as the second. The second is still not working and I don't know why

    Quote Originally Posted by ssanfu View Post
    Also,
    Code:
    ElseIf Me!Combo471 = "3" And Me![subformB].Form![Field1] < Year(Now()) Then
    Try removing the quotes here also.
    And "Now()" will work... since you are getting the date, I would use "Date()"; "Now()" includes the time component.
    script.zip


    Ok thanks for optimization advise

  5. #5
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53
    I found the problem. It was th function IsNull(), because you cannout use it for checkboxes. Therefore you have to check for False. But there still some optimizations, which would be fine to implement.

    1. There are more than one field Form![marr]. basically its a query, which loops through data and puts out one or more fields. My question how could I implement this situation in vba, so that my code is checking every field and not just the first one?

    2. There three different situations, but one situation could follow after the other. I say it clearly, the first and the second if statement could be true at the same time. My code is just checking the first and would ignore the second, but how could code of second if statement also doable?


    Would be nice if somebody could give me some adivises, thanks mates


    Code:
    Private Sub Combo471_AfterUpdate()
    
    'Variablendeklarationen
    Dim strTitle As String
    Dim strPormpt As String
    strTitle = "Achtung!"
    strPrompt = "Sie haben blablabla angegeben. Jedoch liegt ihr angegebenes blabla (" & Me![subformB].Form![Field1] & ") in der Vergangenheit." _
    & vbCrLf & "Bitte prüfen Sie, ob dies so korrekt ist. Andernfalls Bitte ändern Sie das blabla entsprechend."
    
    
    'Prufen ob Feld blabla leer, bei gleichzeitigen blablabla 3 - 5
    If IsNull(Me![subformB].Form![Field1]) And (Me!Combo471 = "3" Or Me!Combo471 = "4" Or Me!Combo471 = "5") Then
        'Wenn dem so ist, Aufruf einer Msg.Box, extern
            Select Case MsgBox2(Title:="Achtung", _
                    Prompt:="Sie haben blablabla " & Me.Combo471 & " angegeben aber kein blabla." & vbCrLf & "Bitte geben Sie ein entsprechendes blabla an", _
                    Buttons:=vbButton2 + vbInformation, _
                    UserButton1:="Jahr manuell eintragen", _
                    UserButton2:="Aktuelles Jahr eintragen")
                Case vbButton1
                    MsgBox "Ok, blabla kann manuell eingetragen werden!", vbOKOnly + vbInformation, "blabla wird so belassen"
                Case Else
                    MsgBox "Aktuelles Jahr wird eingetragen!", vbOKOnly + vbInformation, "Aktuelles Jahr"
                    Me![subformB].Form![Field1] = Year(Date())
            End Select
    'Falls nicht zutrifft dann pruefen,ob Hersteller ab blablabla 3 als guck gekennzeichnet ist oder nicht ob
    ElseIf (Me![subformA].Form![marr]=False) And (Me!Combo471 = "3" Or Me!Combo471 = "4" Or Me!Combo471 = "5") Then
            'Aufruf der selben Messagebox, wie bei anderen Routinen
            Select Case MsgBox2(Title:="Achtung", _
                    Prompt:="Sie haben den blablabla" & Me.Combo471 & " angegeben aber denhossa nicht als guck gekennzeichnet" & vbCrLf & "Bitte kennzeichnen Sie den als guck.", _
                    Buttons:=vbButton2 + vbInformation, _
                    UserButton1:="hossa als guck kennzeichnen", _
                    UserButton2:="marr manuell kennzeichnen")
                Case vbButton1
                    MsgBox "hossa wird als guck gekennzeichnet!", vbOKOnly + vbInformation, "hossa guck"
                    Me![subformA].Form![marr].Value = True
                Case Else
                    MsgBox "Ok,hossa wird manuell als guck gekennzeichnet!", vbOKOnly + vbInformation, "hossa nicht guck"
            End Select
    'Als dritte Alternative pruefen, bei blablabla 3 als Fall, ob blabla in der Verangenheit
    ElseIf Me!Combo471 = "3" And Me![subformB].Form![Field1] < Year(Date()) Then
        Select Case MsgBox2(Title:=strTitle, _
              Prompt:=strPrompt, _
              Buttons:=vbButton3 + vbInformation, _
              UserButton1:="blabla so belassen", _
              UserButton2:="Aktuelles Jahr eintragen", _
              UserButton3:="Derzeitiges blabla löschen und neues manuell eintragen")
            Case vbButton1
                MsgBox "Ok, blabla wird so belassen", vbOKOnly + vbInformation, "blabla wird so belassen"
            Case vbButton2
                MsgBox "Aktuelles Jahr wird eingetragen!", vbOKOnly + vbInformation, "Aktuelles Jahr"
                Me![subformB].Form![Field1] = Year(Date())
            Case Else
                MsgBox "blabla wird gelöscht! Neues kann manuell eingetragen werden", vbOKOnly + vbInformation, "Löschen"
                Me![subformB].Form![Field1] = ""
        End Select
    'Trifft keiner der Situation zu, dann Sub verlassen
    Else
      Exit Sub
    End If
    End Sub

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    1)
    There are more than one field Form![marr]. basically its a query, which loops through data and puts out one or more fields. My question how could I implement this situation in vba, so that my code is checking every field and not just the first one?
    Not sure I understand this. Are you talking fields in a query or controls on a form?

    2) Use two (or more) IF() functions. Something like this:

    Code:
    Private Sub Combo471_AfterUpdate()
    
    'Variablendeklarationen
       Dim strTitle As String
       Dim strPormpt As String
       strTitle = "Achtung!"
       strPrompt = "Sie haben blablabla angegeben. Jedoch liegt ihr angegebenes blabla (" & Me![subformB].Form![Field1] & ") in der Vergangenheit." _
                   & vbCrLf & "Bitte prüfen Sie, ob dies so korrekt ist. Andernfalls Bitte ändern Sie das blabla entsprechend."
    
    
       'Prufen ob Feld blabla leer, bei gleichzeitigen blablabla 3 - 5
       If IsNull(Me![subformB].Form![Field1]) And (Me!Combo471 = "3" Or Me!Combo471 = "4" Or Me!Combo471 = "5") Then
          'Wenn dem so ist, Aufruf einer Msg.Box, extern
          Select Case MsgBox2(Title:="Achtung", _
                              Prompt:="Sie haben blablabla " & Me.Combo471 & " angegeben aber kein blabla." & vbCrLf & "Bitte geben Sie ein entsprechendes blabla an", _
                              Buttons:=vbButton2 + vbInformation, _
                              UserButton1:="Jahr manuell eintragen", _
                              UserButton2:="Aktuelles Jahr eintragen")
             Case vbButton1
                MsgBox "Ok, blabla kann manuell eingetragen werden!", vbOKOnly + vbInformation, "blabla wird so belassen"
             Case Else
                MsgBox "Aktuelles Jahr wird eingetragen!", vbOKOnly + vbInformation, "Aktuelles Jahr"
                Me![subformB].Form![Field1] = Year(Date)
          End Select
       End If
    
       'Falls nicht zutrifft dann pruefen,ob Hersteller ab blablabla 3 als guck gekennzeichnet ist oder nicht ob
       If (Me![subformA].Form![marr] = False) And (Me!Combo471 = "3" Or Me!Combo471 = "4" Or Me!Combo471 = "5") Then
          'Aufruf der selben Messagebox, wie bei anderen Routinen
          Select Case MsgBox2(Title:="Achtung", _
                              Prompt:="Sie haben den blablabla" & Me.Combo471 & " angegeben aber denhossa nicht als guck gekennzeichnet" & vbCrLf & "Bitte kennzeichnen Sie den als guck.", _
                              Buttons:=vbButton2 + vbInformation, _
                              UserButton1:="hossa als guck kennzeichnen", _
                              UserButton2:="marr manuell kennzeichnen")
             Case vbButton1
                '.
                '. more code
                '.
          End Select
       End If

  7. #7
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Hi, I just noticed...

    Right at the beginning of your code you declare:

    Dim strPormpt As String
    strTitle = "Achtung!"
    strPrompt = "Sie haben blablabla angegeben. Jedoch liegt ihr angegebenes blabla (" & Me![subformB].Form![Field1] & ") in der Vergangenheit." _
    & vbCrLf & "Bitte prüfen Sie, ob dies so korrekt ist. Andernfalls Bitte ändern Sie das blabla entsprechend."

    I haven't looked through the rest of your code, but this typo probably isn't helping!

    Kirsti

  8. #8
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53
    Quote Originally Posted by Kirsti View Post
    Dim strPormpt As String
    strTitle = "Achtung!"
    strPrompt = "Sie haben blablabla angegeben. Jedoch liegt ihr angegebenes blabla (" & Me![subformB].Form![Field1] & ") in der Vergangenheit." _
    & vbCrLf & "Bitte prüfen Sie, ob dies so korrekt ist. Andernfalls Bitte ändern Sie das blabla entsprechend."

    I haven't looked through the rest of your code, but this typo probably isn't helping!
    Which way is better, declare and write?

  9. #9
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Hi Snoopy,

    I just mean that you have
    "Dim strPormpt"

    and then "strPrompt"

    They are spelt differently... (in the Dim, you have the 'r' and the 'o' round the wrong way)

    Kirsti

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I hate it when I miss things like that.

    The top two lines of every code page should be

    Option Compare Database
    Option Explicit



    "Option Explicit" requires that variables be defined before they can be used. Helps catch errors like undeclared and/or misspelled variables. There is a setting that can be set that will add it to any new code pages; for existing code pages, you must add it.

  11. #11
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53
    Quote Originally Posted by Kirsti View Post
    Hi Snoopy,

    I just mean that you have
    "Dim strPormpt"

    and then "strPrompt"

    They are spelt differently... (in the Dim, you have the 'r' and the 'o' round the wrong way)

    Kirsti
    O thanks I did not see it




    Quote Originally Posted by ssanfu View Post
    I hate it when I miss things like that.

    The top two lines of every code page should be

    Option Compare Database
    Option Explicit



    "Option Explicit" requires that variables be defined before they can be used. Helps catch errors like undeclared and/or misspelled variables. There is a setting that can be set that will add it to any new code pages; for existing code pages, you must add it.
    Ok, done

    problem solved! Thanks guys

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

Similar Threads

  1. Can anyone see why this isnt working please?
    By shabbaranks in forum Programming
    Replies: 3
    Last Post: 12-23-2011, 03:19 AM
  2. Could someone explain why this code isnt working please?
    By shabbaranks in forum Programming
    Replies: 7
    Last Post: 10-29-2011, 09:14 AM
  3. Replies: 2
    Last Post: 10-08-2011, 10:38 PM
  4. Replies: 2
    Last Post: 07-11-2011, 05:34 AM
  5. Combo Box isnt saving
    By calisen in forum Access
    Replies: 0
    Last Post: 09-11-2008, 03:34 PM

Tags for this Thread

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