Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Recordset query - Visual Basic

Posted on 2011-03-08
3
Medium Priority
?
243 Views
Last Modified: 2012-06-27
Hi,

I have a simple form that returns a product's description and cost based on a barcode being scanned.

This for works fine when the barcode exists in the Inventory table but if the barcode doesn't exit the code is supposed to run an error trap that displays a message box.

Currently it just jumps down to the next line. The rsEnq.NoMatch doesn't seem to be catching the 'NoMatch'

Code below. Any help greatly appreciated.
Dim mydb As Database, rsEnq As Recordset
    Dim sqlBarCode As String
       
    sqlBarCode = "SELECT DISTINCTROW tblInventory.BarCode, tblInventory.Description, tblInventory.SalePrice "
    sqlBarCode = sqlBarCode & "FROM tblInventory "
    sqlBarCode = sqlBarCode & "WHERE (tblInventory.BarCode ='" & Me.txtBarCode1.Text & "');"
    
    ' Create database.
    Set mydb = DBEngine.Workspaces(0).Databases(0)

    ' Create dynaset.
    Set rsEnq = mydb.OpenRecordset(sqlBarCode, DB_OPEN_SNAPSHOT)

    If rsEnq.NoMatch Then
        MsgBox ("BarCode was not recognised. Please write down your Name and the product taken in the book provided."), 0 + 64, "Error"
        GoTo endpgm
    End If
            
    ' Populate text box controls.
    On Error Resume Next
    Me![txtDesc1].Value = rsEnq.Fields("[Description]").Value
    Me![txtCost1].Value = rsEnq.Fields("[SalePrice]").Value
    
    mydb.Close
    
endpgm:

Me.txtBarCode2.Locked = False
Me.txtBarCode2.SetFocus

Open in new window

0
Comment
Question by:itmtsn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 35075082
In DAO, NoMatch is set when a Seek or a Find doesn't find a matching record.

Test the recordset so:

   If rsEnq.EOF or RS.BOF Then
        MsgBox ("BarCode was not recognised. Please write down your Name and the product taken in the book provided."), 0 + 64, "Error"
        GoTo endpgm
    End If

   
0
 

Author Closing Comment

by:itmtsn
ID: 35075229
The RS.BOF gave me a run time 424 error but just using rsEnq.EOF worked perfectly.

Thanks very much :)
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35080720
Thanks.
That was a typo.  I intended to put

If rsEnq.EOF or rsEnq.BOF Then

as is recommended, but in practice I find that the .EOF test on its own works fine.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question