Results 1 to 10 of 10
  1. #1
    VWSpeedyB is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2015
    Location
    Work in Center Valley, PA, live near Philadelphia, PA
    Posts
    25

    Error message box appearing when no error occurs

    Hello all! I'm hoping someone can find the flaw in my code. I have a form that shows 'products to be set-up in our ERP'. I've got a search button for the reference number, called BoMNo, so I can jump to the open record. The search requires input of the last four digits of the record number (prefix is alpha-numeric with leading zeros, so is the same for all records). If the digits match an existing record, that record populates the form. If it does not match, the error box is supposed to appear asking if the user wants to retry or cancel. On error, both the cancel and retry options work as scripted.

    The issue is that the error box shows when the digits entered match an existing record. What needs to be changed in my code to eliminate this issue?

    Code:

    Private Sub GoToBoM_btn_Click()



    Dim S As String, BoMNo As String


    Userinput:
    S = InputBox("Please enter BoM Number", "BoM Number", "BoMNo")
    If S = "" Then Exit Sub
    'user hit cancel

    Me.Filter = "BoMNo Like ""*" & S & "*"""
    Me.FilterOn = True
    'user inputs last four digits of number - if record exists, that record is populated on the form


    'if the record is not found, the below is supposed to occur
    'the Else statement works as expected. However, the initial if function is appearing even when the digits entered
    'match an existing record. I've tried multiple configurations, and cannot figure out how to prevent this occurence.
    'Can anyone advise?


    If BoMNo Like "" Then

    If MsgBox("BoM Number not found. Please check the BoM Number and try again.", vbRetryCancel, "No Data") = vbRetry Then
    GoTo Userinput
    'reshow the input box

    Else

    DoCmd.Close acForm, "NPSU_Updt_frm", acSaveNo
    'close the form
    End If


    End If


    End Sub



    Thanks in advance!!

    Bryon
    (VERY Green programmer)

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Please see if this works:
    Code:
    Private Sub GoToBoM_btn_Click()
    
    
    Dim S As String, BoMNo As String
    
    
    
    
    Userinput:
    S = InputBox("Please enter BoM Number", "BoM Number", "BoMNo")
    If S = "" Then Exit Sub
    'user hit cancel
    
    
    Me.Filter = "BoMNo Like ""*" & S & "*"""
    Me.FilterOn = True
    'user inputs last four digits of number - if record exists, that record is populated on the form
    
    
    
    
    'if the record is not found, the below is supposed to occur
    'the Else statement works as expected. However, the initial if function is appearing even when the digits entered
    'match an existing record. I've tried multiple configurations, and cannot figure out how to prevent this occurence.
    'Can anyone advise?
    
    
    
    
    'If BoMNo Like "" Then
    If Me.Recordset.RecordsetCount =0 'you applied a filter, if no record found the form will be empty
        If MsgBox("BoM Number not found. Please check the BoM Number and try again.", vbRetryCancel, "No Data") = vbRetry Then
            GoTo Userinput 'reshow the input box
        Else
            DoCmd.Close acForm, "NPSU_Updt_frm", acSaveNo 'close the form
        End If
    End If
    
    
    
    
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Please use code tags (# button on posting toolbar) to maintain indentation and code readability. Hard to decipher that, especially with all the notes in it.
    If ... Then ... End If isn't a function - it's referred to as an If block. Without knowing what BoMno is it's hard to say but your description of the behaviour suggests that it contains an empty string even if the record is found.
    I think a better approach might be to use the NoMatch function on a recordset.
    EDIT- forgot to say that stepping through your code is troubleshooting 101. Since the IF depends on the value of a variable here, you would step through and validate the values of your variables.
    https://learn.microsoft.com/en-us/of...h-property-dao
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    VWSpeedyB is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2015
    Location
    Work in Center Valley, PA, live near Philadelphia, PA
    Posts
    25
    Sorry for the difficulties, Micron. When I copied and pasted my code, it was indented as it shows on my screen. I'll try to double check if I post in the future. Thanks for the tip about the recordset and noMatch.

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Have you tried my updated code in post #2?
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    VWSpeedyB is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2015
    Location
    Work in Center Valley, PA, live near Philadelphia, PA
    Posts
    25
    Hi Vlad -

    I've been in and out of meetings.

    I actually have a question - do I remove my " If BoM Like "" Then " and replace with the Recordset line, or use them in conjunction with each other?

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I commented out the first line as it is not the proper syntax, the field will never be an empty string if the filter returns no records.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    VWSpeedyB is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2015
    Location
    Work in Center Valley, PA, live near Philadelphia, PA
    Posts
    25
    Ok, I'll give it a shot and let you know how it goes. Thanks again!

  9. #9
    VWSpeedyB is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2015
    Location
    Work in Center Valley, PA, live near Philadelphia, PA
    Posts
    25
    Hi Vlad - Unfortunately, I still have not had any success with this task. I truly appreciate your time and effort.

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Can you please show what you've tried? Do you get run time errors or still get the message even if the filter is successful? Can you upload a small db sample with just the required object to illustrate the issue?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 1
    Last Post: 07-26-2016, 06:34 AM
  2. Replies: 7
    Last Post: 03-17-2016, 05:53 PM
  3. Replies: 2
    Last Post: 02-18-2016, 10:28 AM
  4. Replies: 5
    Last Post: 05-15-2015, 10:49 AM
  5. After createing ACCDE and error occurs?
    By thechazm in forum Access
    Replies: 0
    Last Post: 03-11-2015, 06:02 AM

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