473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Open recordset problems

I am having some trouble with opening recordsets. I have used code
more or less straight from the access help. But still I am getting
some errors. I am unable to work out what exactly I am doing wrong.

1.When I try the following code it gives the error message "compile
error- type mismatch" on the last line. ( I have seen in past postings
that this error is usually because of DAO/ADO mismatch. Is the
recordsource for forms in Access 2000 by default ADO?)

Dim dbsTemp As DAO.Database
Dim rstTemp As DAO.Recordset

Set dbsTemp = OpenDatabase("C:\Documents and Settings\j\My
Documents\db1.mdb")
Set rstTemp = dbsTemp.OpenRecordset( "SELECT * FROM Table1")

With rstTemp | This is just to test whether it
.MoveLast | opens the recordset. I get a count of 6
.MoveFirst | which is the number of records in the table
MsgBox (rstTemp.RecordCount)
End With
Me.RecordSource = rstTemp -> the error appears here

2. In the following code, the record count is shown as "–1". Thias is
the same table as above, containing 6 records

Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Dim strSource As String
strSource = "SELECT * FROM Table1;"
rst.Open strSource, cnn, adOpenDynamic

If rst.EOF Then
MsgBox ("No records")
Exit Sub
Else
rst.MoveLast
rst.MoveFirst
MsgBox (rst.RecordCount)
End If
Sunil Korah
Nov 13 '05 #1
2 7739
Relying on the RecordCount property in a recordset is not good practice.

The following code is better for your purposes

Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Dim strSource As String

strSource = "SELECT Count(*) FROM Table1;"
rst.Open strSource, cnn, adOpenStatic

If not (rst.EOF AND rst.BOF) Then
MsgBox (rst.Fields(0))
Else
MsgBox ("No records")
Exit Sub
End If
--
Terry Kreft
MVP Microsoft Access
"Sunil Korah" <hb*****@indiatimes.com> wrote in message
news:72**************************@posting.google.c om...
I am having some trouble with opening recordsets. I have used code
more or less straight from the access help. But still I am getting
some errors. I am unable to work out what exactly I am doing wrong.

1.When I try the following code it gives the error message "compile
error- type mismatch" on the last line. ( I have seen in past postings
that this error is usually because of DAO/ADO mismatch. Is the
recordsource for forms in Access 2000 by default ADO?)

Dim dbsTemp As DAO.Database
Dim rstTemp As DAO.Recordset

Set dbsTemp = OpenDatabase("C:\Documents and Settings\j\My
Documents\db1.mdb")
Set rstTemp = dbsTemp.OpenRecordset( "SELECT * FROM Table1")

With rstTemp | This is just to test whether it
.MoveLast | opens the recordset. I get a count of 6
.MoveFirst | which is the number of records in the table
MsgBox (rstTemp.RecordCount)
End With
Me.RecordSource = rstTemp -> the error appears here

2. In the following code, the record count is shown as "-1". Thias is
the same table as above, containing 6 records

Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Dim strSource As String
strSource = "SELECT * FROM Table1;"
rst.Open strSource, cnn, adOpenDynamic

If rst.EOF Then
MsgBox ("No records")
Exit Sub
Else
rst.MoveLast
rst.MoveFirst
MsgBox (rst.RecordCount)
End If
Sunil Korah

Nov 13 '05 #2
As to #2, the rst.RecordCount is not a reliable way to count the records in
some ADO recordsets, is not possible in other recordsets, and should be
avoided because of speed issues in most other situations. Use Count(*)
instead:

With rst
.Source = "SELECT Count(*) FROM Table1"
.Open , cnn, adOpenForwardOnly, adLockReadOnly, adCmdText
If .Fields(0) = 0 Then
MsgBox ("No records")
Exit Sub
Else
MsgBox "Count: " & .Fields(0)

You can then close the recordset and re-open it as you want, or change the
rst.Source and rst.Requery. In either case, there is practically no
overhaead.

.Close
.Source = "SELECT * FROM Table1"
.Open , cnn, adOpenKeyset, adLockOptimistic, adCmdText

' Do recordset stuff

.Close
End If
End With
Set rst = Nothing
"Sunil Korah" <hb*****@indiatimes.com> wrote in message
news:72**************************@posting.google.c om...
I am having some trouble with opening recordsets. I have used code
more or less straight from the access help. But still I am getting
some errors. I am unable to work out what exactly I am doing wrong.

1.When I try the following code it gives the error message "compile
error- type mismatch" on the last line. ( I have seen in past postings
that this error is usually because of DAO/ADO mismatch. Is the
recordsource for forms in Access 2000 by default ADO?)

Dim dbsTemp As DAO.Database
Dim rstTemp As DAO.Recordset

Set dbsTemp = OpenDatabase("C:\Documents and Settings\j\My
Documents\db1.mdb")
Set rstTemp = dbsTemp.OpenRecordset( "SELECT * FROM Table1")

With rstTemp | This is just to test whether it
.MoveLast | opens the recordset. I get a count of 6
.MoveFirst | which is the number of records in the table
MsgBox (rstTemp.RecordCount)
End With
Me.RecordSource = rstTemp -> the error appears here

2. In the following code, the record count is shown as "-1". Thias is
the same table as above, containing 6 records

Dim rst As New ADODB.Recordset
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Dim strSource As String
strSource = "SELECT * FROM Table1;"
rst.Open strSource, cnn, adOpenDynamic

If rst.EOF Then
MsgBox ("No records")
Exit Sub
Else
rst.MoveLast
rst.MoveFirst
MsgBox (rst.RecordCount)
End If
Sunil Korah

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dalan | last post by:
I presume that using an open recordset method is the preferred method of accomplishing what I'm trying to do. Of course, if there are other options that would work, feel free to share them. I...
13
by: Seth Spearman | last post by:
Hey guys, I have the following code: '****************************************************** If Not Me.NewRecord Then Dim rs As DAO.Recordset Dim strBookmark As String Set rs =...
2
by: Colleyville Alan | last post by:
I ran into problems in an app in which I received a msg of "cannot lock the table, it is in use by another user". It turns out that I had opened a recordset with a command like set rstmyrecs =...
1
by: petersk | last post by:
Firstly I am an older person trying to teach myself to create a project and teach myself Access VBA programming along the way. I anticipate a number of problems I will need help with but here...
6
by: blue875 | last post by:
Hello helper people who are smarter than me: I have a form that needs to submit multiple queries to different tables during one Sub's execution. Some sections are as simple as: 1| With rst 2|...
6
by: ZRexRider | last post by:
I've been reading the newsgroups and most of the solutions for this message are simple and obvious but unfortunately don't solve my problem. I have an MS-Access 2002 ADP application that...
2
by: Jim M | last post by:
I rarely deal with recordsets directly with code, since I usually use Access queries, so be patient with this question. I want to open a recordset with various default variables used by my program....
23
by: PW | last post by:
Hi, I'd like to close a recordset and set the database to nothing if a recordset is open if an error has occured. Leaving a recordset open and a database open isn't a good idea, right? ...
8
by: metalheadstorm | last post by:
ok ive set up a connection between my access db ( 97) and my vb6 interface and this is what i got form_load Data6.DatabaseName = App.Path & "\cjmillers.mdb" Data6.RecordSource = "select * from...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.