Solved

Excel Query VBA-When nothing is found-

Posted on 2006-07-11
7
218 Views
Last Modified: 2013-02-22
This is Excel VBA


I have this piece of code, when I run this- the second query

'////20 year WAVG

Currently there is no record for this result,  so I get an error

‘Either BOF or EOF  is true,  or the current record has been deleted.
Request operation requires a current record. “  

I don’t know what to put to get by this.

Any help would greatly be appreciated.


Public Sub OpenADO()
    Dim dbpath As String
    Dim Src As String
    Dim Connection As ADODB.Connection
    Dim Col As Integer
    Dim Recordset As ADODB.Recordset
    Dim A1 As Range
   
       
    'Without Password
     dbpath = "C:\Documents and Settings\n574824\Databases\SRP\SRPtwo.mdb"
   

    Set conn = New ADODB.Connection
    With conn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Open dbpath
    End With
   
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 '////30 year WAVG
'   Create RecordSet
   
    Set Recordset = New ADODB.Recordset
    With Recordset
   
'       Filter
        Src = "SELECT [Expr1] "     '<--- space added to
        Src = Src & "FROM WA "
        Src = Src & "WHERE ([LoanTerm] = '30' ) And [CompanyID] = " & TradeLimit.ComboBox1.Value
       
         .Open source:=Src, ActiveConnection:=conn

'Write the recordset
        Sheet9.Range("L10").Value = .Fields(0)
           
    End With
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'////20 year WAVG
'   Create RecordSet
   
    Set Recordset = New ADODB.Recordset
    With Recordset
   
'       Filter
        Src = "SELECT [Expr1] "     '<--- space added to
        Src = Src & "FROM WA "
        Src = Src & "WHERE ([LoanTerm] = '20' ) And [CompanyID] = " & TradeLimit.ComboBox1.Value
       
         .Open source:=Src, ActiveConnection:=conn

           
    End With

  Set Recordset = Nothing
    conn.Close
    Set conn = Nothing
End Sub
0
Comment
Question by:ca1358
[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
  • 3
  • 3
7 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 17083982
>I don’t know what to put to get by this.
In general, before you manipulate a recordset, you should test for this and handle it appropriately, using something like this...

'All code leading up to your .Open here

rs.Open source:=Src, ActiveConnection:=conn

If Not (rs.BOF and rs.EOF) then
   'The rest of your code goes here.
else
   'There were no records in your recordset.  Handle it gracefully here.
end if
0
 
LVL 37

Assisted Solution

by:Harisha M G
Harisha M G earned 250 total points
ID: 17084013
>> If Not (rs.BOF and rs.EOF) then

That should be

If Not (rs.BOF Or rs.EOF) then
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 17084027
I always thought it was if there were zero records, .BOF and .EOF would be true.

If there are more than one record, and you are starting to iterate through it, then .BOF would be true, but not .EOF
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Harisha M G
ID: 17084072
Jim, Are you talking to me ?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 17084149
Yes.  Sorry, I should have prefixed with your name or (Off-Topic).
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17084189
> I always thought it was if there were zero records, .BOF and .EOF would be true.

Yes, but .BOF And .EOF = True implies .BOF Or .EOF = True, which is a general condition.
0
 

Author Comment

by:ca1358
ID: 17084501
Thank you both!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 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