SQL connection and recordset

abenitez77

Board Regular
Joined
Dec 30, 2004
Messages
149
I am not able to get values into a recordset. The first sql command to insert the records work, but when i try to retrieve the Max(ID), the recordset is Empty.



Dim rst2 As DAO.Recordset

Set Connection2 = CreateObject("ADODB.Connection")

sServer = "SAS01SRVSQ01\SASDATA"
sDBName = "UNFI_Temp"
ConnectionString = _
"Provider=SQLOLEDB;" & _
"Data Source=" + sServer + ";" & _
"Initial Catalog=" + sDBName + ";" & _
"Integrated Security=SSPI"

Connection2.Open ConnectionString


'Insert Header info into XlsData
tSQL = "Insert into XLSData (SupplierName, SupplierNumber, BuyDateS, ShipDateS, FilePath, FileName) "
tSQL = tSQL & "VALUES ('" & Supplier & "','" & SupplierNo & "','" & BuyDate & "','" & ShipDate & "','" & xFilePath & "','" & xFileName & "') "

Set rst2 = Connection2.Execute(tSQL)

rst2.MoveLast
rst2.MoveFirst

tSQL = "Select Max(ID) as IDMax From XLSData"
Set rst2 = Connection2.Execute(tSQL)
rst2.MoveLast
rst2.MoveFirst

myID = rst2.IDMax
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try opening the recordset.
Code:
Set rst2 = CreateObject("ADODB.RecordSet")

rst.Open tSQL, Connection2.
 
Upvote 0
ok, i fixed that part...and in the watch I can see the recordset but how do I get the value from the recordset into a variable? I tried the syntax below..but it did not get the value.

myID = rst.IDMax
 
Upvote 0
Try this,

Rich (BB code):
tSQL = "Select Max(ID) as IDMax From XLSData"
if rst2.state=1 then rst2.close
rst2.Open tSQL, Connection2
rst2.MoveLast
rst2.MoveFirst

myID = rst2.fields(0)
 
Last edited:
Upvote 0
ok, i fixed that part...and in the watch I can see the recordset but how do I get the value from the recordset into a variable? I tried the syntax below..but it did not get the value.

myID = rst.IDMax


Code:
myID=rst2.fields(0)
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top