Solved

Problem with sql query and variable in access

Posted on 2014-09-20
3
637 Views
Last Modified: 2014-09-20
Hi,
   I have written some code which basically takes the value of a query from a dropdown, as well as values from 2 listboxes which contain the start range and end range for an ID.....  This is the code I have:

Dim rs As DAO.Recordset
    Dim xlApp As Object
    Dim xlWb As Object
    Dim xlWs As Object
    Dim Counter As Long
    Dim QueryName As String
    Dim SaveToPath As String
    Dim TemplatePath As String
    Dim firstidnum As String
    Dim secondidnum As String
    
    firstidnum = firstid.Value
    secondidnum = secondid.Value
    
    QueryName = template_dropdown.Value
    SaveToPath = "\\server\share\Database\Generated Reports\" & QueryName & "_"
    
    If QueryName = "DCC" Then
        TemplatePath = "\\server\share\Database\comments-template-modified.xlsx"
    ElseIf QueryName = "DCC Adult" Then
        TemplatePath = "\\server\share\Database\comments-template-modified.xlsx"
    ElseIf QueryName = "Healthcare Report" Then
        TemplatePath = "\\server\share\Database\comments-template-modified.xlsx"
    Else
        TemplatePath = "\\server\share\Database\comments-template.xlsx"
    End If  
    
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM " & QueryName & " WHERE ID BETWEEN " & firstidnum & " AND " & secondidnum & "")
 
    Set xlApp = CreateObject("Excel.Application")
    xlApp.DisplayAlerts = False
    Set xlWb = xlApp.Workbooks.Add(TemplatePath)
    Set xlWs = xlWb.ActiveSheet
   With xlWs
        For Counter = 0 To rs.Fields.Count - 1
            .Cells(2, 1).CopyFromRecordset rs
        Next
 '       .Cells(2, 1).CopyFromRecordset rs
    End With
    xlWb.SaveAs SaveToPath & Format(Now, "yyyymmdd") & ".xlsx"
    xlWb.Close False
    xlApp.DisplayAlerts = True
    Set xlWs = Nothing
    Set xlWb = Nothing
    xlApp.Quit
    Set xlApp = Nothing
    rs.Close
    Set rs = Nothing
 
    MsgBox "The select report has been generated..."

Open in new window

The problem is with this line here:

Set rs = CurrentDb.OpenRecordset("SELECT * FROM " & QueryName & " WHERE ID BETWEEN " & firstidnum & " AND " & secondidnum & "")

It keeps giving me a "Syntax error in FROM clause"

Basically the value QueryName is from a dropdown listbox which contains all the queries, I then want to do a BETWEEN statement, so only the ID's selected via firstidnum and secondidnum are output...

Can anyone help?

Cheers

Damian
0
Comment
Question by:damianb123
[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
  • 2
3 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40334344
test this, wrap the variable "QueryName" in brackets

Set rs = CurrentDb.OpenRecordset("SELECT * FROM  [" & QueryName & "]  WHERE ID BETWEEN " & firstidnum & " AND " & secondidnum & "")
0
 

Author Comment

by:damianb123
ID: 40334352
WELL DONE!  Perfect, just what I needed my friend.

Damian
0
 

Author Closing Comment

by:damianb123
ID: 40334353
WELL DONE!  Perfect, just what I needed my friend.

Damian
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

756 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