Solved

Recordset query - Visual Basic

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

729 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