Solved

SQL Select Query

Posted on 2014-01-16
3
151 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
3 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 500 total points
Comment Utility
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
Comment Utility
Missing & " here:

Value And txn_date
=>
Value & " And txn_date
0
 

Author Closing Comment

by:DAFranzmann
Comment Utility
Works perfectly, thanks for your help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now