?
Solved

Excel VBA SQL - getting last ID

Posted on 2011-09-10
2
Medium Priority
?
198 Views
Last Modified: 2012-05-12
Hi

I am trying to get the last ID entered using the code below
and at the line   varID = .Execute()
the variable varID seems to be blank even though there is no error
How do I get the integer value of the last ID ?

Public Sub oAppend(ByVal oDate As Date, ByVal oText As String, ByVal oNumber As Single)
 
    Dim oSQL As String
    
    On Error GoTo EH
    
    Set con = New ADODB.Connection
    
    con.Open "Provider=SQLOLEDB;Data Source=196.220.43.247,1444;Network Library=DBMSSOCN;Initial Catalog=test;User ID=mu;Password=ct3;"

    Set cmd = New ADODB.Command

    'Check last ID ---------------------------
    
    Dim oSQL_lastID As String
    oSQL_lastID = "SELECT MAX(ID) FROM Table1"

    
    Dim varID As Variant
    Dim intID As String
    
    With cmd
        .CommandText = oSQL_lastID
        .CommandType = adCmdText
        .ActiveConnection = con
        '.Execute
        varID = .Execute() 'varID is blank
    End With
    intID = CStr(varID) 'Error here
    '------------------------------------------

Open in new window

0
Comment
Question by:Murray Brown
[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
2 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36517464
As I said http:Q_27301456.html#a36516694 the .Execute() is returning a Recordset; therefore, you have to use it as such.

i.e., try: Set rs = .Execute()
varID = rs(0)
0
 

Author Closing Comment

by:Murray Brown
ID: 36518225
thanks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

777 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