Solved

SQL Select Query

Posted on 2014-01-16
3
188 Views
Last Modified: 2014-05-01
I am trying to write and SQL select query based on criteria in various cell, one being a unique identifier and the other being a date, however I am receiving a Syntax error in my code and can't see where the error is.


The error is being caused in the 'create query portion of the code.

Sub GetTransactions()
With wsInt
    If Worksheets("DATA").Range("F2").Value = "" Then
        'MsgBox " Base Number Required "
        Exit Sub
    End If
End With

    Dim Cn As ADODB.Connection, Rs As ADODB.Recordset
    Dim MyConn, sSQL As String
    Dim Rw As Long, Col As Long, c As Long
    Dim MyField, Location As Range
'    Dim begindate As Date
'    Dim enddate As Date
   
'Set destination
    Set Location = [C24]

'Set source
    MyConn = "\\ldnvnascrp0055\gcg_gbr_misipb2\Sales\IPB\BAU\ISR\ISR.accdb"

'Create query
    sSQL = "SELECT * FROM QryISRtxns WHERE base =" & Worksheets("DATA").Range("F2").Value And txn_date <= " & Worksheets("DATA").Range("K8").Value
   
       
'Create RecordSet
    Set Cn = New ADODB.Connection
    With Cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Open MyConn
        Set Rs = .Execute(sSQL)
    End With
     
  'Write RecordSet to results area
    Worksheets("DATA").Unprotect ("ISR12345")
    Range("C24:M2000").Select
    Selection.ClearContents
    Range("F2").Select
     
 'Write RecordSet to results area
    Rw = Location.Row
    Col = Location.Column
    c = Col
    Do Until Rs.EOF
        For Each MyField In Rs.Fields
            Cells(Rw, c) = MyField
            c = c + 1
        Next MyField
        Rs.MoveNext
        Rw = Rw + 1
        c = Col
    Loop
    Set Location = Nothing
    Set Cn = Nothing
  '''''''''''  Worksheets("DATA").Protect ("ISR12345")
  End Sub
0
Comment
Question by:DAFranzmann
[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
3 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 500 total points
ID: 39785169
Try this for the sSQL:

sSQL = "SELECT * FROM QryISRtxns WHERE base =" & Worksheets("DATA").Range("F2").Value & " And txn_date <= " & Worksheets("DATA").Range("K8").Value

Open in new window


You may need to format the date going to the SQL too:

sSQL = "SELECT * FROM QryISRtxns WHERE base =" & Worksheets("DATA").Range("F2").Value & " And txn_date <= " & Format(Worksheets("DATA").Range("K8").Value,"dd-mmm-yyyy")

Open in new window

0
 
LVL 8

Expert Comment

by:Surrano
ID: 39785232
Missing & " here:

Value And txn_date
=>
Value & " And txn_date
0
 

Author Closing Comment

by:DAFranzmann
ID: 40034038
Works perfectly, thanks for your help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Writing 0 to a range of cells in Excel 10 24
NEED TO LOOK FOR EXACT NUMBER 14 43
Excel formula to calculate ID # 4 41
Open attachments in viewer, not main app 4 29
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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…

734 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