473,460 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Parse a txt field to a runSQL command

8
I have a form that enables the end-user to "build" a SQL query. They can select up to 11 fields, use multiple Select conditions, and 4 sort levels. As they build the query it is put together in a txt field. The next step was to run the SQL, which I do from a command button, the code is as follow.

dim qryOutput as string

qryOutput = me.txtQry.Value ( THE TEXT BOX on the FORM)

docmd.runSQL "qryOutput" (Have tried various forms of this, used brackets and or commas)

I get an error saying the runSQL expects "SELECT" or "INSERT", ETC...

How can I run the built SQL statement?

Thanks for your help!

PW
Aug 30 '07 #1
3 1414
JKing
1,206 Expert 1GB
The RunSQL command is only for action queries. Deletes, select into, insert into etc.

If you want to create queries on the fly I suggest doing the following.

Step 1: Create a query and save it. It doesn't matter what you include in it as long as you save it. Access doesn't allow you to save a blank query so we're are doing this to get around that. You can call it whatever you want but for this example we will call it qryMyQuery

Step 2: Through code declare a querydef object and change the SQL of the query to reflect your built SQL.

Step 3: Open the altered query.

Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. Dim qdf As DAO.QueryDef
  3. Dim strSQL As String
  4.  
  5. Set db = CurrentDb
  6. Set qdf = db.QueryDefs("qryMyQuery")
  7.  
  8. strSQL = "SELECT * FROM yourTable"
  9. qdf.SQL = strSQL
  10.  
  11. DoCmd.OpenQuery("qryMyQuery")
  12.  
  13. Set qdf = nothing
  14. Set db = nothing
  15.  
Aug 31 '07 #2
paulw4
8
Still not able to take the data in the txt field and have it run as an SQL statement.

Below is the data in the text field txtqry on the form "test"
Expand|Select|Wrap|Line Numbers
  1. SELECT [ContactFirstName], [ContactLastName], [Delivery City] FROM Customers  WHERE [ContactLastName] > "D" ORDER BY [Delivery City], [ContactLastName]
  2.  
Thanks to your help I have the following code.

The "qrytest1" saved select query outputs zip code only....
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2.     Dim qdf As DAO.QueryDef
  3.     Dim strSQL As String
  4.     Set db = CurrentDb
  5.     Set qdf = db.QueryDefs("qrytest1")
  6.     strSQL = Me.txtQry.Value                 (Have also tried forms!test.txtqry)
  7.     DoCmd.OpenQuery ("qrytest1")
  8.  
When this run I get the zip code only output, not what the built select query is.

The intent is that the qrytest1 will run select statement from the txtqry text box.

Again, thank for the help
PW
Aug 31 '07 #3
JKing
1,206 Expert 1GB
You missed a key line in my previous post.

You're missing this line:
Expand|Select|Wrap|Line Numbers
  1. qdf.SQL = strSQL
  2.  
Aug 31 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: me here | last post by:
I have a VBA subroutine that links an MS Excel spreadsheet and copies the data into a local table. This process is controlled by a form that allows users to select the spreadsheet from the file...
13
by: MLH | last post by:
I have a form with two controls: !! - combo box !! - text box A button on the form tries to run this SQL when clicked... INSERT INTO BodyMsgsSent (ToWhom, BodyText) SELECT DISTINCTROW !! AS...
3
by: Pathfinder | last post by:
Hi All I am trying to run the below but I receive the following error "runsql action requires an argument consisting of an SQL statment" Dim MySQL$ MySQL$ = "Select * from mytablename"...
7
by: Richard Hollenbeck | last post by:
Help! I don't know why this isn't working: Private Sub Combo9_Change() Dim UsersCourseSelection As String UsersCourseSelection = Me("Combo9").Value Combo13.Visible = True 'the following...
1
by: sierra467 | last post by:
I realize that lookup fields in a table should not be used but that is the way this particular creator has done. Could someone help me by answering my question. I am trying to run a...
4
by: Mark Reed | last post by:
Hi All, here is what I am trying to achieve. I have a memo field on a form which users will need to add to as and when. I do not want them to be able to edit information which already exists...
4
by: Bob | last post by:
Hi all, I've got a table that I've imported and it has junk at the top of the table, so after import I run a delete query to remove the junk lines then I'm left with the field names I want for...
2
by: jujube | last post by:
Hi, I thought I found a great command in RunSQL, but.... In my Unload event for a form, I added some clean-up code: Private Sub Form_Unload(Cancel As Integer) If Not IsNull(Me.ID.Value) And...
2
by: prakashwadhwani | last post by:
I have a continuous form bound to an underlying table. How do I change the value of one field (all rows) to a particular constant say eg. 1998. Someone on the Group here mentioned I should...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.