Results 1 to 4 of 4
  1. #1
    majidrazlighi is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    May 2016
    Posts
    2

    problem in code for if function

    in my code when its run the first if itsnot checking


    plz help me

    Option Compare Database


    Private Sub Command38_Click()
    Forms!nabod!codegosfand.SetFocus
    If Forms!nabod!codegosfand.Text = Null Then
    MsgBox ("کد گوسفند مورد نظر را وارد نمائيد")
    Else
    Forms!nabod!tarikhtavalod.SetFocus
    If DLookup("[tarikhtavalod]", "[gosfandha]", "[code]= Forms!nabod!codegosfand") = Null Then
    tarikhtavalod.Text = DLookup("[tarikhtavalod]", "[gosfandha]", "[code]= Forms!nabod!codegosfand")
    Else
    Forms!nabod!kholos.SetFocus
    kholos.Text = DLookup("[kholos]", "[gosfandha]", "[code]= Forms!nabod!codegosfand")
    Forms!nabod!codepedar.SetFocus
    If DLookup("[codepedar]", "[gosfandha]", "[code]= Forms!nabod!codegosfand") = Null Then
    codepedar.Text = ""
    Else
    codepedar.Text = DLookup("[codepedar]", "[gosfandha]", "[code]= Forms!nabod!codegosfand")
    Forms!nabod!codemadar.SetFocus
    If DLookup("[codemadar]", "[gosfandha]", "[code]= Forms!nabod!codegosfand") = Null Then
    codemadar.Text = ""
    Else
    codemadar.Text = DLookup("[codemadar]", "[gosfandha]", "[code]= Forms!nabod!codegosfand")
    End If
    End If
    End If
    End If
    End Sub

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Try:
    If Forms!nabod!codegosfand = Null Then
    Or
    If IsNull(Forms!nabod!codegosfand) Then
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    Bob's second suggestion is the way to go; you cannot use the

    If WhatEverControl = Null

    construct in VBA code...you have to use the IsNull() function.

    Also notice that Bob dropped the .Text from his examples. In Access VBA, the .Text is seldom used, as referencing this requires that the Control in question has Focus at that time. You need to use the .Value property, instead, and since Value is the Default Property for Controls such as Textboxes, Comboboxes, etc., in VBA code you can simply omit it. In other words, in code:

    If IsNull(Forms!nabod!codegosfand.Value) Then

    and

    If IsNull(Forms!nabod!codegosfand) Then

    mean the same thing.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Also, this line
    Code:
    DLookup("[tarikhtavalod]", "[gosfandha]", "code = Forms!nabod!codegosfand") = Null Then
    is saying: lookup the field "[tarikhtavalod]" in table/query "[gosfandha]" where the field "Code" is equal to the string "Forms!nabod!codegosfand". I don't think the code field has anything close to that string.

    You have a value in a control that you want to search for, so you have to concatenate the value from that control to the criteria.
    If the field "code" is a text type field, delimiters are required.
    Code:
    If DLookup("[tarikhtavalod]", "[gosfandha]", "code" = '" & Forms!nabod!codegosfand & "'") = Null Then
    If the field "code" is a number type field, NO delimiters are required.
    Code:
    If DLookup("[tarikhtavalod]", "[gosfandha]", "code = " & Forms!nabod!codegosfand ) = Null Then


    Then there is the NULL issue. As Linq said, you have to use the IsNull() function.
    Now the line for a text type field looks like
    Code:
    If IsNull(DLookup("[tarikhtavalod]", "[gosfandha]", "code = '" & Forms!nabod!codegosfand & "'")) Then
    Expanded, the criteria clause looks like "code = ' " & Forms!nabod!codegosfand & " ' "


    And a number type field looks like
    Code:
    If IsNull(DLookup("[tarikhtavalod]", "[gosfandha]", "code = " & Forms!nabod!codegosfand )) Then

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

Similar Threads

  1. The problem with the function sum()
    By end in forum Access
    Replies: 2
    Last Post: 01-31-2016, 03:41 PM
  2. VBA Function Code
    By Dano60 in forum Access
    Replies: 17
    Last Post: 01-04-2016, 02:41 PM
  3. VBA If Then function - problem in code
    By Knelou in forum Access
    Replies: 63
    Last Post: 09-25-2013, 10:49 PM
  4. Problem with IIF function
    By Hulk in forum Forms
    Replies: 3
    Last Post: 03-20-2011, 12:59 PM
  5. VBA Function problem
    By smikkelsen in forum Programming
    Replies: 5
    Last Post: 07-16-2010, 07:46 AM

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