Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using simple sql select statement to assign value to variable 1

Status
Not open for further replies.

ije

MIS
Jun 10, 2002
38
0
0
AU
I am trying to assign the results of a a SQL select statement to a variable, but the result is always blank ie it appears no value is returned.

I am only a casual vbscripter and thought someone could assist?

Below is the code I am using, which at the moment I would be happy to have the returned value displayed in a messagebox. BTW, the SQL statement works when used in Query Analyser.

Thanks in advance heros.

Dim conn, orecordset
Dim sop_number,agency,message,selectSQL

sop_number= cstr(wscript.arguments(0))
agency= cstr(wscript.arguments(1))

Set conn = createobject("ADODB.Connection")
conn.open "dsn=heat-sdg;uid=sa;pwd=benice;database=SDG"

SelectSQL= "select filepath from filetable where sop_number ='" & trim(sop_number) & "' and agency='" & trim(agency) & "'"

Set oRecordSet = CreateObject("ADODB.Recordset")
oRecordSet.Open SelectSQL, Conn

If oRecordSet.RecordCount =1 Then
message.Value = oRecordSet("filepath").Value
End If

oRecordSet.Close
Conn.Close

Set oRecordSet = Nothing
Set Conn = Nothing

msgbox"The value of message is: " & message
 
1)Comment out all Error Resume Next instructions, if any.
2)If you want to use the RecordCount property, you have to choose the proper cursor type, like this:
oRecordSet.Open SelectSQL, Conn, 1
3)Not sure that message.value will not raise an error
To display a message box, try something like this:
MsgBox oRecordSet("filepath").Value

Hope This Help
PH.
 
thanks PH - i am not quite I understand ( due to my inexperience I am sure! ). Would you have a simple example you are able to share?

Many thanks
ije [afro2]
 
I'm not sure myself about the default CursorType and support for RecordCount, but have you tried replacing:

If oRecordSet.RecordCount =1 Then
message.Value = oRecordSet("filepath").Value
End If

By:

oRecordSet.MoveFirst
message = oRecordSet("filepath").Value

This does 3 things:

1.) Avoids messing with cursors right now.
2.) Corrects the error in setting "message."
3.) Does the MoveFirst which is probably going to be required anyway.

I'm too tired to look up CursorTypes and RecordCount support right now, but you ought to check that out too.
 
thanks all

I have it working with:

Set oRecordSet = CreateObject("ADODB.Recordset")
oRecordSet.Open SelectSQL, Conn
message= oRecordSet.fields("filepath")

this works fine, but does not cater for situations where it finds no records or multiple records. That is the next bit i guess for me....


 
Microsoft says:

The cursor type of the Recordset object affects whether the number of records can be determined. The RecordCount property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and either -1 or the actual count for a dynamic cursor, depending on the data source.

Quote from:

You probably want an adOpenStatic (which is 3) cursor type in your case. As suggested by PHV you can also use an adOpenKeySet (1) cursor type. The real issue here is whether or not your table is undergoing concurrent deletes that you care about. When "harvesting" a few records for reporting a static cursor is probably going to be fastest. If you are using a cursor location of adUseClient (3) you have no choice - you have to use a static cursor to accomplish what you want here. The default is adUseServer (2) but for an Access database for example it usually makes sense to go with a server side cursor.


Microsoft says:

As a general rule, you should use server-side cursors for working with Access databases, and client-side cursors when working with SQL Server databases. However, although using client-side cursors with Access databases generates additional overhead to cache records and doesn't expose all of the functionality of the Jet database engine, you still may want to use client-side cursors with Access databases if you are working with a remote database, or if you need to ensure uniformity of behavior when working with multiple data sources.

Quote from:

The test for an empty Recordset is if .BOF and .EOF are both true.

Microsoft says:

If you open a Recordset object containing no records, the BOF and EOF properties are set to True (see the RecordCount property for more information about this state of a Recordset). When you open a Recordset object that contains at least one record, the first record is the current record and the BOF and EOF properties are False.

Quote from:

These things can be looked up easily in the MDAC 2.6 SDK. Here is a download link (12MB or so):


Much of this and more information is available in the MSDN Library CDs or online at MSDN Library Online:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top