Results 1 to 6 of 6
  1. #1
    Raveen1609 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    53

    Writing code for finding Null value field

    Dears. I have a simple problem, which I couldnt solve. I am a basic Access VBA user.



    On click event of the Yes/No field checkedData

    If my datefield1 is empty, I want message "ok", else
    I want to check, either Datefield2 or Stringfiled must be filled. If both are empty, I must get message "Select" otherwise Message "Ok"
    I try the following code, but it is not working. I am making some mistake not sure.

    Kindly help.

    Private Sub CheckedData_Click()
    If Me.Datefiled1 <> "" Then
    If ([Datefield2] & "") = "" And (Stringfield & "") = "" Then
    MsgBox "Select"

    Else

    MsgBox "ok"
    End If
    End If
    End Sub

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    "" is an empty string. It is not Null. You cannot compare anything to Null as it basically means "unknown". So Null cannot be = or < or > than anything and the reverse is true. You can test if something is null though.

    If IsNull(Me.Datefiled1) Then <--- it is either true or it is false
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Raveen1609 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    53
    Thanks very much.

    I have modified the code as below, the False part seems to be working, I am getting "Select" message, whereas the true part is not working. I am not getting "ok" message.


    Private Sub CheckedData_Click()
    If Me.Datefiled1 <> "" Then
    If IsNull(Me.Me.Datefiled2) And IsNull(Me.Stringfield) Then


    MsgBox "Select"

    Else
    MsgBox "ok"


    End If
    End If
    End Sub

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    If my datefield1 is empty, I want message "ok", else
    I want to check, either Datefield2 or Stringfiled must be filled. If both are empty, I must get message "Select" otherwise Message "Ok"
    You have "ok" and Ok" in your post. I modified Ok to Okay just to show the message false part of
    If Datefield2 or Stringfield is not NULL

    Try this
    Code:
    Private Sub CheckedData_Click()
    If Me.Datefiled1 <> "" Then
            If IsNull(Me.Me.Datefiled2) And IsNull(Me.Stringfield) Then
              MsgBox "Select"  ' Datefiled2 and Stringfield are both NULL
            Else
              MsgBox "Okay"    ' Datefield2 or Stringfiled is not NULL
            End If
    Else 
         MsgBox "ok"           ' Datefiled1 is Empty
    End If
    End Sub
    Hope I've understood your issue/logic.
    Last edited by orange; 12-12-2020 at 07:06 AM. Reason: corrected

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Shoudn't the first check also look for Not Null instead of <>""?
    Code:
    Private Sub CheckedData_Click()
    If Not IsNull(Me.Datefiled10 Then   ' <> "" Then
            If IsNull(Me.Me.Datefiled2) And IsNull(Me.Stringfield) Then
              MsgBox "Select"  ' Datefiled2 and Stringfield are both NULL
            Else
              MsgBox "Okay"    ' Datefield2 or Stringfiled is not NULL
            End If
    Else 
         MsgBox "ok"           ' Datefiled1 is Empty
    End If
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    Raveen1609 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    53
    Thanks very much. Now the code is working, but!!

    I have few moe codes below the Else MsgBox "ok" for the first IF condition is false. I want the same codes to be executed, if the second IF condition is false also. So currently I am copying the same long codes in this location. Is it possible to do it with a single Else statement to execute, if the conditions are false in both If statements

    In this case I copied the same codes again below the Else and MsgBox "Okay"

    Shoudn't the first check also look for Not Null instead of <>"" yes accept, will change. Thanks

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

Similar Threads

  1. Replies: 11
    Last Post: 10-16-2019, 07:33 AM
  2. Help writing code please!
    By kieranharrison in forum Forms
    Replies: 6
    Last Post: 06-23-2019, 09:42 PM
  3. Code writing
    By Obidombie in forum Forms
    Replies: 1
    Last Post: 02-11-2016, 04:11 AM
  4. Finding the Max Date and Null Values if Null
    By SpdRacerX in forum Queries
    Replies: 1
    Last Post: 02-03-2012, 06:29 AM
  5. Replies: 2
    Last Post: 06-20-2010, 06:54 PM

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