Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Select Query

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

604 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