473,399 Members | 3,656 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,399 software developers and data experts.

Dumb questions about ADODB recordsets

I'm trying to grab the fieldnames and values from a recordset, but
I'm getting errors. I have an idea what the error might come from,
but I'm not sure how to correct it.

I'm connecting to an Access db which has the table 'Users', and I
want to grab the fieldnames and values without necessarily knowing
their numbers and formats. The msgboxes are there to illustrate the
error.

To run this, you need an Access db called Users.mdb in the executable
directory, a table called Users in that db, at least 4 fields in the
table, including a field called UserName. One of the records must
have the UserName Fred. For older versions of Access, OLEDB.4.0 needs
to be changed (3.5 instead of 4.0 for Access 97 IIRC).

Public Sub GetValues
MyConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Settings
\Users.mdb;"
ConnectionString = "SELECT * FROM Users WHERE UserName='Fred';"
Dim rs As ADODB.Recordset
Dim cnTemp As New ADODB.Connection
cnTemp.Open(MyConnection)
rs = cnTemp.Execute(ConnectionString)
MsgBox(rs.Fields(2).Value)
MsgBox(rs.Fields(3).Name)
cnTemp.Close()
End Sub

I get the following error at the line
MsgBox(rs.Fields(2).Value)

An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in adodb.dll

Additional information: Either BOF or EOF is True, or the current
record has been deleted. Requested operation requires a current
record.

It reads the recordset fine, since I get no errors if I change Value
to Name. My guess is that the recordset can potentially return more
than one record, and hence more than one potential value for that
field. So how do I get around this by pointing instead to the first
record in the recordset? I've tried rs.movefirst before accessing
the Value, but I get the same error.

Also, I would like to disconnect the recordset before doing anything
with it, but ending the connection appears to wipe out the recordset
as well, as counting the fields before (6) and after (0) closing the
connection shows. Should I create another recordset to move rs into,
or should I just not bother and close cnTemp at the end?
--
Cheers, ymt.
Mar 20 '06 #1
3 3855
Yuk,

In dotNet is ADODB almost not used except in conversion projects from VB6.

Therefore you can better try another group, which is as well not the best
one for this question, however in which are some regulars who answers these
questions as well. (There is a better one, however I have looked where the
last week active in that and that is not so high active.)

I would try it in your case in:
microsoft.public.dotnet.framework.adonet

You can try as well
microsoft.public.datatabase.ado
Which has the problem at the moment that it is not so active.

It does not mean that you are not welcome here or that there are no persons
active in this newsgroup who cannot answer this question. However as with
me, you have the change that it is very old long time not used knowledge.

Cor

"Yuk Tang" <ji********@yahoo.com> schreef in bericht
news:Xn*******************************@130.133.1.4 ...
I'm trying to grab the fieldnames and values from a recordset, but
I'm getting errors. I have an idea what the error might come from,
but I'm not sure how to correct it.

I'm connecting to an Access db which has the table 'Users', and I
want to grab the fieldnames and values without necessarily knowing
their numbers and formats. The msgboxes are there to illustrate the
error.

To run this, you need an Access db called Users.mdb in the executable
directory, a table called Users in that db, at least 4 fields in the
table, including a field called UserName. One of the records must
have the UserName Fred. For older versions of Access, OLEDB.4.0 needs
to be changed (3.5 instead of 4.0 for Access 97 IIRC).

Public Sub GetValues
MyConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Settings
\Users.mdb;"
ConnectionString = "SELECT * FROM Users WHERE UserName='Fred';"
Dim rs As ADODB.Recordset
Dim cnTemp As New ADODB.Connection
cnTemp.Open(MyConnection)
rs = cnTemp.Execute(ConnectionString)
MsgBox(rs.Fields(2).Value)
MsgBox(rs.Fields(3).Name)
cnTemp.Close()
End Sub

I get the following error at the line
MsgBox(rs.Fields(2).Value)

An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in adodb.dll

Additional information: Either BOF or EOF is True, or the current
record has been deleted. Requested operation requires a current
record.

It reads the recordset fine, since I get no errors if I change Value
to Name. My guess is that the recordset can potentially return more
than one record, and hence more than one potential value for that
field. So how do I get around this by pointing instead to the first
record in the recordset? I've tried rs.movefirst before accessing
the Value, but I get the same error.

Also, I would like to disconnect the recordset before doing anything
with it, but ending the connection appears to wipe out the recordset
as well, as counting the fields before (6) and after (0) closing the
connection shows. Should I create another recordset to move rs into,
or should I just not bother and close cnTemp at the end?
--
Cheers, ymt.

Mar 20 '06 #2
Hi Yuk,

the following code works for me:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim i As Int16
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

Try
If cn.State <> 1 Then
With cn
.CursorLocation =
ADODB.CursorLocationEnum.adUseClient
.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=<YOUR DATABASE FILENAME GOES HERE>")
End With
End If

If rs.State <> 1 Then
rs.Open("SELECT * FROM HOSTS", cn,
ADODB.CursorTypeEnum.adOpenDynamic,
ADODB.LockTypeEnum.adLockOptimistic)
End If

With rs
For i = 0 To .Fields.Count - 1
Console.WriteLine(.Fields(i).Name & " - " &
..Fields(i).Value)
Next
End With

Catch ex As Exception
MsgBox("ERROR: " & ex.Source & " - " & ex.Message & vbCrLf
& ex.StackTrace, MsgBoxStyle.Critical)
Finally
If rs.State = 1 Then
rs.Close()
End If

If cn.State = 1 Then
cn.Close()
End If
End Try
End Sub
hth,

diego

Mar 20 '06 #3
Thanks for the replies. I guess I'll have to switch over to ADO.NET or
something sooner or later.
--
Cheers, ymt.
Mar 20 '06 #4

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

Similar topics

37
by: middletree | last post by:
Yesterday, I posted a problem which, by the way, I haven't been able to solve yet. But in Aaron's reply, he questioned why I did several things the way I did. My short answer is that I have a lot...
2
by: InvisibleMan | last post by:
Hi, I feel a little dumb for asking this (considering im writing TSQL) but there doesn't seem to be any definitive answers on the search engines... Okay I understand that if you open the ADO...
1
by: jason | last post by:
hello, i have a C# class library that i recently had to add ADODB functionality to, in order to support consumer applications that can only work with recordsets. i added the ADODB library...
3
by: | last post by:
Hello ppl, I have snippet that works fine udner ADODB with VB6, but something wrong with it in Vb.NET. Anyone can help? Recordset1 (ADODB.Recordset) Error: Arguments are of the wrong type, are...
6
by: Wonder | last post by:
We have a VB.NET project with a reference to ADO 2.5 When I open it on my machine, I get the following build error: "The referenced component 'ADODB' has an updated custom wrapper available." ...
7
by: Peter Newman | last post by:
Im still trying to get my head around this, but its the only way i have seen to be able to create a 'recordset' and bind text boxex to it and navigate throw each record by the click of a button ......
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
7
by: boyleyc | last post by:
Hi all I have written a database in access and used ADODB recordsets all the way through. The only recordsets that are not ADODB are the listbox navigation code automatically generated by access...
16
patjones
by: patjones | last post by:
Hi - I am just starting my study of ADO, and I have a purpose in my current project which seems to lend itself to using an ADODB recordset. The set up is this. I have a table tblScreenShot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.