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
Solved

SQL Select Query

Posted on 2014-01-16
3
180 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

828 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