Results 1 to 5 of 5
  1. #1
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136

    Not Like

    I need some help in Visual Basic. I want to display a message box if my field 'Code' does not include a 'G'. I have used the following but am getting an error message.


    If [Forms]![Enter New Receipts]![Type of Receipt] = 2 And [Forms]![Enter New Receipts]![Code] is Not Like "*G*" Then MsgBox "Do you also need to change the Donor's Code to include 'G'?"
    I have also tried using "<> *G*" but that doesn't work either.
    What is the right way to describe 'not like' in VB?

  2. #2
    Toyman is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    233
    Quote Originally Posted by Baldeagle View Post
    I need some help in Visual Basic. I want to display a message box if my field 'Code' does not include a 'G'. I have used the following but am getting an error message.
    If [Forms]![Enter New Receipts]![Type of Receipt] = 2 And [Forms]![Enter New Receipts]![Code] is Not Like "*G*" Then MsgBox "Do you also need to change the Donor's Code to include 'G'?"
    I have also tried using "<> *G*" but that doesn't work either.
    What is the right way to describe 'not like' in VB?
    Try this:
    Instr([Forms]![Enter New Receipts]![Code],"G") = 0

    'this uses the instring function to look for the postion of "G" in the field. It returns "0" if none is located.

  3. #3
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136

    Not Like

    Thanks for the suggestion. Maybe I haven't followed your coding correctly but I am getting a syntax error. The following is the full coding that I have. The first section works OK but the second doesn't.
    Private Sub List36_LostFocus()
    If [Forms]![Enter New Receipts]![Type of Receipt] = 2 And [Forms]![Enter New Receipts]![GA Dec?] = 0 Then
    MsgBox "Is there a Gift Aid Declaration? If so Tick the Gift Aid Declaration box."
    End If
    InStr([Forms]![Enter New Receipts]![Code], "G") = 0 Then MsgBox "Do you also need to change the Donor's Code to include 'G'?"
    End Sub

    As I indicated in my first post, I want the second message to appear if there is no 'G' in Code - there can be a variety of possibilities such as 'GJ' 'MGJ' or simply 'G'.
    Any further help would be appreciated.

  4. #4
    Toyman is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    233
    Quote Originally Posted by Baldeagle View Post
    Thanks for the suggestion. Maybe I haven't followed your coding correctly but I am getting a syntax error. The following is the full coding that I have. The first section works OK but the second doesn't.
    Private Sub List36_LostFocus()
    If [Forms]![Enter New Receipts]![Type of Receipt] = 2 And [Forms]![Enter New Receipts]![GA Dec?] = 0 Then
    MsgBox "Is there a Gift Aid Declaration? If so Tick the Gift Aid Declaration box."
    End If
    InStr([Forms]![Enter New Receipts]![Code], "G") = 0 Then MsgBox "Do you also need to change the Donor's Code to include 'G'?"
    End Sub

    As I indicated in my first post, I want the second message to appear if there is no 'G' in Code - there can be a variety of possibilities such as 'GJ' 'MGJ' or simply 'G'.
    Any further help would be appreciated.
    The inStr function is outside of the if/then statement

  5. #5
    Baldeagle is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Northern Ireland
    Posts
    136
    Thanks for that. I have put another 'If' at the start of the 'InStr' line and it works for me now.

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

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