?
Solved

SQL Select Query

Posted on 2014-01-16
3
Medium Priority
?
192 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

770 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