Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem running sql in excel (Excel 97)

    I have written a module in excel that will work with the following snipit of code:

    strdatetime = "SELECT DISTINCT AMOSTONS.DATE_TIME FROM AMOSTONS WHERE (AMOSTONS.DATE_TIME)=" & strdt1

    Set rsAmmo = dbAAR.OpenRecordset(strdatetime)

    strdt1 is set earlier in the code and is actually taken from a cell on a worksheet. This all works fine.

    My problem is that I want to do the same thing as above except I may have more than one date/time (strdt1 or strdt2 , etc..) Instead of AMOSTONS.DATE_TIME = to only strdt1 it needs to be an or statement because it could also be equal to strdt2.

    I have tried it several ways and keep hitting a deadend, any suggestions??
    Thanks

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Problem running sql in excel (Excel 97)

    Something like this:

    strdatetime = "SELECT DISTINCT AMOSTONS.DATE_TIME FROM AMOSTONS WHERE (AMOSTONS.DATE_TIME)=" & strdt1
    If strdt2<>"" then
    strdatetime=strdatetime & " OR " strds2
    End If

    Any Help?
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Problem running sql in excel (Excel 97)

    Hi,
    I think you'd actually need to use something like:
    strdatetime = "SELECT DISTINCT AMOSTONS.DATE_TIME FROM AMOSTONS WHERE (AMOSTONS.DATE_TIME)=" & strdt1
    If strdt2<>"" then
    strdatetime=strdatetime & " OR (AMOSTONS.DATE_TIME)=" & strdt2
    End If
    Alternatively, if there are going to be more than 2 possibilities, the IN clause might be easier.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Problem running sql in excel (Excel 97)

    SELECT DISTINCT AMOSTONS.DATE_TIME FROM AMOSTONS WHERE (AMOSTONS.DATE_TIME)=" & strdt1 & "or (AMOSTONS.DATE_TIME)=" _
    & strdt2

    Thanks for your help, I figured the above out before I read your post but virtually it is the same thing. Thanks again.

  5. #5
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Re: Problem running sql in excel (Excel 97)

    Rory

    Another question I have, after I search the messages, could I run a SQL Stored Procedure from VBA by using its name?

    My co-worker has developed 4 stored procedures and he wants each to return results to 4 worksheets respectively. I was thinking of doing something like:

    With shtFirstSheet
    With .QueryTables.Add(Connection:="ODBC;DSN=sServer;UID =" & sUID & ";PWD=" & sPWord & ";APP=Microsoft
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Problem running sql in excel (Excel 97)

    Hi Wassim,

    I simply recorded this macro (Data, get external data, run saved query):

    With ActiveSheet.QueryTables.Add(Connection:= _
    "FINDER;C:WINDOWSApplication DataMicrosoftQueriesQuery from MS Access 97 Database.dqy" _
    , Destination:=Range("A1"))
    .Name = "Query from MS Access 97 Database"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = True
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
    End With
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  7. #7
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Problem running sql in excel (Excel 97)

    Hi Wassim,
    To be honest I don't know! I'd suggest trying it and see if it works. I'd also suggest using the commandtext property rather than the Sql property, as that's what MS recommends. If you're interested in increasing performance, you'd probably be better off using ADO to connect to the database via an OLEDB provider and then use the Range.Copyfromrecordset method to put the data into your worksheets.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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