Solved

Recordset query - Visual Basic

Posted on 2011-03-08
3
236 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now