Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 4th, 2012, 02:06 PM
fred2028 fred2028 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 142 fred2028 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 32 m 7 sec
Reputation Power: 7
Returning # of rows in a SELECT statement with VBA

I'm trying to execute a select statement from VBA and want to work with the returned values. Basically I want to test if there are already rows in existence for specific conditions. I think the logic here would be to execute a select query, assign the returned values to a RecordSet, and do a count of the recordset. However, I'm not sure how to do this in VBA. I can execute delete and insert commands, but for commands like select where it returns data, I'm not sure how to do it. Only the VBA part is foreign to me, I know my way around the SQL. Any ideas?
__________________
Cheers,
Fred Liu

Reply With Quote
  #2  
Old June 4th, 2012, 02:16 PM
medialint's Avatar
medialint medialint is offline
Type Cast Exception
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,939 medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)  Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Months 2 Weeks 2 Days 1 h 38 m 6 sec
Reputation Power: 8490
Facebook
SQL is the same as in the native DB environment. You make the connection to whatever (DB2, Oracle, etc) and execute the same query you'd use directly against your database. Including all the date formatting quirks, wild card variances, etc.

Assign the result to a recordset and you can use .RecordCount on the recordset (if read only) to get the number of records. Or refer to individual fields in the recordset as rs![Field Name] and use .MoveNext, .EOF etc properties of the recordset accordingly.

Simplified

Code:
    cCon.Open MyConnectionString
    cRS.Open strSQL, cCon, adOpenStatic, adLockReadOnly
    MsgBox cRS.RecordCount
    MsgBox cRS.Fields(1).Name
    MsgBox cRS.Fields(1).Value
'etc


NOTE: Above example is ADO not DAO
__________________
medialint.com

“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss

Reply With Quote
  #3  
Old June 4th, 2012, 02:55 PM
fred2028 fred2028 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 142 fred2028 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 32 m 7 sec
Reputation Power: 7
Thanks. I've incorporated it into my code, however I am getting a VBA error at runtime saying my recordSetFred.Open line has arguments that are of the wrong type ... The code works fine before, so the DB connections should be right.
Code:
Public Sub uploaddata()
    Dim conn As connect
    Set conn = New connect
    
    Dim tableindex() As String
    
    Dim i As Integer
    Dim sqlstr As String
    
    Dim recordSetFred As Recordset
    Set recordSetFred = New Recordset
    
    'connect to DEV
    'conn.connect_devstf
    conn.connect_devstf
    
    'delete existing data
    tableindex = getuniqueinventory

    For i = 0 To UBound(tableindex)
        ' The order of the tableindex is processDateInt, inventoryID, traderID
        sqlstr = "delete from equity.dbo.EnhanceGamma " + _
            " where ProcessDateInt = " + Split(tableindex(i), ";")(0) + _
            " and inventoryId = '" + Split(tableindex(i), ";")(1) + "'"
        conn.m_rds.Open sqlstr, conn.m_conn
    Next
    
    'insert new data into UploadedExpiry table
    i = 1
    Do While Range("Expiry").Offset(i, 0) <> ""
        sqlstr = "select ticker from equity.dbo.enhancegamma" & _
            " where ticker = '" & Range("Expiry").Offset(i, 1) & "'" & _
            " and exchange = 'ca'" & _
            " and spot = " & Range("Expiry").Offset(i, 5)
            
        recordSetFred.Open sqlstr, conn, adOpenStatic, adLockReadOnly
        MsgBox recordSetFred.RecordCount
            
        With Range("Expiry")
            sqlstr = "INSERT INTO equity.dbo.EnhanceGamma " & vbCrLf & _
                     "VALUES(" & .Offset(i, 0) & ", " & _
                     "      '" & .Offset(i, 1) & "', " & _
                     "      'CA', " & _
                     "      '" & .Offset(i, 2) & "', " & _
                     "       " & .Offset(i, 3) & ", " & _
                     "       " & .Offset(i, 4) & ", " & _
                     "       " & .Offset(i, 5) & ", " & _
                     "       " & .Offset(i, 6) & ", " & _
                     "       " & .Offset(i, 7) & ", " & _
                     "       " & .Offset(i, 8) & ", " & _
                     "      '" & .Offset(i, 9) & "' " & _
                     "       )"
        End With
        conn.m_rds.Open sqlstr, conn.m_conn
        i = i + 1
    Loop
    conn.disconn
    Set conn = Nothing
End Sub

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Returning # of rows in a SELECT statement with VBA

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap