Solved

Recordset query - Visual Basic

Posted on 2011-03-08
3
237 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
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

832 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