+ Reply to Thread
Results 1 to 5 of 5

Query work in access 2003 BUT not when using VBA in Excel Cell

  1. #1
    Registered User
    Join Date
    11-24-2008
    Location
    san francisco
    Posts
    3

    Smile Query work in access 2003 BUT not when using VBA in Excel Cell

    Hi: First thing to metion is that the result field I expect is a Memo Field
    When I run the following (qry*)query in access I do get the expected one (row*)row as the result-However when I use this (modl*)code into an Excel VBA module-nothing happens- I tested for recorcound and it does show -1. Just wondering why I Am not getting the results when using VBA in Excel-Thanks in advance
    ****** IN ACCESS THIS WORKS FINE **********
    (qry*)
    SELECT Periods_Desc FROM FCST_Template_SGA GROUP BY Pull_Date, Template_Name, Periods_Desc HAVING Template_Name="AFTRHRS";
    (row*)
    JAN 08 ACTUAL-FEB 08 ACTUAL-MAR 08 ACTUAL-APR 08 ACTUAL-MAY 08 ACTUAL-JUN 08 ACTUAL-JUL 08 ACTUAL-AUG 08 ACTUAL-SEP 08 ACTUAL-OCT 08 6+6 FCST-NOV 08 6+6 FCST-DEC 08 6+6 FCST-
    ******HOWEVER IN EXCEL IT DOES NOT DISPLAY ANYTHING ****
    (modl*)
    Private Const cDir_Database As String = "C:\Documents and Settings\BLABLA\MRA_Database.mdb"

    Public DB_Conn As ADODB.Connection 'Access connection
    Public DB_RSet As ADODB.Recordset 'Access Record Set
    Public DB_SQL As String 'SQL Commands
    '======
    Private Sub UserForm_Initialize()
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Set wbBook = ActiveWorkbook
    Set wsSheet = wbBook.Worksheets("SGA")
    OPEN_DATABASE
    '*** HEADER
    DB_SQL = "SELECT Periods_Desc FROM FCST_Template_SGA GROUP BY Pull_Date, Template_Name, Periods_Desc HAVING Template_Name='" & "AFTRHRS_SF_UBH" & "';"
    With DB_RSet
    Application.StatusBar = "Checking Valid Source Codes..."
    .Open DB_SQL, _
    DB_Conn, _
    adOpenForwardOnly, _
    adLockReadOnly 'Open the list of Source Codes
    MsgBox CStr(.RecordCount)

    wsSheet.Range("A1").CopyFromRecordset DB_RSet
    .Close
    End With
    CLOSE_DATABASE
    End Sub

    '===
    Public Sub OPEN_DATABASE()
    Application.StatusBar = "Connecting to the Database..."
    Application.Cursor = xlWait
    Set DB_Conn = New Connection 'Database Connection
    Set DB_RSet = New Recordset 'Database RecordSet
    DB_Conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" & cDir_Database
    DB_Conn.Open
    DB_Conn.BeginTrans

    Application.StatusBar = False
    End Sub
    '=====
    Public Sub CLOSE_DATABASE()
    Application.StatusBar = "Disconnecting from the Database..."
    DB_Conn.CommitTrans 'Commit All Work
    DB_Conn.Close 'Close the connection
    Set DB_RSet = Nothing

    Application.Cursor = xlDefault
    Application.StatusBar = False
    End Sub

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464
    Hi,

    Well the simple answer is that Excel VBA syntax is different, (quite different) to the Access syntax.

    Regards

  3. #3
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717
    Well if you want to get the resultant data in Excel..

    U can you Excel Feature .. Get External Data...

  4. #4
    Registered User
    Join Date
    11-24-2008
    Location
    san francisco
    Posts
    3

    Question sql is Sql?

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Well the simple answer is that Excel VBA syntax is different, (quite different) to the Access syntax.

    Regards
    Can you be a bit more specific or direct me to a website that will explain what you are trying to say?
    I thought SQL inbedded in VBA or if used in Access- is the same -SQL is SQL?

    THANKS

  5. #5
    Registered User
    Join Date
    11-24-2008
    Location
    san francisco
    Posts
    3

    Lightbulb needed trim fro Query work in access 2003 BUT not when using VBA in Excel Cell

    I figured it out- I needed to add trim to the recordset I am bringin.
    As I suspected - I think the cell in excel cannot acomodate the the memo field beign brought over(to big for excel)
    REGARDS,

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1